Add scan type mismatch tests.
This commit is contained in:
parent
2a9a616e7c
commit
7de8c1c45e
2 changed files with 70 additions and 8 deletions
|
|
@ -143,7 +143,10 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, slicePtrValue refl
|
||||||
|
|
||||||
if !rowElemPtr.IsNil() {
|
if !rowElemPtr.IsNil() {
|
||||||
updated = true
|
updated = true
|
||||||
appendElemToSlice(slicePtrValue, rowElemPtr)
|
err = appendElemToSlice(slicePtrValue, rowElemPtr)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
@ -178,7 +181,11 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, slicePtrValue refl
|
||||||
|
|
||||||
if updated {
|
if updated {
|
||||||
scanContext.uniqueObjectsMap[groupKey] = slicePtrValue.Elem().Len()
|
scanContext.uniqueObjectsMap[groupKey] = slicePtrValue.Elem().Len()
|
||||||
appendElemToSlice(slicePtrValue, destinationStructPtr)
|
err = appendElemToSlice(slicePtrValue, destinationStructPtr)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,7 +270,7 @@ func getSliceElemPtrAt(slicePtrValue reflect.Value, index int) reflect.Value {
|
||||||
return elem.Addr()
|
return elem.Addr()
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendElemToSlice(slicePtrValue reflect.Value, objPtrValue reflect.Value) {
|
func appendElemToSlice(slicePtrValue reflect.Value, objPtrValue reflect.Value) error {
|
||||||
if slicePtrValue.IsNil() {
|
if slicePtrValue.IsNil() {
|
||||||
panic("Slice is nil")
|
panic("Slice is nil")
|
||||||
}
|
}
|
||||||
|
|
@ -276,9 +283,13 @@ func appendElemToSlice(slicePtrValue reflect.Value, objPtrValue reflect.Value) {
|
||||||
newElemValue = objPtrValue.Elem()
|
newElemValue = objPtrValue.Elem()
|
||||||
}
|
}
|
||||||
|
|
||||||
if newElemValue.Type().AssignableTo(sliceElemType) {
|
if !newElemValue.Type().AssignableTo(sliceElemType) {
|
||||||
sliceValue.Set(reflect.Append(sliceValue, newElemValue))
|
return fmt.Errorf("Scan: can't append %s to %s slice ", newElemValue.Type().String(), sliceValue.Type().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sliceValue.Set(reflect.Append(sliceValue, newElemValue))
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func newElemPtrValueForSlice(slicePtrValue reflect.Value) reflect.Value {
|
func newElemPtrValueForSlice(slicePtrValue reflect.Value) reflect.Value {
|
||||||
|
|
@ -408,6 +419,7 @@ func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue re
|
||||||
err = scanner.Scan(cellValue)
|
err = scanner.Scan(cellValue)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
err = fmt.Errorf("%s, at struct field: %s %s of type %s. ", err.Error(), field.Name, field.Type.String(), structType.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
updated = true
|
updated = true
|
||||||
|
|
@ -419,7 +431,12 @@ func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue re
|
||||||
if cellValue != nil {
|
if cellValue != nil {
|
||||||
updated = true
|
updated = true
|
||||||
initializeValueIfNil(fieldValue)
|
initializeValueIfNil(fieldValue)
|
||||||
setReflectValue(reflect.ValueOf(cellValue), fieldValue)
|
err = setReflectValue(reflect.ValueOf(cellValue), fieldValue)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("Scan: %s, at struct field: %s %s of type %s. ", err.Error(), field.Name, field.Type.String(), structType.String())
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var changed bool
|
var changed bool
|
||||||
|
|
@ -542,7 +559,7 @@ func setReflectValue(source, destination reflect.Value) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sourceElem.Type().AssignableTo(destination.Type()) {
|
if !sourceElem.Type().AssignableTo(destination.Type()) {
|
||||||
return errors.New("Can't set " + sourceElem.Type().String() + " to " + destination.Type().String())
|
return errors.New("can't set " + sourceElem.Type().String() + " to " + destination.Type().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
destination.Set(sourceElem)
|
destination.Set(sourceElem)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
. "github.com/sub0zero/go-sqlbuilder/sqlbuilder"
|
. "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/model"
|
||||||
. "github.com/sub0zero/go-sqlbuilder/tests/.test_files/dvd_rental/dvds/table"
|
. "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) {
|
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)
|
assert.NilError(t, err)
|
||||||
})
|
})
|
||||||
|
|
@ -151,6 +159,36 @@ func TestScanToStruct(t *testing.T) {
|
||||||
assert.Equal(t, *dest.StoreID, int16(1))
|
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) {
|
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})
|
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) {
|
t.Run("slice of complex structs", func(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue