-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmigration_test.go
85 lines (78 loc) · 1.94 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
package pgmigrate
import (
"testing"
"github.com/peterldowns/testy/check"
)
func TestIDFromFilename(t *testing.T) {
t.Parallel()
check.Equal(t, "0001_initial", IDFromFilename("0001_initial.sql"))
check.Equal(t, "0001_initial.up", IDFromFilename("0001_initial.up.sql"))
check.Equal(t, "0001_initial", IDFromFilename("0001_initial"))
}
func TestSortByID(t *testing.T) {
t.Parallel()
t.Run("simple example", testcase( //nolint:paralleltest // it is parallel
[]string{
"0002_followup",
"0001_initial",
},
[]string{
"0001_initial",
"0002_followup",
},
))
t.Run("lexicographical ordering", testcase( //nolint:paralleltest // it is parallel
[]string{
"1_one",
"0001_one",
"01_one",
"001_one",
},
[]string{
"0001_one",
"001_one",
"01_one",
"1_one",
},
))
t.Run("more complicated", testcase( //nolint:paralleltest // it is parallel
[]string{
"0001_initial",
"002_garbage",
"03_something",
"0002_followup",
"0003_whatever",
},
[]string{
"0001_initial",
"0002_followup",
"0003_whatever",
"002_garbage",
"03_something",
},
))
}
// testcase builds a test case for SortByID:
// - initial contains the ids of some migrations in their original order.
// - expected contains the ids of the same migrations in their expected sorted order.
//
// the testcase will construct the slice of Migration, sort it, and then check
// to make sure the result is in the expected ID order.
func testcase(initial, expected []string) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
migrations := make([]Migration, 0, len(initial))
for _, id := range initial {
migrations = append(migrations, Migration{ID: id, SQL: "-- not implemented"})
}
SortByID(migrations)
check.Equal(t, expected, getIDs(migrations))
}
}
func getIDs(migrations []Migration) []string {
ids := make([]string, 0, len(migrations))
for _, m := range migrations {
ids = append(ids, m.ID)
}
return ids
}