MCPcopy
hub / github.com/pocketbase/pocketbase / TestTransactionFromInnerCreateHook

Function TestTransactionFromInnerCreateHook

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

Source from the content-addressed store, hash-verified

247}
248
249func TestTransactionFromInnerCreateHook(t *testing.T) {
250 t.Parallel()
251
252 app, _ := tests.NewTestApp()
253 defer app.Cleanup()
254
255 app.OnRecordCreateExecute("demo2").BindFunc(func(e *core.RecordEvent) error {
256 originalApp := e.App
257 return e.App.RunInTransaction(func(txApp core.App) error {
258 e.App = txApp
259 defer func() {
260 e.App = originalApp
261 }()
262
263 nextErr := e.Next()
264
265 return nextErr
266 })
267 })
268
269 app.OnRecordAfterCreateSuccess("demo2").BindFunc(func(e *core.RecordEvent) error {
270 if e.App.IsTransactional() {
271 t.Fatal("Expected e.App to be non-transactional")
272 }
273
274 // perform a db query with the app instance to ensure that it is still valid
275 _, err := e.App.FindFirstRecordByFilter("demo2", "1=1")
276 if err != nil {
277 t.Fatalf("Failed to perform a db query after tx success: %v", err)
278 }
279
280 return e.Next()
281 })
282
283 collection, err := app.FindCollectionByNameOrId("demo2")
284 if err != nil {
285 t.Fatal(err)
286 }
287
288 record := core.NewRecord(collection)
289
290 record.Set("title", "test_inner_tx")
291
292 if err = app.Save(record); err != nil {
293 t.Fatalf("Create failed: %v", err)
294 }
295
296 expectedHookCalls := map[string]int{
297 "OnRecordCreateExecute": 1,
298 "OnRecordAfterCreateSuccess": 1,
299 }
300 for k, total := range expectedHookCalls {
301 if found, ok := app.EventCalls[k]; !ok || total != found {
302 t.Fatalf("Expected %q %d calls, got %d", k, total, found)
303 }
304 }
305}
306

Callers

nothing calls this directly

Calls 13

CleanupMethod · 0.95
SetMethod · 0.95
NewTestAppFunction · 0.92
NewRecordFunction · 0.92
OnRecordCreateExecuteMethod · 0.65
RunInTransactionMethod · 0.65
NextMethod · 0.65
IsTransactionalMethod · 0.65
SaveMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…