diff --git a/exec_test.go b/exec_test.go index c6cf630..e9e6192 100644 --- a/exec_test.go +++ b/exec_test.go @@ -3,6 +3,7 @@ package litesql import ( "io" "os" + "path/filepath" "testing" "time" @@ -20,12 +21,12 @@ var testConfiguration = &Configuration{ BusyTimeout: 1000, TransactionLock: "immediate", ForeignKeys: true, - JournalMode: "OFF", + JournalMode: "WAL", CacheSize: -4000, AutoVacuum: "incremental", Synchronous: "normal", MemoryMapSize: 0, - MaxConnectionsOpen: 1, + MaxConnectionsOpen: 4, } type user struct { @@ -37,12 +38,12 @@ type user struct { func testSimple(t *testing.T) *LiteDB { t.Helper() - // give ourselves a short timeout; all in memory + // give ourselves a short timeout ctx, cancel := scope.WithTTL(t.Context(), timeout) t.Cleanup(cancel) // create the underlying sqlite3 database - ldb, oerr := Open(":memory:", testConfiguration) + ldb, oerr := Open(filepath.Join(t.TempDir(), "test.db"), testConfiguration) must.NoError(t, oerr, must.Sprint("unable to open test database")) // open the sample schema diff --git a/hack/simple.sql b/hack/simple.sql index 486c497..f262a18 100644 --- a/hack/simple.sql +++ b/hack/simple.sql @@ -2,7 +2,7 @@ pragma encoding = 'UTF-8'; pragma foreign_keys = ON; pragma temp_store = MEMORY; pragma max_length = 1048576; -- 1 MiB -pragma journal_mode = OFF; +pragma journal_mode = WAL; pragma synchronous = NORMAL; pragma busy_timeout = 1000; pragma cache_size = -4000; -- 4 MB diff --git a/litesql_test.go b/litesql_test.go index ca55a47..01e389e 100644 --- a/litesql_test.go +++ b/litesql_test.go @@ -21,7 +21,7 @@ func TestLiteDB_Pragmas(t *testing.T) { must.Eq(t, "UTF-8", m["encoding"]) must.Eq(t, "1000", m["busy_timeout"]) must.Eq(t, "1", m["foreign_keys"]) - must.Eq(t, "off", m["journal_mode"]) + must.Eq(t, "wal", m["journal_mode"]) must.Eq(t, "-4000", m["cache_size"]) must.Eq(t, "2", m["auto_vacuum"]) must.Eq(t, "1", m["synchronous"]) diff --git a/litesqltest/database.go b/litesqltest/database.go index 14638f3..8800f7a 100644 --- a/litesqltest/database.go +++ b/litesqltest/database.go @@ -1,21 +1,17 @@ -// Package libsqltest enables convenient testing with a real sqlite3 database -// backed by memory for use in unit tests. +// Package litesqltest enables convenient testing with a real sqlite3 database +// for use in unit tests. package litesqltest import ( + "path/filepath" "testing" - "time" "cattlecloud.net/go/litesql" - "cattlecloud.net/go/scope" ) -const timeout = 3 * time.Second - -// TestConfiguration provides PRAGMA settings appropriate for an in-memory -// database used in unit tests. This configuration disables durability features -// (like WAL mode and synchronous=full) to maximize performance for tests that -// do not require crash recovery guarantees. +// TestConfiguration provides PRAGMA settings appropriate for a database used +// in unit tests. This configuration uses a smaller page cache and disables +// memory-mapped I/O to keep resource usage low. // // Do not modify. var TestConfiguration = &litesql.Configuration{ @@ -24,21 +20,21 @@ var TestConfiguration = &litesql.Configuration{ BusyTimeout: 500, TransactionLock: "immediate", ForeignKeys: true, - JournalMode: "OFF", + JournalMode: "WAL", CacheSize: -4000, AutoVacuum: "incremental", Synchronous: "normal", MemoryMapSize: 0, - MaxConnectionsOpen: 1, + MaxConnectionsOpen: 4, } +// Open creates a database in a temporary directory for use in unit tests. +// +// The database is backed by a file, which is removed when the test completes. func Open(t *testing.T) *litesql.LiteDB { t.Helper() - _, cancel := scope.WithTTL(t.Context(), timeout) - t.Cleanup(cancel) - - const filename = ":memory:" + filename := filepath.Join(t.TempDir(), "test.db") ldb, oerr := litesql.Open(filename, TestConfiguration) if oerr != nil { diff --git a/litesqltest/database_test.go b/litesqltest/database_test.go index 559c3f0..42d0cef 100644 --- a/litesqltest/database_test.go +++ b/litesqltest/database_test.go @@ -13,13 +13,14 @@ func Test_Open(t *testing.T) { m, perr := ldb.Pragmas(t.Context()) must.NoError(t, perr) - must.MapLen(t, 8, m) + must.MapLen(t, 9, m) must.Eq(t, "UTF-8", m["encoding"]) must.Eq(t, "500", m["busy_timeout"]) must.Eq(t, "1", m["foreign_keys"]) - must.Eq(t, "off", m["journal_mode"]) + must.Eq(t, "wal", m["journal_mode"]) must.Eq(t, "-4000", m["cache_size"]) must.Eq(t, "2", m["auto_vacuum"]) must.Eq(t, "1", m["synchronous"]) + must.Eq(t, "0", m["mmap_size"]) must.Eq(t, "4096", m["page_size"]) }