MCPcopy
hub / github.com/pocketbase/pocketbase / TestFindCollectionTruncate

Function TestFindCollectionTruncate

core/collection_query_test.go:372–474  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

370}
371
372func TestFindCollectionTruncate(t *testing.T) {
373 t.Parallel()
374
375 app, _ := tests.NewTestApp()
376 defer app.Cleanup()
377
378 countFiles := func(collectionId string) (int, error) {
379 entries, err := os.ReadDir(filepath.Join(app.DataDir(), "storage", collectionId))
380 return len(entries), err
381 }
382
383 t.Run("truncate view", func(t *testing.T) {
384 view2, err := app.FindCollectionByNameOrId("view2")
385 if err != nil {
386 t.Fatal(err)
387 }
388
389 err = app.TruncateCollection(view2)
390 if err == nil {
391 t.Fatalf("Expected truncate to fail because view collections can't be truncated")
392 }
393 })
394
395 t.Run("truncate failure", func(t *testing.T) {
396 demo3, err := app.FindCollectionByNameOrId("demo3")
397 if err != nil {
398 t.Fatal(err)
399 }
400
401 originalTotalRecords, err := app.CountRecords(demo3)
402 if err != nil {
403 t.Fatal(err)
404 }
405
406 originalTotalFiles, err := countFiles(demo3.Id)
407 if err != nil {
408 t.Fatal(err)
409 }
410
411 err = app.TruncateCollection(demo3)
412 if err == nil {
413 t.Fatalf("Expected truncate to fail due to cascade delete failed required constraint")
414 }
415
416 // short delay to ensure that the file delete goroutine has been executed
417 time.Sleep(100 * time.Millisecond)
418
419 totalRecords, err := app.CountRecords(demo3)
420 if err != nil {
421 t.Fatal(err)
422 }
423
424 if totalRecords != originalTotalRecords {
425 t.Fatalf("Expected %d records, got %d", originalTotalRecords, totalRecords)
426 }
427
428 totalFiles, err := countFiles(demo3.Id)
429 if err != nil {

Callers

nothing calls this directly

Calls 7

CleanupMethod · 0.95
NewTestAppFunction · 0.92
DataDirMethod · 0.65
TruncateCollectionMethod · 0.65
CountRecordsMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…