From fc7b9901f6bd25aedb92611d0563cfeee7c43cb2 Mon Sep 17 00:00:00 2001 From: Philip O'Toole Date: Mon, 18 Dec 2023 10:28:04 -0500 Subject: [PATCH] Add DB-level unit test of WAL removal on close --- db/db_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/db/db_test.go b/db/db_test.go index 0ecc815d..ae5d5f8c 100644 --- a/db/db_test.go +++ b/db/db_test.go @@ -29,6 +29,35 @@ func Test_OpenNonExistentDatabase(t *testing.T) { } } +func Test_WALRemovedOnClose(t *testing.T) { + path := mustTempPath() + defer os.Remove(path) + db, err := Open(path, false, true) + if err != nil { + t.Fatalf("error opening non-existent database") + } + defer db.Close() + if !db.WALEnabled() { + t.Fatalf("WAL mode not enabled") + } + + _, err = db.ExecuteStringStmt("CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)") + if err != nil { + t.Fatalf("failed to create table: %s", err.Error()) + } + walPath := db.WALPath() + if !fileExists(walPath) { + t.Fatalf("WAL file does not exist after creating a table") + } + if err := db.Close(); err != nil { + t.Fatalf("error closing database: %s", err.Error()) + } + + if fileExists(db.WALPath()) { + t.Fatalf("WAL file not removed after closing the database") + } +} + func Test_RemoveFiles(t *testing.T) { d := t.TempDir() mustCreateClosedFile(fmt.Sprintf("%s/foo", d))