@@ -245,3 +245,46 @@ func TestBackupStepByStep(t *testing.T) {
245
245
func TestBackupAllRemainingPages (t * testing.T ) {
246
246
testBackup (t , testRowCount , false )
247
247
}
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