(t *testing.T)
| 305 | } |
| 306 | |
| 307 | func 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 | |
| 361 | func TestTransactionFromInnerDeleteHook(t *testing.T) { |
| 362 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…