From ecd98f3653a3b1f6852974636ae0604500ae4785 Mon Sep 17 00:00:00 2001 From: Hanggang Z <90540647+hthuz@users.noreply.github.com> Date: Wed, 9 Apr 2025 21:49:00 +0800 Subject: [PATCH] chore: add more orm_test (#4766) --- core/stores/sqlx/orm_test.go | 107 ++++++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 19 deletions(-) diff --git a/core/stores/sqlx/orm_test.go b/core/stores/sqlx/orm_test.go index 264a5ff11..cf07cbfd5 100644 --- a/core/stores/sqlx/orm_test.go +++ b/core/stores/sqlx/orm_test.go @@ -267,6 +267,20 @@ func TestUnmarshalRowStruct(t *testing.T) { }, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination) }) + dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + value := new(struct { + Name string + age int + }) + + rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5") + mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs) + + assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error { + return unmarshalRow(value, rows, true) + }, "select name, age from users where user=?", "anyone"), ErrNotMatchDestination) + }) + dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { rs := sqlmock.NewRows([]string{"value"}).FromCSVString("8") mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs) @@ -310,6 +324,20 @@ func TestUnmarshalRowStructWithTags(t *testing.T) { }, "select name, age from users where user=?", "anyone"), ErrNotReadableValue) }) + dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + value := new(struct { + age int `db:"age"` + Name string `db:"name"` + }) + + rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5") + mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs) + + assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error { + return unmarshalRow(value, rows, true) + }, "select name, age from users where user=?", "anyone"), ErrNotReadableValue) + }) + dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { var value struct { Age *int `db:"age"` @@ -1307,25 +1335,26 @@ func TestAnonymousStructPr(t *testing.T) { } func TestAnonymousStructPrError(t *testing.T) { - type Score struct { - Discipline string `db:"discipline"` - score uint `db:"score"` - } - type ClassType struct { - Grade sql.NullString `db:"grade"` - ClassName *string `db:"class_name"` - } - type Class struct { - *ClassType - Score - } - var value []*struct { - Age int64 `db:"age"` - Class - Name string `db:"name"` - } dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + + type Score struct { + Discipline string `db:"discipline"` + score uint `db:"score"` + } + type ClassType struct { + Grade sql.NullString `db:"grade"` + ClassName *string `db:"class_name"` + } + type Class struct { + *ClassType + Score + } + var value []*struct { + Age int64 `db:"age"` + Class + Name string `db:"name"` + } rs := sqlmock.NewRows([]string{ "name", "age", @@ -1338,14 +1367,54 @@ func TestAnonymousStructPrError(t *testing.T) { AddRow("second", 3, "grade one", "chinese", "class three grade two", 99) mock.ExpectQuery("select (.+) from users where user=?"). WithArgs("anyone").WillReturnRows(rs) - assert.Error(t, query(context.Background(), db, func(rows *sql.Rows) error { + assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error { return unmarshalRows(&value, rows, true) }, "select name, age, grade, discipline, class_name, score from users where user=?", - "anyone")) + "anyone"), ErrNotReadableValue) if len(value) > 0 { assert.Equal(t, value[0].score, 0) } }) + dbtest.RunTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) { + + type Score struct { + Discipline string + score uint + } + type ClassType struct { + Grade sql.NullString + ClassName *string + } + type Class struct { + *ClassType + Score + } + var value []*struct { + Age int64 + Class + Name string + } + rs := sqlmock.NewRows([]string{ + "name", + "age", + "grade", + "discipline", + "class_name", + "score", + }). + AddRow("first", 2, nil, "math", "experimental class", 100). + AddRow("second", 3, "grade one", "chinese", "class three grade two", 99) + mock.ExpectQuery("select (.+) from users where user=?"). + WithArgs("anyone").WillReturnRows(rs) + assert.ErrorIs(t, query(context.Background(), db, func(rows *sql.Rows) error { + return unmarshalRows(&value, rows, true) + }, "select name, age, grade, discipline, class_name, score from users where user=?", + "anyone"), ErrNotMatchDestination) + if len(value) > 0 { + assert.Equal(t, value[0].score, 0) + } + }) + } type mockedScanner struct {