MCPcopy
hub / github.com/pocketbase/pocketbase / TestRecordClone

Function TestRecordClone

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

Source from the content-addressed store, hash-verified

323}
324
325func TestRecordClone(t *testing.T) {
326 t.Parallel()
327
328 app, _ := tests.NewTestApp()
329 defer app.Cleanup()
330
331 record, err := app.FindAuthRecordByEmail("users", "test@example.com")
332 if err != nil {
333 t.Fatal(err)
334 }
335 originalId := record.Id
336
337 extraFieldsCheck := []string{`"email":`, `"custom":`}
338
339 autodateTest := types.NowDateTime()
340
341 // change the fields
342 record.Id = "changed"
343 record.Set("name", "name_new")
344 record.Set("custom", "test_custom")
345 record.SetRaw("created", autodateTest)
346 record.SetExpand(map[string]any{"test": 123})
347 record.IgnoreEmailVisibility(true)
348 record.WithCustomData(true)
349 record.Unhide(record.Collection().Fields.FieldNames()...)
350
351 // ensure that the email visibility and the custom data toggles are active
352 raw, err := record.MarshalJSON()
353 if err != nil {
354 t.Fatal(err)
355 }
356 rawStr := string(raw)
357 for _, f := range extraFieldsCheck {
358 if !strings.Contains(rawStr, f) {
359 t.Fatalf("Expected %s in\n%s", f, rawStr)
360 }
361 }
362
363 // check changes
364 if v := record.GetString("name"); v != "name_new" {
365 t.Fatalf("Expected name to be %q, got %q", "name_new", v)
366 }
367 if v := record.GetDateTime("created").String(); v != autodateTest.String() {
368 t.Fatalf("Expected created to be %q, got %q", autodateTest.String(), v)
369 }
370 if v := record.GetString("custom"); v != "test_custom" {
371 t.Fatalf("Expected custom to be %q, got %q", "test_custom", v)
372 }
373
374 // check clone
375 if v := record.Clone().LastSavedPK(); v != originalId {
376 t.Fatalf("Expected the clone LastSavedPK to be %q, got %q", originalId, v)
377 }
378 if v := record.Clone().PK(); v != record.Id {
379 t.Fatalf("Expected the clone PK to be %q, got %q", record.Id, v)
380 }
381 if v := record.Clone().Id; v != record.Id {
382 t.Fatalf("Expected the clone id to be %q, got %q", record.Id, v)

Callers

nothing calls this directly

Calls 15

CleanupMethod · 0.95
NewTestAppFunction · 0.92
NowDateTimeFunction · 0.92
SetRawMethod · 0.80
SetExpandMethod · 0.80
IgnoreEmailVisibilityMethod · 0.80
WithCustomDataMethod · 0.80
UnhideMethod · 0.80
FieldNamesMethod · 0.80
CollectionMethod · 0.80
GetStringMethod · 0.80
GetDateTimeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…