summaryrefslogtreecommitdiff
path: root/database_test.go
blob: b412af02637a75345110d1c85f552f29844d8c69 (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
47
48
49
50
51
52
53
54
55
56
package main

import (
	"fmt"
	"os"
	"strings"
	"testing"
)

func TestMigrationNumber(t *testing.T) {
	n := len(migrations)

	entries, err := os.ReadDir(migrationDir)
	if err != nil {
		t.Fatalf("Failed to determine number of migration files: %v", err)
	}
	expectation := len(entries)

	if n != expectation {
		t.Fatalf("Expected %d migration files but got %d.", expectation, n)
	}
}

func TestMigrationContent(t *testing.T) {
	for i, migration := range migrations {
		path := migrationPath(i)

		expectation := fmt.Sprintf("PRAGMA user_version = %d;", i+1)
		if !strings.HasPrefix(migration, expectation) {
			t.Fatalf("Expected '%s' at start of '%s'.", expectation, path)
		}

		n := strings.Count(migration, `user_version`)
		if n != 1 {
			t.Fatalf(
				"'user_version' was mentioned more than once in %s. "+
				"This is likely not intended.",
				path,
			)
		}
	}
}

func TestMigrations(t *testing.T) {
	db, _ := emptyDB(t)

	for i := range migrations {
		path := migrationPath(i)
		t.Logf("Test execution of %s", path)

		err := Migrate(db, i)
		if err != nil {
			t.Fatalf("Failed to execute migration %s: %v", path, err)
		}
	}
}