Add scan type mismatch tests.

This commit is contained in:
zer0sub 2019-05-22 11:28:32 +02:00
parent 2a9a616e7c
commit 7de8c1c45e
2 changed files with 70 additions and 8 deletions

View file

@ -1,6 +1,8 @@
package tests
import (
"fmt"
"github.com/google/uuid"
. "github.com/sub0zero/go-sqlbuilder/sqlbuilder"
"github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/model"
. "github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/table"
@ -66,7 +68,13 @@ func TestScanToValidDestination(t *testing.T) {
})
t.Run("pointer to slice of strings", func(t *testing.T) {
err := query.Query(db, &[]string{})
err := query.Query(db, &[]int32{})
assert.NilError(t, err)
})
t.Run("pointer to slice of strings", func(t *testing.T) {
err := query.Query(db, &[]*int32{})
assert.NilError(t, err)
})
@ -151,6 +159,36 @@ func TestScanToStruct(t *testing.T) {
assert.Equal(t, *dest.StoreID, int16(1))
})
t.Run("type mismatch base type", func(t *testing.T) {
type Inventory struct {
InventoryID int
FilmID string
}
dest := Inventory{}
err := query.Query(db, &dest)
assert.Error(t, err, `Scan: can't set int32 to int, at struct field: InventoryID int of type tests.Inventory. `)
fmt.Println(err)
})
t.Run("type mismatch scanner type", func(t *testing.T) {
type Inventory struct {
InventoryID uuid.UUID
FilmID string
}
dest := Inventory{}
err := query.Query(db, &dest)
assert.Error(t, err, `Scan: unable to scan type int32 into UUID, at struct field: InventoryID uuid.UUID of type tests.Inventory. `)
fmt.Println(err)
})
}
func TestScanToNestedStruct(t *testing.T) {
@ -387,6 +425,13 @@ func TestScanToSlice(t *testing.T) {
assert.DeepEqual(t, dest, []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
})
t.Run("slice type mismatch ", func(t *testing.T) {
var dest []int
err := query.Query(db, &dest)
assert.Error(t, err, `Scan: can't append int32 to []int slice `)
})
})
t.Run("slice of complex structs", func(t *testing.T) {