MCPcopy
hub / github.com/pocketbase/pocketbase / TestCollectionModelEventSync

Function TestCollectionModelEventSync

core/collection_model_test.go:1023–1295  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1021}
1022
1023func TestCollectionModelEventSync(t *testing.T) {
1024 t.Parallel()
1025
1026 app, _ := tests.NewTestApp()
1027 defer app.Cleanup()
1028
1029 testCollections := make([]*core.Collection, 4)
1030 for i := 0; i < 4; i++ {
1031 testCollections[i] = core.NewBaseCollection("sync_test_" + strconv.Itoa(i))
1032 if err := app.Save(testCollections[i]); err != nil {
1033 t.Fatal(err)
1034 }
1035 }
1036
1037 createModelEvent := func() *core.ModelEvent {
1038 event := new(core.ModelEvent)
1039 event.App = app
1040 event.Context = context.Background()
1041 event.Type = "test_a"
1042 event.Model = testCollections[0]
1043 return event
1044 }
1045
1046 createModelErrorEvent := func() *core.ModelErrorEvent {
1047 event := new(core.ModelErrorEvent)
1048 event.ModelEvent = *createModelEvent()
1049 event.Error = errors.New("error_a")
1050 return event
1051 }
1052
1053 changeCollectionEventBefore := func(e *core.CollectionEvent) {
1054 e.Type = "test_b"
1055 //nolint:staticcheck
1056 e.Context = context.WithValue(context.Background(), "test", 123)
1057 e.Collection = testCollections[1]
1058 }
1059
1060 modelEventFinalizerChange := func(e *core.ModelEvent) {
1061 e.Type = "test_c"
1062 //nolint:staticcheck
1063 e.Context = context.WithValue(context.Background(), "test", 456)
1064 e.Model = testCollections[2]
1065 }
1066
1067 changeCollectionEventAfter := func(e *core.CollectionEvent) {
1068 e.Type = "test_d"
1069 //nolint:staticcheck
1070 e.Context = context.WithValue(context.Background(), "test", 789)
1071 e.Collection = testCollections[3]
1072 }
1073
1074 expectedBeforeModelEventHandlerChecks := func(t *testing.T, e *core.ModelEvent) {
1075 if e.Type != "test_a" {
1076 t.Fatalf("Expected type %q, got %q", "test_a", e.Type)
1077 }
1078
1079 if v := e.Context.Value("test"); v != nil {
1080 t.Fatalf("Expected context value %v, got %v", nil, v)

Callers

nothing calls this directly

Calls 15

CleanupMethod · 0.95
NewTestAppFunction · 0.92
NewBaseCollectionFunction · 0.92
TriggerMethod · 0.80
SaveMethod · 0.65
PKMethod · 0.65
NextMethod · 0.65
ErrorMethod · 0.65
OnCollectionValidateMethod · 0.65
OnModelValidateMethod · 0.65
OnCollectionCreateMethod · 0.65
OnModelCreateMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…