(t *testing.T)
| 7 | ) |
| 8 | |
| 9 | func Test_CommitHook(t *testing.T) { |
| 10 | path := mustTempPath() |
| 11 | defer os.Remove(path) |
| 12 | db, err := Open(path, false, false) |
| 13 | if err != nil { |
| 14 | t.Fatalf("error opening database") |
| 15 | } |
| 16 | defer db.Close() |
| 17 | mustExecute(db, "CREATE TABLE foo (id INTEGER PRIMARY KEY, name TEXT)") |
| 18 | |
| 19 | count := &atomic.Int32{} |
| 20 | hook := func() bool { |
| 21 | count.Add(1) |
| 22 | return true |
| 23 | } |
| 24 | if err := db.RegisterCommitHook(hook); err != nil { |
| 25 | t.Fatalf("error registering commit hook") |
| 26 | } |
| 27 | |
| 28 | // A select should not trigger the hook and a basic insert should trigger the hook. |
| 29 | mustQuery(db, "SELECT * FROM foo") |
| 30 | mustExecute(db, "INSERT INTO foo(name) VALUES('fiona')") |
| 31 | if count.Load() != 1 { |
| 32 | t.Fatalf("expected count 1, got %d", count.Load()) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Test_CommitHook_Rollback demonstrates that the Commit hook is not called for a |
| 37 | // transaction which is rolled back. |
nothing calls this directly
no test coverage detected