(t *testing.T)
| 370 | } |
| 371 | |
| 372 | func 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 { |
nothing calls this directly
no test coverage detected
searching dependent graphs…