MCPcopy
hub / github.com/pocketbase/pocketbase / TestTransactionFromInnerUpdateHook

Function TestTransactionFromInnerUpdateHook

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

Source from the content-addressed store, hash-verified

305}
306
307func TestTransactionFromInnerUpdateHook(t *testing.T) {
308 t.Parallel()
309
310 app, _ := tests.NewTestApp()
311 defer app.Cleanup()
312
313 app.OnRecordUpdateExecute("demo2").BindFunc(func(e *core.RecordEvent) error {
314 originalApp := e.App
315 return e.App.RunInTransaction(func(txApp core.App) error {
316 e.App = txApp
317 defer func() {
318 e.App = originalApp
319 }()
320
321 nextErr := e.Next()
322
323 return nextErr
324 })
325 })
326
327 app.OnRecordAfterUpdateSuccess("demo2").BindFunc(func(e *core.RecordEvent) error {
328 if e.App.IsTransactional() {
329 t.Fatal("Expected e.App to be non-transactional")
330 }
331
332 // perform a db query with the app instance to ensure that it is still valid
333 _, err := e.App.FindFirstRecordByFilter("demo2", "1=1")
334 if err != nil {
335 t.Fatalf("Failed to perform a db query after tx success: %v", err)
336 }
337
338 return e.Next()
339 })
340
341 existingModel, err := app.FindFirstRecordByFilter("demo2", "1=1")
342 if err != nil {
343 t.Fatal(err)
344 }
345
346 if err = app.Save(existingModel); err != nil {
347 t.Fatalf("Update failed: %v", err)
348 }
349
350 expectedHookCalls := map[string]int{
351 "OnRecordUpdateExecute": 1,
352 "OnRecordAfterUpdateSuccess": 1,
353 }
354 for k, total := range expectedHookCalls {
355 if found, ok := app.EventCalls[k]; !ok || total != found {
356 t.Fatalf("Expected %q %d calls, got %d", k, total, found)
357 }
358 }
359}
360
361func TestTransactionFromInnerDeleteHook(t *testing.T) {
362 t.Parallel()

Callers

nothing calls this directly

Calls 10

CleanupMethod · 0.95
NewTestAppFunction · 0.92
OnRecordUpdateExecuteMethod · 0.65
RunInTransactionMethod · 0.65
NextMethod · 0.65
IsTransactionalMethod · 0.65
SaveMethod · 0.65
BindFuncMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…