@@ -18,6 +18,7 @@ import (
1818 "os/exec"
1919 "testing"
2020
21+ "github.com/DATA-DOG/go-sqlmock"
2122 "github.com/stretchr/testify/require"
2223 "github.com/aarondl/sqlboiler/v4/drivers"
2324)
@@ -142,3 +143,84 @@ func TestDriver(t *testing.T) {
142143 require .False (t , found , "blacklisted column 'string_three' should not be present in table 'magic'" )
143144 })
144145}
146+
147+ // TestTableNames_RowsErr verifies that TableNames propagates errors returned
148+ // by rows.Err() after the row-iteration loop. A RowError on the first row
149+ // simulates a mid-iteration failure (e.g. a dropped connection) and the test
150+ // asserts that the caller receives the underlying error instead of a nil.
151+ func TestTableNames_RowsErr (t * testing.T ) {
152+ db , mock , err := sqlmock .New ()
153+ require .NoError (t , err )
154+ defer db .Close ()
155+
156+ simulatedErr := fmt .Errorf ("simulated row iteration error" )
157+
158+ rows := sqlmock .NewRows ([]string {"table_name" }).
159+ AddRow ("table1" ).
160+ RowError (0 , simulatedErr )
161+
162+ mock .ExpectQuery (`select table_name from information_schema\.tables` ).
163+ WithArgs ("testschema" ).
164+ WillReturnRows (rows )
165+
166+ m := & MySQLDriver {conn : db }
167+ _ , err = m .TableNames ("testschema" , nil , nil )
168+ require .Error (t , err )
169+ require .Contains (t , err .Error (), "simulated row iteration error" )
170+ require .NoError (t , mock .ExpectationsWereMet ())
171+ }
172+
173+ // TestViewNames_RowsErr verifies that ViewNames propagates errors returned
174+ // by rows.Err() after the row-iteration loop. A RowError on the first row
175+ // simulates a mid-iteration failure and the test asserts that the caller
176+ // receives the underlying error instead of a nil.
177+ func TestViewNames_RowsErr (t * testing.T ) {
178+ db , mock , err := sqlmock .New ()
179+ require .NoError (t , err )
180+ defer db .Close ()
181+
182+ simulatedErr := fmt .Errorf ("simulated row iteration error" )
183+
184+ rows := sqlmock .NewRows ([]string {"table_name" }).
185+ AddRow ("view1" ).
186+ RowError (0 , simulatedErr )
187+
188+ mock .ExpectQuery (`select table_name from information_schema\.views` ).
189+ WithArgs ("testschema" ).
190+ WillReturnRows (rows )
191+
192+ m := & MySQLDriver {conn : db }
193+ _ , err = m .ViewNames ("testschema" , nil , nil )
194+ require .Error (t , err )
195+ require .Contains (t , err .Error (), "simulated row iteration error" )
196+ require .NoError (t , mock .ExpectationsWereMet ())
197+ }
198+
199+ // TestColumns_RowsErr verifies that Columns propagates errors returned by
200+ // rows.Err() after the row-iteration loop. A RowError on the first row
201+ // simulates a mid-iteration failure and the test asserts that the caller
202+ // receives the underlying error instead of a nil.
203+ func TestColumns_RowsErr (t * testing.T ) {
204+ db , mock , err := sqlmock .New ()
205+ require .NoError (t , err )
206+ defer db .Close ()
207+
208+ simulatedErr := fmt .Errorf ("simulated row iteration error" )
209+
210+ rows := sqlmock .NewRows ([]string {
211+ "column_name" , "column_type" , "column_comment" , "data_type" ,
212+ "column_default" , "is_nullable" , "is_generated" , "is_unique" ,
213+ }).
214+ AddRow ("id" , "int" , "" , "int" , nil , false , false , true ).
215+ RowError (0 , simulatedErr )
216+
217+ mock .ExpectQuery (`select\s+c\.column_name` ).
218+ WithArgs ("test_table" , "test_table" , "testschema" , "testschema" , "testschema" , "testschema" , "test_table" , "test_table" , "testschema" ).
219+ WillReturnRows (rows )
220+
221+ m := & MySQLDriver {conn : db }
222+ _ , err = m .Columns ("testschema" , "test_table" , nil , nil )
223+ require .Error (t , err )
224+ require .Contains (t , err .Error (), "simulated row iteration error" )
225+ require .NoError (t , mock .ExpectationsWereMet ())
226+ }
0 commit comments