forked from larapulse/migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_test.go
204 lines (157 loc) · 5.29 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
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
package migrator
import (
"database/sql"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
var (
errTestDBExecFailed = errors.New("DB exec command failed")
errTestDBQueryFailed = errors.New("DB query command failed")
errTestDBTransactionFailed = errors.New("DB transaction failed")
errTestLastInsertID = errors.New("Failed to get last insert ID")
errTestAffectedRows = errors.New("Failed to amount of affected rows")
)
type testDummyCommand string
func (c testDummyCommand) ToSQL() string {
return string(c)
}
func testDBConnection(t *testing.T) (db *sql.DB, mock sqlmock.Sqlmock, resetDB func()) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
resetDB = func() {
defer db.Close()
// we make sure that all expectations were met
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
return
}
func TestMigrationExec(t *testing.T) {
t.Run("it executes migration in transaction", func(t *testing.T) {
m := Migration{Transaction: true}
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand("test"),
}
mock.ExpectBegin()
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(commands[1].ToSQL()).WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit()
// now we execute our method
if err := m.exec(db, nil, commands...); err != nil {
t.Errorf("error was not expected while running query: %s", err)
}
})
t.Run("it executes general transaction", func(t *testing.T) {
m := Migration{}
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand("test"),
}
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(commands[1].ToSQL()).WillReturnResult(sqlmock.NewResult(2, 1))
// now we execute our method
if err := m.exec(db, nil, commands...); err != nil {
t.Errorf("error was not expected while running query: %s", err)
}
})
}
func TestRunInTransaction(t *testing.T) {
t.Run("it returns an error if transaction wasn't started", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{}
want := sqlmock.ErrCancelled
mock.ExpectBegin().WillReturnError(want)
// now we execute our method
got := runInTransaction(db, nil, commands...)
assert.Equal(t, want, got)
})
t.Run("it rolled back transaction in case of error", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{testDummyCommand("run")}
want := sqlmock.ErrCancelled
mock.ExpectBegin()
mock.ExpectExec(commands[0].ToSQL()).WillReturnError(want)
mock.ExpectRollback()
// now we execute our method
got := runInTransaction(db, nil, commands...)
assert.Equal(t, want, got)
})
t.Run("it returns an error if committing transaction was unsuccessful", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{}
want := sqlmock.ErrCancelled
mock.ExpectBegin()
mock.ExpectCommit().WillReturnError(want)
// now we execute our method
got := runInTransaction(db, nil, commands...)
assert.Equal(t, want, got)
})
t.Run("it executes all commands", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand("test"),
}
mock.ExpectBegin()
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(commands[1].ToSQL()).WillReturnResult(sqlmock.NewResult(2, 1))
mock.ExpectCommit()
// now we execute our method
if err := runInTransaction(db, nil, commands...); err != nil {
t.Errorf("error was not expected while running query: %s", err)
}
})
}
func TestRun(t *testing.T) {
t.Run("it returns an error on invalid command", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand(""),
}
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
err := run(db, nil, commands...)
assert.Error(t, err)
assert.Equal(t, ErrNoSQLCommandsToRun, err)
})
t.Run("it returns an error on DB command execution", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand("dead"),
}
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(commands[1].ToSQL()).WillReturnError(errTestDBExecFailed)
err := run(db, nil, commands...)
assert.Error(t, err)
assert.Equal(t, errTestDBExecFailed, err)
})
t.Run("it executes all commands", func(t *testing.T) {
db, mock, resetDB := testDBConnection(t)
defer resetDB()
commands := []Command{
testCommand("test"),
testDummyCommand("test"),
}
mock.ExpectExec(commands[0].ToSQL()).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectExec(commands[1].ToSQL()).WillReturnResult(sqlmock.NewResult(2, 1))
err := run(db, nil, commands...)
assert.Nil(t, err)
})
}