@@ -26,6 +26,19 @@ func getMockData() (data *Data, mock sqlmock.Sqlmock, err error) {
26
26
return
27
27
}
28
28
29
+ func c (name string , v interface {}) * sqlmock.Column {
30
+ var t string
31
+ switch reflect .ValueOf (v ).Kind () {
32
+ case reflect .String :
33
+ t = "VARCHAR"
34
+ case reflect .Int :
35
+ t = "INT"
36
+ case reflect .Bool :
37
+ t = "BOOL"
38
+ }
39
+ return sqlmock .NewColumn (name ).OfType (t , v ).Nullable (true )
40
+ }
41
+
29
42
func TestGetTablesOk (t * testing.T ) {
30
43
data , mock , err := getMockData ()
31
44
assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
@@ -134,16 +147,26 @@ func TestCreateTableSQLOk(t *testing.T) {
134
147
}
135
148
}
136
149
150
+ func mockTableSelect (mock sqlmock.Sqlmock , name string ) {
151
+ cols := sqlmock .NewRows ([]string {"Field" , "Extra" }).
152
+ AddRow ("id" , "" ).
153
+ AddRow ("email" , "" ).
154
+ AddRow ("name" , "" )
155
+
156
+ rows := sqlmock .NewRowsWithColumnDefinition (c ("id" , 0 ), c ("email" , "" ), c ("name" , "" )).
157
+ AddRow (
1 ,
"[email protected] " ,
"Test Name 1" ).
158
+ AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
159
+
160
+ mock .ExpectQuery ("^SHOW COLUMNS FROM `" + name + "`$" ).WillReturnRows (cols )
161
+ mock .ExpectQuery ("^SELECT (.+) FROM `" + name + "`$" ).WillReturnRows (rows )
162
+ }
163
+
137
164
func TestCreateTableRowValues (t * testing.T ) {
138
165
data , mock , err := getMockData ()
139
166
assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
140
167
defer data .Close ()
141
168
142
- rows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
143
- AddRow (
1 ,
"[email protected] " ,
"Test Name 1" ).
144
- AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
145
-
146
- mock .ExpectQuery ("^SELECT (.+) FROM `test`$" ).WillReturnRows (rows )
169
+ mockTableSelect (mock , "test" )
147
170
148
171
table := data .createTable ("test" )
149
172
@@ -155,26 +178,22 @@ func TestCreateTableRowValues(t *testing.T) {
155
178
// we make sure that all expectations were met
156
179
assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
157
180
158
- assert .
EqualValues (
t ,
"('1' ,'[email protected] ','Test Name 1')" ,
result )
181
+ assert .
EqualValues (
t ,
"(1 ,'[email protected] ','Test Name 1')" ,
result )
159
182
}
160
183
161
184
func TestCreateTableValuesSteam (t * testing.T ) {
162
185
data , mock , err := getMockData ()
163
186
assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
164
187
defer data .Close ()
165
188
166
- rows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
167
- AddRow (
1 ,
"[email protected] " ,
"Test Name 1" ).
168
- AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
169
-
170
- mock .ExpectQuery ("^SELECT (.+) FROM `test`$" ).WillReturnRows (rows )
189
+ mockTableSelect (mock , "test" )
171
190
172
191
data .MaxAllowedPacket = 4096
173
192
174
193
table := data .createTable ("test" )
175
194
176
195
s := table .Stream ()
177
- assert .
EqualValues (
t ,
"INSERT INTO `test` VALUES ('1' ,'[email protected] ','Test Name 1'),('2' ,'[email protected] ','Test Name 2');" ,
<- s )
196
+ assert .
EqualValues (
t ,
"INSERT INTO `test` (`id`, `email`, `name`) VALUES (1 ,'[email protected] ','Test Name 1'),(2 ,'[email protected] ','Test Name 2');" ,
<- s )
178
197
179
198
// we make sure that all expectations were met
180
199
assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
@@ -185,19 +204,15 @@ func TestCreateTableValuesSteamSmallPackets(t *testing.T) {
185
204
assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
186
205
defer data .Close ()
187
206
188
- rows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
189
- AddRow (
1 ,
"[email protected] " ,
"Test Name 1" ).
190
- AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
191
-
192
- mock .ExpectQuery ("^SELECT (.+) FROM `test`$" ).WillReturnRows (rows )
207
+ mockTableSelect (mock , "test" )
193
208
194
209
data .MaxAllowedPacket = 64
195
210
196
211
table := data .createTable ("test" )
197
212
198
213
s := table .Stream ()
199
- assert .
EqualValues (
t ,
"INSERT INTO `test` VALUES ('1' ,'[email protected] ','Test Name 1');" ,
<- s )
200
- assert .
EqualValues (
t ,
"INSERT INTO `test` VALUES ('2' ,'[email protected] ','Test Name 2');" ,
<- s )
214
+ assert .
EqualValues (
t ,
"INSERT INTO `test` (`id`, `email`, `name`) VALUES (1 ,'[email protected] ','Test Name 1');" ,
<- s )
215
+ assert .
EqualValues (
t ,
"INSERT INTO `test` (`id`, `email`, `name`) VALUES (2 ,'[email protected] ','Test Name 2');" ,
<- s )
201
216
202
217
// we make sure that all expectations were met
203
218
assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
@@ -208,11 +223,17 @@ func TestCreateTableAllValuesWithNil(t *testing.T) {
208
223
assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
209
224
defer data .Close ()
210
225
211
- rows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
226
+ cols := sqlmock .NewRows ([]string {"Field" , "Extra" }).
227
+ AddRow ("id" , "" ).
228
+ AddRow ("email" , "" ).
229
+ AddRow ("name" , "" )
230
+
231
+ rows := sqlmock .NewRowsWithColumnDefinition (c ("id" , 0 ), c ("email" , "" ), c ("name" , "" )).
212
232
AddRow (1 , nil , "Test Name 1" ).
213
233
AddRow (
2 ,
"[email protected] " ,
"Test Name 2" ).
214
234
AddRow (3 , "" , "Test Name 3" )
215
235
236
+ mock .ExpectQuery ("^SHOW COLUMNS FROM `test`$" ).WillReturnRows (cols )
216
237
mock .ExpectQuery ("^SELECT (.+) FROM `test`$" ).WillReturnRows (rows )
217
238
218
239
table := data .createTable ("test" )
@@ -227,7 +248,7 @@ func TestCreateTableAllValuesWithNil(t *testing.T) {
227
248
// we make sure that all expectations were met
228
249
assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
229
250
230
- expectedResults := []
string {
"('1' ,NULL,'Test Name 1')" ,
"('2' ,'[email protected] ','Test Name 2')" ,
"('3' ,'','Test Name 3')" }
251
+ expectedResults := []
string {
"(1 ,NULL,'Test Name 1')" ,
"(2 ,'[email protected] ','Test Name 2')" ,
"(3 ,'','Test Name 3')" }
231
252
232
253
assert .EqualValues (t , expectedResults , results )
233
254
}
@@ -240,11 +261,17 @@ func TestCreateTableOk(t *testing.T) {
240
261
createTableRows := sqlmock .NewRows ([]string {"Table" , "Create Table" }).
241
262
AddRow ("Test_Table" , "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`s` char(60) DEFAULT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1" )
242
263
243
- createTableValueRows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
264
+ createTableValueCols := sqlmock .NewRows ([]string {"Field" , "Extra" }).
265
+ AddRow ("id" , "" ).
266
+ AddRow ("email" , "" ).
267
+ AddRow ("name" , "" )
268
+
269
+ createTableValueRows := sqlmock .NewRowsWithColumnDefinition (c ("id" , 0 ), c ("email" , "" ), c ("name" , "" )).
244
270
AddRow (1 , nil , "Test Name 1" ).
245
271
AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
246
272
247
273
mock .ExpectQuery ("^SHOW CREATE TABLE `Test_Table`$" ).WillReturnRows (createTableRows )
274
+ mock .ExpectQuery ("^SHOW COLUMNS FROM `Test_Table`$" ).WillReturnRows (createTableValueCols )
248
275
mock .ExpectQuery ("^SELECT (.+) FROM `Test_Table`$" ).WillReturnRows (createTableValueRows )
249
276
250
277
var buf bytes.Buffer
@@ -277,7 +304,7 @@ CREATE TABLE 'Test_Table' (~id~ int(11) NOT NULL AUTO_INCREMENT,~s~ char(60) DEF
277
304
278
305
LOCK TABLES ~Test_Table~ WRITE;
279
306
/*!40000 ALTER TABLE ~Test_Table~ DISABLE KEYS */;
280
- INSERT INTO ~Test_Table~ VALUES ('1' ,NULL,'Test Name 1'),('2' ,'[email protected] ','Test Name 2');
307
+ INSERT INTO ~Test_Table~ (~id~, ~email~, ~name~) VALUES (1 ,NULL,'Test Name 1'),(2 ,'[email protected] ','Test Name 2');
281
308
/*!40000 ALTER TABLE ~Test_Table~ ENABLE KEYS */;
282
309
UNLOCK TABLES;
283
310
`
@@ -293,11 +320,17 @@ func TestCreateTableOkSmallPackets(t *testing.T) {
293
320
createTableRows := sqlmock .NewRows ([]string {"Table" , "Create Table" }).
294
321
AddRow ("Test_Table" , "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`s` char(60) DEFAULT NULL, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1" )
295
322
296
- createTableValueRows := sqlmock .NewRows ([]string {"id" , "email" , "name" }).
323
+ createTableValueCols := sqlmock .NewRows ([]string {"Field" , "Extra" }).
324
+ AddRow ("id" , "" ).
325
+ AddRow ("email" , "" ).
326
+ AddRow ("name" , "" )
327
+
328
+ createTableValueRows := sqlmock .NewRowsWithColumnDefinition (c ("id" , 0 ), c ("email" , "" ), c ("name" , "" )).
297
329
AddRow (1 , nil , "Test Name 1" ).
298
330
AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
299
331
300
332
mock .ExpectQuery ("^SHOW CREATE TABLE `Test_Table`$" ).WillReturnRows (createTableRows )
333
+ mock .ExpectQuery ("^SHOW COLUMNS FROM `Test_Table`$" ).WillReturnRows (createTableValueCols )
301
334
mock .ExpectQuery ("^SELECT (.+) FROM `Test_Table`$" ).WillReturnRows (createTableValueRows )
302
335
303
336
var buf bytes.Buffer
@@ -330,8 +363,8 @@ CREATE TABLE 'Test_Table' (~id~ int(11) NOT NULL AUTO_INCREMENT,~s~ char(60) DEF
330
363
331
364
LOCK TABLES ~Test_Table~ WRITE;
332
365
/*!40000 ALTER TABLE ~Test_Table~ DISABLE KEYS */;
333
- INSERT INTO ~Test_Table~ VALUES ('1' ,NULL,'Test Name 1');
334
- INSERT INTO ~Test_Table~ VALUES ('2' ,'[email protected] ','Test Name 2');
366
+ INSERT INTO ~Test_Table~ (~id~, ~email~, ~name~) VALUES (1 ,NULL,'Test Name 1');
367
+ INSERT INTO ~Test_Table~ (~id~, ~email~, ~name~) VALUES (2 ,'[email protected] ','Test Name 2');
335
368
/*!40000 ALTER TABLE ~Test_Table~ ENABLE KEYS */;
336
369
UNLOCK TABLES;
337
370
`
0 commit comments