MCPcopy
hub / github.com/benbjohnson/litestream / TestDB_EnsureExists

Function TestDB_EnsureExists

db_test.go:1519–1657  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1517}
1518
1519func TestDB_EnsureExists(t *testing.T) {
1520 t.Run("NoReplica", func(t *testing.T) {
1521 db := litestream.NewDB(filepath.Join(t.TempDir(), "db"))
1522 db.Replica = nil
1523 if err := db.EnsureExists(context.Background()); err == nil {
1524 t.Fatal("expected error")
1525 }
1526 })
1527
1528 t.Run("DBAlreadyExists", func(t *testing.T) {
1529 dir := t.TempDir()
1530 dbPath := filepath.Join(dir, "db")
1531
1532 if err := os.WriteFile(dbPath, []byte("dummy"), 0644); err != nil {
1533 t.Fatal(err)
1534 }
1535
1536 db := litestream.NewDB(dbPath)
1537 client := file.NewReplicaClient(filepath.Join(dir, "replica"))
1538 db.Replica = litestream.NewReplicaWithClient(db, client)
1539
1540 if err := db.EnsureExists(context.Background()); err != nil {
1541 t.Fatal(err)
1542 }
1543
1544 data, err := os.ReadFile(dbPath)
1545 if err != nil {
1546 t.Fatal(err)
1547 }
1548 if string(data) != "dummy" {
1549 t.Fatal("expected file to remain unchanged")
1550 }
1551 })
1552
1553 t.Run("NoBackup", func(t *testing.T) {
1554 dir := t.TempDir()
1555 dbPath := filepath.Join(dir, "db")
1556
1557 db := testingutil.NewDB(t, dbPath)
1558 client := file.NewReplicaClient(filepath.Join(dir, "replica"))
1559 db.Replica = litestream.NewReplicaWithClient(db, client)
1560
1561 if err := db.EnsureExists(context.Background()); err != nil {
1562 t.Fatalf("expected nil error for no backup, got %v", err)
1563 }
1564
1565 if _, err := os.Stat(dbPath); !os.IsNotExist(err) {
1566 t.Fatal("expected database file to not exist when no backup available")
1567 }
1568 })
1569
1570 t.Run("MissingParentDir", func(t *testing.T) {
1571 dir := t.TempDir()
1572 dbPath := filepath.Join(dir, "subdir", "nested", "db")
1573
1574 db := testingutil.NewDB(t, dbPath)
1575 client := file.NewReplicaClient(filepath.Join(dir, "replica"))
1576 db.Replica = litestream.NewReplicaWithClient(db, client)

Callers

nothing calls this directly

Calls 10

EnsureExistsMethod · 0.95
OpenMethod · 0.95
SyncAndWaitMethod · 0.95
CloseMethod · 0.95
NewDBFunction · 0.92
NewReplicaClientFunction · 0.92
NewReplicaWithClientFunction · 0.92
NewDBFunction · 0.92
MustOpenSQLDBFunction · 0.92
RunMethod · 0.45

Tested by

no test coverage detected