Skip to content

Commit ecd98f3

Browse files
authored
chore: add more orm_test (#4766)
1 parent 6164158 commit ecd98f3

File tree

1 file changed

+88
-19
lines changed

1 file changed

+88
-19
lines changed

core/stores/sqlx/orm_test.go

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,20 @@ func TestUnmarshalRowStruct(t *testing.T) {
267267
}, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination)
268268
})
269269

270+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
271+
value := new(struct {
272+
Name string
273+
age int
274+
})
275+
276+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
277+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
278+
279+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
280+
return unmarshalRow(value, rows, true)
281+
}, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination)
282+
})
283+
270284
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
271285
rs := sqlmock.NewRows([]string{"value"}).FromCSVString("8")
272286
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
@@ -310,6 +324,20 @@ func TestUnmarshalRowStructWithTags(t *testing.T) {
310324
}, "select name, age from users where user=?", "anyone"), ErrNotReadableValue)
311325
})
312326

327+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
328+
value := new(struct {
329+
age int `db:"age"`
330+
Name string `db:"name"`
331+
})
332+
333+
rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
334+
mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
335+
336+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
337+
return unmarshalRow(value, rows, true)
338+
}, "select name, age from users where user=?", "anyone"), ErrNotReadableValue)
339+
})
340+
313341
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
314342
var value struct {
315343
Age *int `db:"age"`
@@ -1307,25 +1335,26 @@ func TestAnonymousStructPr(t *testing.T) {
13071335
}
13081336

13091337
func TestAnonymousStructPrError(t *testing.T) {
1310-
type Score struct {
1311-
Discipline string `db:"discipline"`
1312-
score uint `db:"score"`
1313-
}
1314-
type ClassType struct {
1315-
Grade sql.NullString `db:"grade"`
1316-
ClassName *string `db:"class_name"`
1317-
}
1318-
type Class struct {
1319-
*ClassType
1320-
Score
1321-
}
1322-
var value []*struct {
1323-
Age int64 `db:"age"`
1324-
Class
1325-
Name string `db:"name"`
1326-
}
13271338

13281339
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
1340+
1341+
type Score struct {
1342+
Discipline string `db:"discipline"`
1343+
score uint `db:"score"`
1344+
}
1345+
type ClassType struct {
1346+
Grade sql.NullString `db:"grade"`
1347+
ClassName *string `db:"class_name"`
1348+
}
1349+
type Class struct {
1350+
*ClassType
1351+
Score
1352+
}
1353+
var value []*struct {
1354+
Age int64 `db:"age"`
1355+
Class
1356+
Name string `db:"name"`
1357+
}
13291358
rs := sqlmock.NewRows([]string{
13301359
"name",
13311360
"age",
@@ -1338,14 +1367,54 @@ func TestAnonymousStructPrError(t *testing.T) {
13381367
AddRow("second", 3, "grade one", "chinese", "class three grade two", 99)
13391368
mock.ExpectQuery("select (.+) from users where user=?").
13401369
WithArgs("anyone").WillReturnRows(rs)
1341-
assert.Error(t, query(context.Background(), db, func(rows *sql.Rows) error {
1370+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
13421371
return unmarshalRows(&value, rows, true)
13431372
}, "select name, age, grade, discipline, class_name, score from users where user=?",
1344-
"anyone"))
1373+
"anyone"), ErrNotReadableValue)
13451374
if len(value) > 0 {
13461375
assert.Equal(t, value[0].score, 0)
13471376
}
13481377
})
1378+
dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
1379+
1380+
type Score struct {
1381+
Discipline string
1382+
score uint
1383+
}
1384+
type ClassType struct {
1385+
Grade sql.NullString
1386+
ClassName *string
1387+
}
1388+
type Class struct {
1389+
*ClassType
1390+
Score
1391+
}
1392+
var value []*struct {
1393+
Age int64
1394+
Class
1395+
Name string
1396+
}
1397+
rs := sqlmock.NewRows([]string{
1398+
"name",
1399+
"age",
1400+
"grade",
1401+
"discipline",
1402+
"class_name",
1403+
"score",
1404+
}).
1405+
AddRow("first", 2, nil, "math", "experimental class", 100).
1406+
AddRow("second", 3, "grade one", "chinese", "class three grade two", 99)
1407+
mock.ExpectQuery("select (.+) from users where user=?").
1408+
WithArgs("anyone").WillReturnRows(rs)
1409+
assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error {
1410+
return unmarshalRows(&value, rows, true)
1411+
}, "select name, age, grade, discipline, class_name, score from users where user=?",
1412+
"anyone"), ErrNotMatchDestination)
1413+
if len(value) > 0 {
1414+
assert.Equal(t, value[0].score, 0)
1415+
}
1416+
})
1417+
13491418
}
13501419

13511420
type mockedScanner struct {

0 commit comments

Comments
 (0)