MCPcopy
hub / github.com/pocketbase/pocketbase / TestRecordOriginal

Function TestRecordOriginal

core/record_model_test.go:150–234  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

148}
149
150func TestRecordOriginal(t *testing.T) {
151 t.Parallel()
152
153 app, _ := tests.NewTestApp()
154 defer app.Cleanup()
155
156 record, err := app.FindAuthRecordByEmail("users", "test@example.com")
157 if err != nil {
158 t.Fatal(err)
159 }
160 originalId := record.Id
161 originalName := record.GetString("name")
162
163 extraFieldsCheck := []string{`"email":`, `"custom":`}
164
165 // change the fields
166 record.Id = "changed"
167 record.Set("name", "name_new")
168 record.Set("custom", "test_custom")
169 record.SetExpand(map[string]any{"test": 123})
170 record.IgnoreEmailVisibility(true)
171 record.IgnoreUnchangedFields(true)
172 record.WithCustomData(true)
173 record.Unhide(record.Collection().Fields.FieldNames()...)
174
175 // ensure that the email visibility and the custom data toggles are active
176 raw, err := record.MarshalJSON()
177 if err != nil {
178 t.Fatal(err)
179 }
180 rawStr := string(raw)
181 for _, f := range extraFieldsCheck {
182 if !strings.Contains(rawStr, f) {
183 t.Fatalf("Expected %s in\n%s", f, rawStr)
184 }
185 }
186
187 // check changes
188 if v := record.GetString("name"); v != "name_new" {
189 t.Fatalf("Expected name to be %q, got %q", "name_new", v)
190 }
191 if v := record.GetString("custom"); v != "test_custom" {
192 t.Fatalf("Expected custom to be %q, got %q", "test_custom", v)
193 }
194
195 // check original
196 if v := record.Original().PK(); v != originalId {
197 t.Fatalf("Expected the original PK to be %q, got %q", originalId, v)
198 }
199 if v := record.Original().Id; v != originalId {
200 t.Fatalf("Expected the original id to be %q, got %q", originalId, v)
201 }
202 if v := record.Original().GetString("name"); v != originalName {
203 t.Fatalf("Expected the original name to be %q, got %q", originalName, v)
204 }
205 if v := record.Original().GetString("custom"); v != "" {
206 t.Fatalf("Expected the original custom to be %q, got %q", "", v)
207 }

Callers

nothing calls this directly

Calls 15

CleanupMethod · 0.95
NewTestAppFunction · 0.92
GetStringMethod · 0.80
SetExpandMethod · 0.80
IgnoreEmailVisibilityMethod · 0.80
IgnoreUnchangedFieldsMethod · 0.80
WithCustomDataMethod · 0.80
UnhideMethod · 0.80
FieldNamesMethod · 0.80
CollectionMethod · 0.80
OriginalMethod · 0.80
ExpandMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…