summaryrefslogtreecommitdiff
path: root/testing.go
blob: 14da69fe7bcf06b185bafab42726337c2af8f778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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)
	})

	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()
	})

	return db, path
}

func migrationPath(index int) string {
	name := fmt.Sprintf(migrationFilePattern, index+1)
	return filepath.Join(migrationDir, name)
}