From efb324070827bd1fe713b93f80c601551d246579 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 31 May 2026 21:20:48 +0200 Subject: Add migration execution This adds the execution of the embedded migrations and adds a suitable unit test. --- testing.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 testing.go (limited to 'testing.go') diff --git a/testing.go b/testing.go new file mode 100644 index 0000000..d4a8457 --- /dev/null +++ b/testing.go @@ -0,0 +1,50 @@ +package main + +import ( + "database/sql" + "fmt" + "os" + "path/filepath" + "testing" + + _ "github.com/mattn/go-sqlite3" +) + +func newSite(t *testing.T) string { + site, err := os.MkdirTemp("", "finserv-testing-") + if err != nil { + t.Fatalf("Error creating test site '%s': %v", site, err) + } + + t.Cleanup(func() { + os.RemoveAll(site) + t.Logf("Removed test site '%s'.", site) + }) + + t.Logf("Created test site '%s'.", site) + return site +} + +func emptyDB(t *testing.T) (*sql.DB, string) { + site := newSite(t) + + path := filepath.Join(site, "main.sqlite3") + + db, err := sql.Open("sqlite3", path) + if err != nil { + t.Fatalf("Failed to open SQLite3 database '%s': %v", path, err) + } + + t.Cleanup(func() { + db.Close() + t.Logf("Removed test database '%s'.", path) + }) + + t.Logf("Created test database '%s'.", path) + return db, path +} + +func migrationPath(index int) string { + name := fmt.Sprintf(migrationFilePattern, index+1) + return filepath.Join(migrationDir, name) +} -- cgit v1.3.1