(t *testing.T)
| 247 | } |
| 248 | |
| 249 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…