MCPcopy
hub / github.com/pocketbase/pocketbase / TestAutodateRecordResave

Function TestAutodateRecordResave

core/field_autodate_test.go:353–441  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

351}
352
353func TestAutodateRecordResave(t *testing.T) {
354 app, _ := tests.NewTestApp()
355 defer app.Cleanup()
356
357 collection, err := app.FindCollectionByNameOrId("demo2")
358 if err != nil {
359 t.Fatal(err)
360 }
361
362 record, err := app.FindRecordById(collection, "llvuca81nly1qls")
363 if err != nil {
364 t.Fatal(err)
365 }
366
367 lastUpdated := record.GetDateTime("updated")
368
369 // save with autogenerated date
370 err = app.Save(record)
371 if err != nil {
372 t.Fatal(err)
373 }
374
375 newUpdated := record.GetDateTime("updated")
376 if newUpdated.Equal(lastUpdated) {
377 t.Fatalf("[0] Expected updated to change, got %v", newUpdated)
378 }
379 lastUpdated = newUpdated
380
381 // save with custom date
382 manualUpdated := lastUpdated.Add(-1 * time.Minute)
383 record.SetRaw("updated", manualUpdated)
384 err = app.Save(record)
385 if err != nil {
386 t.Fatal(err)
387 }
388
389 newUpdated = record.GetDateTime("updated")
390 if !newUpdated.Equal(manualUpdated) {
391 t.Fatalf("[1] Expected updated to be the manual set date %v, got %v", manualUpdated, newUpdated)
392 }
393 lastUpdated = newUpdated
394
395 // save again with autogenerated date
396 err = app.Save(record)
397 if err != nil {
398 t.Fatal(err)
399 }
400
401 newUpdated = record.GetDateTime("updated")
402 if newUpdated.Equal(lastUpdated) {
403 t.Fatalf("[2] Expected updated to change, got %v", newUpdated)
404 }
405 lastUpdated = newUpdated
406
407 // simulate save failure
408 app.OnRecordUpdateExecute(collection.Id).Bind(&hook.Handler[*core.RecordEvent]{
409 Id: "test_failure",
410 Func: func(*core.RecordEvent) error {

Callers

nothing calls this directly

Calls 12

CleanupMethod · 0.95
NewTestAppFunction · 0.92
GetDateTimeMethod · 0.80
SetRawMethod · 0.80
FindRecordByIdMethod · 0.65
SaveMethod · 0.65
EqualMethod · 0.65
OnRecordUpdateExecuteMethod · 0.65
AddMethod · 0.45
BindMethod · 0.45
UnbindMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…