-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
239 lines (204 loc) · 7.28 KB
/
migrator_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package pms
import (
"fmt"
"regexp"
"sort"
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
func TestNewMigrator(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
}
func TestMigrationUp(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
rows := mock.NewRows([]string{"version"}).AddRow(0)
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(SELECT_VERSION).WillReturnRows(rows)
files := []TestFile{
{true, "1_users.up.sql", []byte("INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]')")},
{false, "1_users.down.sql", []byte("DELETE FROM users WHERE name='Bobby' AND email='[email protected]'")},
{true, "2_users.up.sql", []byte(`INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]'); INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]');`)},
}
mock.ExpectBegin()
f.CreateFiles(files)
f.CreateQueryMocks(files, mock)
mock.ExpectExec(fmt.Sprintf(QUERY_UPDATE_VERSION, TABLE_NAME, 2)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
err = m.Up()
if err != nil {
t.Error(err)
}
}
func TestMigrationDown(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
rows := mock.NewRows([]string{"version"}).AddRow(2)
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(SELECT_VERSION).WillReturnRows(rows)
files := []TestFile{
{false, "1_users.up.sql", []byte("INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]')")},
{true, "1_users.down.sql", []byte("DROP TABLE users")},
{false, "2_users.up.sql", []byte(`INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]'); INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]');`)},
{true, "2_users.down.sql", []byte("DELETE FROM users WHERE name='Bobby' AND email='[email protected]'")},
{false, "3_posts.down.sql", []byte("CREATE TABLE posts(id SERIAL);")},
}
sort.Slice(files, func(i, j int) bool {
return files[i].name > files[j].name
})
mock.ExpectBegin()
f.CreateFiles(files)
f.CreateQueryMocks(files, mock)
mock.ExpectExec(fmt.Sprintf(QUERY_UPDATE_VERSION, TABLE_NAME, 0)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
err = m.Down()
if err != nil {
t.Error(err)
}
}
func TestMigratorVersion(t *testing.T) {
t.Run("increase version", func(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
rows := mock.NewRows([]string{"version"}).AddRow(1)
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(SELECT_VERSION).WillReturnRows(rows)
files := []TestFile{
{false, "1_users.up.sql", []byte("INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]')")},
{false, "1_users.down.sql", []byte("DROP TABLE users")},
{true, "2_users.up.sql", []byte(`INSERT INTO users (name, email) VALUES ('Melony', '[email protected]');`)},
{false, "2_users.down.sql", []byte("DELETE FROM users WHERE name='Bobby' AND email='[email protected]'")},
{true, "3_posts.up.sql", []byte("CREATE TABLE posts(id SERIAL);")},
{true, "4_comments.up.sql", []byte("CREATE TABLE comments(id SERIAL);")},
{false, "5_comments.up.sql", []byte("CREATE TABLE comments(id SERIAL);")},
}
sort.Slice(files, func(i, j int) bool {
return files[i].name < files[j].name
})
mock.ExpectBegin()
f.CreateFiles(files)
f.CreateQueryMocks(files, mock)
mock.ExpectExec(fmt.Sprintf(QUERY_UPDATE_VERSION, TABLE_NAME, 4)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
err = m.Version(4)
if err != nil {
t.Error(err)
}
})
t.Run("decrease version", func(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
rows := mock.NewRows([]string{"version"}).AddRow(2)
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(SELECT_VERSION).WillReturnRows(rows)
files := []TestFile{
{false, "1_users.up.sql", []byte("INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]')")},
{false, "1_users.down.sql", []byte("DROP TABLE users")},
{false, "2_users.up.sql", []byte(`INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]'); INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]');`)},
{true, "2_users.down.sql", []byte("DELETE FROM users WHERE name='Bobby' AND email='[email protected]'")},
{false, "3_posts.up.sql", []byte("CREATE TABLE posts(id SERIAL);")},
}
sort.Slice(files, func(i, j int) bool {
return files[i].name < files[j].name
})
mock.ExpectBegin()
f.CreateFiles(files)
f.CreateQueryMocks(files, mock)
mock.ExpectExec(fmt.Sprintf(QUERY_UPDATE_VERSION, TABLE_NAME, 1)).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
err = m.Version(1)
if err != nil {
t.Error(err)
}
})
t.Run("current version", func(t *testing.T) {
f := FileTester{t: t}
f.MakeTestDir()
defer f.RemoveAll()
db, mock := newSQlMock(t)
defer db.Close()
mock.ExpectPing()
rows := mock.NewRows([]string{"version"}).AddRow(2)
mock.ExpectExec(regexp.QuoteMeta(fmt.Sprintf(QUERY_CREATE_TABLE, TABLE_NAME))).WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(SELECT_VERSION).WillReturnRows(rows)
files := []TestFile{
{false, "1_users.up.sql", []byte("INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]')")},
{false, "1_users.down.sql", []byte("DROP TABLE users")},
{false, "2_users.up.sql", []byte(`INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]'); INSERT INTO users (name, email) VALUES ('Bobby', '[email protected]');`)},
{true, "2_users.down.sql", []byte("DELETE FROM users WHERE name='Bobby' AND email='[email protected]'")},
{false, "3_posts.up.sql", []byte("CREATE TABLE posts(id SERIAL);")},
}
sort.Slice(files, func(i, j int) bool {
return files[i].name < files[j].name
})
f.CreateFiles(files)
m, err := New(db, testDirname)
if err != nil {
t.Error(err)
}
if m == nil {
t.Error("migrator is nil")
}
err = m.Version(2)
if err.Error() != fmt.Sprintf(ERROR_EQUAL_VERSION, 2) {
t.Errorf("not valid error message %q, expected %q", err, fmt.Sprintf(ERROR_EQUAL_VERSION, 2))
}
})
}