-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_test.go
120 lines (109 loc) · 2.65 KB
/
migration_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package migration_test
import (
"context"
"io/fs"
"os"
"path"
"strconv"
"strings"
"testing"
"testing/fstest"
"code.soquee.net/migration"
)
func newMapFS(m map[string]string) fstest.MapFS {
mfs := make(fstest.MapFS)
for k, v := range m {
mfs[k] = &fstest.MapFile{
Data: []byte(v),
}
}
return mfs
}
const migrationsTable = "__migrations"
var genTestCases = [...]struct {
name string
err error
dir string
}{
0: {
name: "test",
err: nil,
dir: "test",
},
1: {
name: "test me'\"\tagain",
err: nil,
dir: "test_me_again",
},
}
func TestLastRun(t *testing.T) {
for i, tc := range genTestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
migrationDir := "001_" + tc.dir
fs := newMapFS(map[string]string{
path.Join(migrationDir, "up.sql"): "-- up.sql",
})
ident, name, err := migration.LastRun(context.Background(), migrationsTable, fs, nil)
if ident != "" {
t.Errorf("Wrong ident: %q", ident)
}
if name != migrationDir {
t.Errorf("Wrong name: want=%q, got=%q", migrationDir, name)
}
if err != nil {
t.Errorf("Unexpected error: %q", err)
}
})
}
}
func TestGenerate(t *testing.T) {
for i, tc := range genTestCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
dir, err := os.MkdirTemp("", "migration_test")
if err != nil {
t.Fatalf("Error creating temp directory for tests: %q", err)
}
defer func() {
err = os.RemoveAll(dir)
if err != nil {
t.Logf("Error cleaning up temp dir %q: %q", dir, err)
}
}()
err = migration.Generator(dir)(tc.name)
if err != tc.err {
t.Errorf("Unexpected error: want=%q, got=%q", tc.err, err)
}
walked := 0
walker, err := migration.NewWalker(context.Background(), migrationsTable, nil)
if err != nil {
t.Fatalf("error creating walker: %q", err)
}
err = walker(os.DirFS(dir), func(name string, info fs.DirEntry, status migration.RunStatus) error {
dirName := info.Name()
if dirName == path.Base(dir) {
t.Fatalf("Walk included top level directory but should only hit migrations")
}
walked++
if walked > 1 {
t.Fatalf("Too many files created in temp dir %q, is cleanup working?", dir)
}
// TODO: test name generation
if status != migration.StatusUnknown {
t.Errorf("Unexpected status: want=%d, got=%d", migration.StatusUnknown, status)
}
idx := strings.Index(dirName, "_")
if idx < 0 {
idx = 0
}
dirName = dirName[idx+1:]
if dirName != tc.dir {
t.Errorf("Unexpected migration dir: want=%q, got=%q", tc.dir, dirName)
}
return nil
})
if err != nil {
t.Errorf("Unexpected error walking test output: %q", err)
}
})
}
}