1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084 |
- package sqlx
- import (
- "database/sql"
- "errors"
- "testing"
- "git.i2edu.net/i2/go-zero/core/logx"
- "github.com/DATA-DOG/go-sqlmock"
- "github.com/stretchr/testify/assert"
- )
- func TestUnmarshalRowBool(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value bool
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.True(t, value)
- })
- }
- func TestUnmarshalRowBoolNotSettable(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value bool
- assert.NotNil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(value, rows, true)
- }, "select value from users where user=?", "anyone"))
- })
- }
- func TestUnmarshalRowInt(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value int
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, 2, value)
- })
- }
- func TestUnmarshalRowInt8(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value int8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, int8(3), value)
- })
- }
- func TestUnmarshalRowInt16(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("4")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value int16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.Equal(t, int16(4), value)
- })
- }
- func TestUnmarshalRowInt32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value int32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.Equal(t, int32(5), value)
- })
- }
- func TestUnmarshalRowInt64(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("6")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value int64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, int64(6), value)
- })
- }
- func TestUnmarshalRowUint(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value uint
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, uint(2), value)
- })
- }
- func TestUnmarshalRowUint8(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value uint8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, uint8(3), value)
- })
- }
- func TestUnmarshalRowUint16(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("4")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value uint16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, uint16(4), value)
- })
- }
- func TestUnmarshalRowUint32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value uint32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, uint32(5), value)
- })
- }
- func TestUnmarshalRowUint64(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("6")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value uint64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, uint16(6), value)
- })
- }
- func TestUnmarshalRowFloat32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("7")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value float32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, float32(7), value)
- })
- }
- func TestUnmarshalRowFloat64(t *testing.T) {
- runOrmTest(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)
- var value float64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, float64(8), value)
- })
- }
- func TestUnmarshalRowString(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- const expect = "hello"
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString(expect)
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value string
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowStruct(t *testing.T) {
- value := new(struct {
- Name string
- Age int
- })
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- assert.Equal(t, "liao", value.Name)
- assert.Equal(t, 5, value.Age)
- })
- }
- func TestUnmarshalRowStructWithTags(t *testing.T) {
- value := new(struct {
- Age int `db:"age"`
- Name string `db:"name"`
- })
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("liao,5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- assert.Equal(t, "liao", value.Name)
- assert.Equal(t, 5, value.Age)
- })
- }
- func TestUnmarshalRowStructWithTagsWrongColumns(t *testing.T) {
- value := new(struct {
- Age *int `db:"age"`
- Name string `db:"name"`
- })
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name"}).FromCSVString("liao")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.NotNil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- })
- }
- func TestUnmarshalRowsBool(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []bool{true, false}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1\n0")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []bool
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []int{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []int
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt8(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []int8{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []int8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt16(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []int16{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []int16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []int32{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []int32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt64(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []int64{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []int64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []uint{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []uint
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint8(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []uint8{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []uint8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint16(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []uint16{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []uint16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []uint32{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []uint32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint64(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []uint64{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []uint64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsFloat32(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []float32{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []float32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsFloat64(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []float64{2, 3}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []float64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsString(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []string{"hello", "world"}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("hello\nworld")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []string
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsBoolPtr(t *testing.T) {
- yes := true
- no := false
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*bool{&yes, &no}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("1\n0")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*bool
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsIntPtr(t *testing.T) {
- two := 2
- three := 3
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*int{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*int
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt8Ptr(t *testing.T) {
- two := int8(2)
- three := int8(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*int8{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*int8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt16Ptr(t *testing.T) {
- two := int16(2)
- three := int16(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*int16{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*int16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt32Ptr(t *testing.T) {
- two := int32(2)
- three := int32(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*int32{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*int32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsInt64Ptr(t *testing.T) {
- two := int64(2)
- three := int64(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*int64{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*int64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUintPtr(t *testing.T) {
- two := uint(2)
- three := uint(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*uint{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*uint
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint8Ptr(t *testing.T) {
- two := uint8(2)
- three := uint8(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*uint8{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*uint8
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint16Ptr(t *testing.T) {
- two := uint16(2)
- three := uint16(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*uint16{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*uint16
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint32Ptr(t *testing.T) {
- two := uint32(2)
- three := uint32(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*uint32{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*uint32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsUint64Ptr(t *testing.T) {
- two := uint64(2)
- three := uint64(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*uint64{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*uint64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsFloat32Ptr(t *testing.T) {
- two := float32(2)
- three := float32(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*float32{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*float32
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsFloat64Ptr(t *testing.T) {
- two := float64(2)
- three := float64(3)
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*float64{&two, &three}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("2\n3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*float64
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsStringPtr(t *testing.T) {
- hello := "hello"
- world := "world"
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- expect := []*string{&hello, &world}
- rs := sqlmock.NewRows([]string{"value"}).FromCSVString("hello\nworld")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var value []*string
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select value from users where user=?", "anyone"))
- assert.EqualValues(t, expect, value)
- })
- }
- func TestUnmarshalRowsStruct(t *testing.T) {
- expect := []struct {
- Name string
- Age int64
- }{
- {
- Name: "first",
- Age: 2,
- },
- {
- Name: "second",
- Age: 3,
- },
- }
- var value []struct {
- Name string
- Age int64
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- }
- })
- }
- func TestUnmarshalRowsStructWithNullStringType(t *testing.T) {
- expect := []struct {
- Name string
- NullString sql.NullString
- }{
- {
- Name: "first",
- NullString: sql.NullString{
- String: "firstnullstring",
- Valid: true,
- },
- },
- {
- Name: "second",
- NullString: sql.NullString{
- String: "",
- Valid: false,
- },
- },
- }
- var value []struct {
- Name string `db:"name"`
- NullString sql.NullString `db:"value"`
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "value"}).AddRow(
- "first", "firstnullstring").AddRow("second", nil)
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.NullString.String, value[i].NullString.String)
- assert.Equal(t, each.NullString.Valid, value[i].NullString.Valid)
- }
- })
- }
- func TestUnmarshalRowsStructWithTags(t *testing.T) {
- expect := []struct {
- Name string
- Age int64
- }{
- {
- Name: "first",
- Age: 2,
- },
- {
- Name: "second",
- Age: 3,
- },
- }
- var value []struct {
- Age int64 `db:"age"`
- Name string `db:"name"`
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- }
- })
- }
- func TestUnmarshalRowsStructAndEmbeddedAnonymousStructWithTags(t *testing.T) {
- type Embed struct {
- Value int64 `db:"value"`
- }
- expect := []struct {
- Name string
- Age int64
- Value int64
- }{
- {
- Name: "first",
- Age: 2,
- Value: 3,
- },
- {
- Name: "second",
- Age: 3,
- Value: 4,
- },
- }
- var value []struct {
- Name string `db:"name"`
- Age int64 `db:"age"`
- Embed
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age", "value"}).FromCSVString("first,2,3\nsecond,3,4")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age, value from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- assert.Equal(t, each.Value, value[i].Value)
- }
- })
- }
- func TestUnmarshalRowsStructAndEmbeddedStructPtrAnonymousWithTags(t *testing.T) {
- type Embed struct {
- Value int64 `db:"value"`
- }
- expect := []struct {
- Name string
- Age int64
- Value int64
- }{
- {
- Name: "first",
- Age: 2,
- Value: 3,
- },
- {
- Name: "second",
- Age: 3,
- Value: 4,
- },
- }
- var value []struct {
- Name string `db:"name"`
- Age int64 `db:"age"`
- *Embed
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age", "value"}).FromCSVString("first,2,3\nsecond,3,4")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age, value from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- assert.Equal(t, each.Value, value[i].Value)
- }
- })
- }
- func TestUnmarshalRowsStructPtr(t *testing.T) {
- expect := []*struct {
- Name string
- Age int64
- }{
- {
- Name: "first",
- Age: 2,
- },
- {
- Name: "second",
- Age: 3,
- },
- }
- var value []*struct {
- Name string
- Age int64
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- }
- })
- }
- func TestUnmarshalRowsStructWithTagsPtr(t *testing.T) {
- expect := []*struct {
- Name string
- Age int64
- }{
- {
- Name: "first",
- Age: 2,
- },
- {
- Name: "second",
- Age: 3,
- },
- }
- var value []*struct {
- Age int64 `db:"age"`
- Name string `db:"name"`
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, value[i].Age)
- }
- })
- }
- func TestUnmarshalRowsStructWithTagsPtrWithInnerPtr(t *testing.T) {
- expect := []*struct {
- Name string
- Age int64
- }{
- {
- Name: "first",
- Age: 2,
- },
- {
- Name: "second",
- Age: 3,
- },
- }
- var value []*struct {
- Age *int64 `db:"age"`
- Name string `db:"name"`
- }
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"name", "age"}).FromCSVString("first,2\nsecond,3")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRows(&value, rows, true)
- }, "select name, age from users where user=?", "anyone"))
- for i, each := range expect {
- assert.Equal(t, each.Name, value[i].Name)
- assert.Equal(t, each.Age, *value[i].Age)
- }
- })
- }
- func TestCommonSqlConn_QueryRowOptional(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"age"}).FromCSVString("5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs("anyone").WillReturnRows(rs)
- var r struct {
- User string `db:"user"`
- Age int `db:"age"`
- }
- assert.Nil(t, query(db, func(rows *sql.Rows) error {
- return unmarshalRow(&r, rows, false)
- }, "select age from users where user=?", "anyone"))
- assert.Empty(t, r.User)
- assert.Equal(t, 5, r.Age)
- })
- }
- func TestUnmarshalRowError(t *testing.T) {
- tests := []struct {
- name string
- colErr error
- scanErr error
- err error
- next int
- validate func(err error)
- }{
- {
- name: "with error",
- err: errors.New("foo"),
- validate: func(err error) {
- assert.NotNil(t, err)
- },
- },
- {
- name: "without next",
- validate: func(err error) {
- assert.Equal(t, ErrNotFound, err)
- },
- },
- {
- name: "with error",
- scanErr: errors.New("foo"),
- next: 1,
- validate: func(err error) {
- assert.Equal(t, ErrNotFound, err)
- },
- },
- }
- for _, test := range tests {
- t.Run(test.name, func(t *testing.T) {
- runOrmTest(t, func(db *sql.DB, mock sqlmock.Sqlmock) {
- rs := sqlmock.NewRows([]string{"age"}).FromCSVString("5")
- mock.ExpectQuery("select (.+) from users where user=?").WithArgs(
- "anyone").WillReturnRows(rs)
- var r struct {
- User string `db:"user"`
- Age int `db:"age"`
- }
- test.validate(query(db, func(rows *sql.Rows) error {
- scanner := mockedScanner{
- colErr: test.colErr,
- scanErr: test.scanErr,
- err: test.err,
- }
- return unmarshalRow(&r, &scanner, false)
- }, "select age from users where user=?", "anyone"))
- })
- })
- }
- }
- func runOrmTest(t *testing.T, fn func(db *sql.DB, mock sqlmock.Sqlmock)) {
- logx.Disable()
- db, mock, err := sqlmock.New()
- if err != nil {
- t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
- }
- defer db.Close()
- fn(db, mock)
- if err := mock.ExpectationsWereMet(); err != nil {
- t.Errorf("there were unfulfilled expectations: %s", err)
- }
- }
- type mockedScanner struct {
- colErr error
- scanErr error
- err error
- next int
- }
- func (m *mockedScanner) Columns() ([]string, error) {
- return nil, m.colErr
- }
- func (m *mockedScanner) Err() error {
- return m.err
- }
- func (m *mockedScanner) Next() bool {
- if m.next > 0 {
- m.next--
- return true
- }
- return false
- }
- func (m *mockedScanner) Scan(v ...interface{}) error {
- return m.scanErr
- }
|