Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package litesql
import (
"io"
"os"
"path/filepath"
"testing"
"time"

Expand All @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hack/simple.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion litesql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
28 changes: 12 additions & 16 deletions litesqltest/database.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions litesqltest/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}