MCPcopy Index your code
hub / github.com/dnote/dnote / TestDBPathFlag

Function TestDBPathFlag

pkg/cli/main_test.go:513–571  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

511}
512
513func TestDBPathFlag(t *testing.T) {
514 // Helper function to verify database contents
515 verifyDatabase := func(t *testing.T, dbPath, expectedBook, expectedNote string) *database.DB {
516 ok, err := utils.FileExists(dbPath)
517 if err != nil {
518 t.Fatal(errors.Wrapf(err, "checking if custom db exists at %s", dbPath))
519 }
520 if !ok {
521 t.Errorf("custom database was not created at %s", dbPath)
522 }
523
524 db, err := database.Open(dbPath)
525 if err != nil {
526 t.Fatal(errors.Wrapf(err, "opening db at %s", dbPath))
527 }
528
529 var noteCount, bookCount int
530 database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
531 database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), &noteCount)
532
533 assert.Equalf(t, bookCount, 1, fmt.Sprintf("%s book count mismatch", dbPath))
534 assert.Equalf(t, noteCount, 1, fmt.Sprintf("%s note count mismatch", dbPath))
535
536 var book database.Book
537 database.MustScan(t, "getting book", db.QueryRow("SELECT label FROM books"), &book.Label)
538 assert.Equalf(t, book.Label, expectedBook, fmt.Sprintf("%s book label mismatch", dbPath))
539
540 var note database.Note
541 database.MustScan(t, "getting note", db.QueryRow("SELECT body FROM notes"), &note.Body)
542 assert.Equalf(t, note.Body, expectedNote, fmt.Sprintf("%s note body mismatch", dbPath))
543
544 return db
545 }
546
547 // Setup - use two different custom database paths
548 testDir, customOpts := setupTestEnv(t)
549 customDBPath1 := fmt.Sprintf("%s/custom-test1.db", testDir)
550 customDBPath2 := fmt.Sprintf("%s/custom-test2.db", testDir)
551
552 // Execute - add different notes to each database
553 testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath1, "add", "db1-book", "-c", "content in db1")
554 testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath2, "add", "db2-book", "-c", "content in db2")
555
556 // Test both databases
557 db1 := verifyDatabase(t, customDBPath1, "db1-book", "content in db1")
558 defer db1.Close()
559
560 db2 := verifyDatabase(t, customDBPath2, "db2-book", "content in db2")
561 defer db2.Close()
562
563 // Verify that the databases are independent
564 var db1HasDB2Book int
565 db1.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db2-book").Scan(&db1HasDB2Book)
566 assert.Equal(t, db1HasDB2Book, 0, "db1 should not have db2's book")
567
568 var db2HasDB1Book int
569 db2.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db1-book").Scan(&db2HasDB1Book)
570 assert.Equal(t, db2HasDB1Book, 0, "db2 should not have db1's book")

Callers

nothing calls this directly

Calls 9

FileExistsFunction · 0.92
OpenFunction · 0.92
MustScanFunction · 0.92
EqualfFunction · 0.92
RunDnoteCmdFunction · 0.92
EqualFunction · 0.92
setupTestEnvFunction · 0.70
QueryRowMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected