-
Notifications
You must be signed in to change notification settings - Fork 35
/
migration_test.go
119 lines (108 loc) · 1.89 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
package main
import (
"reflect"
"strings"
"testing"
)
func TestNewMigration(t *testing.T) {
var cases = []struct {
path string
migration *Migration
err bool
}{
{
path: "testdata/0_unknown.sql",
migration: nil,
err: true,
},
{
path: "testdata/1_foo.sql",
migration: &Migration{
Name: "1_foo.sql",
reader: nil,
},
err: false,
},
}
for n, c := range cases {
migration, err := NewMigration(c.path)
if (err != nil) != c.err {
t.Error("case", n, "got unexpected error:", err)
continue
}
if migration != nil {
migration.reader = nil
}
if !reflect.DeepEqual(migration, c.migration) {
t.Error("case", n, "got unexpected output:", migration)
}
}
}
func TestMigrationUp(t *testing.T) {
var cases = []struct {
reader *strings.Reader
output []string
}{
{
reader: strings.NewReader(`-- rambler up
first
-- rambler up
second
-- rambler down
third
-- rambler up
fourth
`),
output: []string{"first", "second", "fourth"},
},
}
for n, c := range cases {
migration := &Migration{
reader: c.reader,
}
statements := migration.Up()
if !reflect.DeepEqual(statements, c.output) {
t.Error("case", n, "got unexpected output:", statements)
}
}
}
func TestMigrationDown(t *testing.T) {
var cases = []struct {
reader *strings.Reader
output []string
}{
{
reader: strings.NewReader(`-- rambler up
first
-- rambler up
second
-- rambler down
third
-- rambler up
fourth
`),
output: []string{"third"},
},
{
reader: strings.NewReader(`-- rambler up
first
-- rambler down
second
-- rambler down
third
-- rambler up
fourth
`),
output: []string{"third", "second"},
},
}
for n, c := range cases {
migration := &Migration{
reader: c.reader,
}
statements := migration.Down()
if !reflect.DeepEqual(statements, c.output) {
t.Error("case", n, "got unexpected output:", statements)
}
}
}