MCPcopy
hub / github.com/pocketbase/pocketbase / TestRunInTransaction

Function TestRunInTransaction

core/db_tx_test.go:11–49  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestRunInTransaction(t *testing.T) {
12 app, _ := tests.NewTestApp()
13 defer app.Cleanup()
14
15 t.Run("failed nested transaction", func(t *testing.T) {
16 app.RunInTransaction(func(txApp core.App) error {
17 superuser, _ := txApp.FindAuthRecordByEmail(core.CollectionNameSuperusers, "test@example.com")
18
19 return txApp.RunInTransaction(func(tx2Dao core.App) error {
20 if err := tx2Dao.Delete(superuser); err != nil {
21 t.Fatal(err)
22 }
23 return errors.New("test error")
24 })
25 })
26
27 // superuser should still exist
28 superuser, _ := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "test@example.com")
29 if superuser == nil {
30 t.Fatal("Expected superuser test@example.com to not be deleted")
31 }
32 })
33
34 t.Run("successful nested transaction", func(t *testing.T) {
35 app.RunInTransaction(func(txApp core.App) error {
36 superuser, _ := txApp.FindAuthRecordByEmail(core.CollectionNameSuperusers, "test@example.com")
37
38 return txApp.RunInTransaction(func(tx2Dao core.App) error {
39 return tx2Dao.Delete(superuser)
40 })
41 })
42
43 // superuser should have been deleted
44 superuser, _ := app.FindAuthRecordByEmail(core.CollectionNameSuperusers, "test@example.com")
45 if superuser != nil {
46 t.Fatalf("Expected superuser test@example.com to be deleted, found %v", superuser)
47 }
48 })
49}
50
51func TestTransactionHooksCallsOnFailure(t *testing.T) {
52 app, _ := tests.NewTestApp()

Callers

nothing calls this directly

Calls 6

CleanupMethod · 0.95
NewTestAppFunction · 0.92
RunInTransactionMethod · 0.65
FindAuthRecordByEmailMethod · 0.65
DeleteMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…