Execution additional tests.

This commit is contained in:
go-jet 2019-08-13 17:20:35 +02:00
parent 486e45db5c
commit 13c671fa3f
3 changed files with 68 additions and 28 deletions

View file

@ -1,6 +1,8 @@
package postgres
import (
"fmt"
"github.com/go-jet/jet/execution"
"github.com/go-jet/jet/internal/testutils"
. "github.com/go-jet/jet/postgres"
"github.com/go-jet/jet/tests/.gentestdata/jetdb/dvds/model"
@ -33,9 +35,17 @@ func TestScanToInvalidDestination(t *testing.T) {
testutils.AssertQueryPanicErr(t, query, db, []**struct{}{}, "jet: destination has to be a pointer to slice or pointer to struct")
})
t.Run("map dest", func(t *testing.T) {
testutils.AssertQueryPanicErr(t, query, db, &map[string]string{}, "jet: destination has to be a pointer to slice or pointer to struct")
})
t.Run("map dest", func(t *testing.T) {
testutils.AssertQueryPanicErr(t, query, db, []map[string]string{}, "jet: destination has to be a pointer to slice or pointer to struct")
})
t.Run("map dest", func(t *testing.T) {
testutils.AssertQueryPanicErr(t, query, db, &[]map[string]string{}, "jet: unsupported slice element type")
})
}
func TestScanToValidDestination(t *testing.T) {
@ -45,6 +55,12 @@ func TestScanToValidDestination(t *testing.T) {
assert.NilError(t, err)
})
t.Run("global query function scan", func(t *testing.T) {
queryStr, args := query.Sql()
err := execution.Query(nil, db, queryStr, args, &struct{}{})
assert.NilError(t, err)
})
t.Run("pointer to slice", func(t *testing.T) {
err := query.Query(db, &[]struct{}{})
@ -75,6 +91,8 @@ func TestScanToStruct(t *testing.T) {
SELECT(Inventory.AllColumns).
ORDER_BY(Inventory.InventoryID)
fmt.Println(query.DebugSql())
t.Run("one struct", func(t *testing.T) {
dest := model.Inventory{}
err := query.LIMIT(1).Query(db, &dest)
@ -166,11 +184,19 @@ func TestScanToStruct(t *testing.T) {
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 postgres.Inventory. `)
testutils.AssertQueryPanicErr(t, query, db, &dest, `jet: Scan: unable to scan type int32 into UUID, at 'InventoryID uuid.UUID' of type postgres.Inventory`)
})
t.Run("type mismatch base type", func(t *testing.T) {
type Inventory struct {
InventoryID int32
FilmID bool
}
dest := []Inventory{}
testutils.AssertQueryPanicErr(t, query.OFFSET(10), db, &dest, `jet: can't set int16 to bool`)
})
}
func TestScanToNestedStruct(t *testing.T) {
@ -410,11 +436,18 @@ func TestScanToSlice(t *testing.T) {
})
t.Run("slice type mismatch ", func(t *testing.T) {
t.Run("slice type convertible", func(t *testing.T) {
var dest []int
err := query.Query(db, &dest)
assert.Error(t, err, `jet: can't append int32 to []int slice `)
assert.NilError(t, err)
})
t.Run("slice type mismatch", func(t *testing.T) {
var dest []bool
testutils.AssertQueryPanicErr(t, query, db, &dest, `jet: can't append int32 to []bool slice`)
//assert.Error(t, err, `jet: can't append int32 to []bool slice `)
})
})
@ -655,7 +688,7 @@ func TestScanToSlice(t *testing.T) {
}
}
testutils.AssertQueryPanicErr(t, query, db, &dest, "jet: unsupported slice element type at 'Cities []**struct { *model.City }'.")
testutils.AssertQueryPanicErr(t, query, db, &dest, "jet: unsupported slice element type at 'Cities []**struct { *model.City }'")
})
}