Skip to content

Commit 4b0af85

Browse files
authored
Merge pull request #331 from a-p-/test-backup-error
Test the error reporting when preparing to perform a backup.
2 parents 3fb7a0e + 0e686ca commit 4b0af85

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

backup_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,46 @@ func TestBackupStepByStep(t *testing.T) {
245245
func TestBackupAllRemainingPages(t *testing.T) {
246246
testBackup(t, testRowCount, false)
247247
}
248+
249+
// Test the error reporting when preparing to perform a backup.
250+
func TestBackupError(t *testing.T) {
251+
const driverName = "sqlite3_TestBackupError"
252+
253+
// The driver's connection will be needed in order to perform the backup.
254+
var dbDriverConn *SQLiteConn
255+
sql.Register(driverName, &SQLiteDriver{
256+
ConnectHook: func(conn *SQLiteConn) error {
257+
dbDriverConn = conn
258+
return nil
259+
},
260+
})
261+
262+
// Connect to the database.
263+
dbTempFilename := TempFilename(t)
264+
defer os.Remove(dbTempFilename)
265+
db, err := sql.Open(driverName, dbTempFilename)
266+
if err != nil {
267+
t.Fatal("Failed to open the database:", err)
268+
}
269+
defer db.Close()
270+
db.Ping()
271+
272+
// Need the driver connection in order to perform the backup.
273+
if dbDriverConn == nil {
274+
t.Fatal("Failed to get the driver connection.")
275+
}
276+
277+
// Prepare to perform the backup.
278+
// Intentionally using the same connection for both the source and destination databases, to trigger an error result.
279+
backup, err := dbDriverConn.Backup("main", dbDriverConn, "main")
280+
if err == nil {
281+
t.Fatal("Failed to get the expected error result.")
282+
}
283+
const expectedError = "source and destination must be distinct"
284+
if err.Error() != expectedError {
285+
t.Fatalf("Unexpected error message; expected value: \"%v\"; actual value: \"%v\"", expectedError, err.Error())
286+
}
287+
if backup != nil {
288+
t.Fatal("Failed to get the expected nil backup result.")
289+
}
290+
}

0 commit comments

Comments
 (0)