@@ -26,6 +26,19 @@ func getMockData() (data *Data, mock sqlmock.Sqlmock, err error) {
2626 return
2727}
2828
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+
2942func TestGetTablesOk (t * testing.T ) {
3043 data , mock , err := getMockData ()
3144 assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
@@ -134,16 +147,26 @@ func TestCreateTableSQLOk(t *testing.T) {
134147 }
135148}
136149
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+
137164func TestCreateTableRowValues (t * testing.T ) {
138165 data , mock , err := getMockData ()
139166 assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
140167 defer data .Close ()
141168
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" )
147170
148171 table := data .createTable ("test" )
149172
@@ -155,26 +178,22 @@ func TestCreateTableRowValues(t *testing.T) {
155178 // we make sure that all expectations were met
156179 assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
157180
158- assert .
EqualValues (
t ,
"('1' ,'[email protected] ','Test Name 1')" ,
result )
181+ assert .
EqualValues (
t ,
"(1 ,'[email protected] ','Test Name 1')" ,
result )
159182}
160183
161184func TestCreateTableValuesSteam (t * testing.T ) {
162185 data , mock , err := getMockData ()
163186 assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
164187 defer data .Close ()
165188
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" )
171190
172191 data .MaxAllowedPacket = 4096
173192
174193 table := data .createTable ("test" )
175194
176195 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 )
178197
179198 // we make sure that all expectations were met
180199 assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
@@ -185,19 +204,15 @@ func TestCreateTableValuesSteamSmallPackets(t *testing.T) {
185204 assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
186205 defer data .Close ()
187206
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" )
193208
194209 data .MaxAllowedPacket = 64
195210
196211 table := data .createTable ("test" )
197212
198213 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 )
201216
202217 // we make sure that all expectations were met
203218 assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
@@ -208,11 +223,17 @@ func TestCreateTableAllValuesWithNil(t *testing.T) {
208223 assert .NoError (t , err , "an error was not expected when opening a stub database connection" )
209224 defer data .Close ()
210225
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" , "" )).
212232 AddRow (1 , nil , "Test Name 1" ).
213233 AddRow (
2 ,
"[email protected] " ,
"Test Name 2" ).
214234 AddRow (3 , "" , "Test Name 3" )
215235
236+ mock .ExpectQuery ("^SHOW COLUMNS FROM `test`$" ).WillReturnRows (cols )
216237 mock .ExpectQuery ("^SELECT (.+) FROM `test`$" ).WillReturnRows (rows )
217238
218239 table := data .createTable ("test" )
@@ -227,7 +248,7 @@ func TestCreateTableAllValuesWithNil(t *testing.T) {
227248 // we make sure that all expectations were met
228249 assert .NoError (t , mock .ExpectationsWereMet (), "there were unfulfilled expections" )
229250
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')" }
231252
232253 assert .EqualValues (t , expectedResults , results )
233254}
@@ -240,11 +261,17 @@ func TestCreateTableOk(t *testing.T) {
240261 createTableRows := sqlmock .NewRows ([]string {"Table" , "Create Table" }).
241262 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" )
242263
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" , "" )).
244270 AddRow (1 , nil , "Test Name 1" ).
245271 AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
246272
247273 mock .ExpectQuery ("^SHOW CREATE TABLE `Test_Table`$" ).WillReturnRows (createTableRows )
274+ mock .ExpectQuery ("^SHOW COLUMNS FROM `Test_Table`$" ).WillReturnRows (createTableValueCols )
248275 mock .ExpectQuery ("^SELECT (.+) FROM `Test_Table`$" ).WillReturnRows (createTableValueRows )
249276
250277 var buf bytes.Buffer
@@ -277,7 +304,7 @@ CREATE TABLE 'Test_Table' (~id~ int(11) NOT NULL AUTO_INCREMENT,~s~ char(60) DEF
277304
278305LOCK TABLES ~Test_Table~ WRITE;
279306/*!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'); 281308/*!40000 ALTER TABLE ~Test_Table~ ENABLE KEYS */;
282309UNLOCK TABLES;
283310`
@@ -293,11 +320,17 @@ func TestCreateTableOkSmallPackets(t *testing.T) {
293320 createTableRows := sqlmock .NewRows ([]string {"Table" , "Create Table" }).
294321 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" )
295322
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" , "" )).
297329 AddRow (1 , nil , "Test Name 1" ).
298330 AddRow (
2 ,
"[email protected] " ,
"Test Name 2" )
299331
300332 mock .ExpectQuery ("^SHOW CREATE TABLE `Test_Table`$" ).WillReturnRows (createTableRows )
333+ mock .ExpectQuery ("^SHOW COLUMNS FROM `Test_Table`$" ).WillReturnRows (createTableValueCols )
301334 mock .ExpectQuery ("^SELECT (.+) FROM `Test_Table`$" ).WillReturnRows (createTableValueRows )
302335
303336 var buf bytes.Buffer
@@ -330,8 +363,8 @@ CREATE TABLE 'Test_Table' (~id~ int(11) NOT NULL AUTO_INCREMENT,~s~ char(60) DEF
330363
331364LOCK TABLES ~Test_Table~ WRITE;
332365/*!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'); 335368/*!40000 ALTER TABLE ~Test_Table~ ENABLE KEYS */;
336369UNLOCK TABLES;
337370`
0 commit comments