mirror of https://github.com/VinGarcia/ksql.git
Add error test cases for QueryChunk()
parent
6fe450d057
commit
1d4adece95
|
@ -693,6 +693,10 @@ func (c DB) Transaction(ctx context.Context, fn func(ORMProvider) error) error {
|
|||
var errType = reflect.TypeOf(new(error)).Elem()
|
||||
|
||||
func parseInputFunc(fn interface{}) (reflect.Type, error) {
|
||||
if fn == nil {
|
||||
return nil, fmt.Errorf("the ForEachChunk attribute is required and cannot be nil")
|
||||
}
|
||||
|
||||
t := reflect.TypeOf(fn)
|
||||
|
||||
if t.Kind() != reflect.Func {
|
||||
|
|
|
@ -888,6 +888,69 @@ func TestQueryChunks(t *testing.T) {
|
|||
assert.Equal(t, "User3", users[2].Name)
|
||||
assert.Equal(t, []int{2, 1}, lengths)
|
||||
})
|
||||
|
||||
t.Run("should report error if the input function is invalid", func(t *testing.T) {
|
||||
db := connectDB(t, driver)
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
c := newTestDB(db, driver, "users")
|
||||
|
||||
funcs := []interface{}{
|
||||
nil,
|
||||
"not a function",
|
||||
func() error {
|
||||
return nil
|
||||
},
|
||||
func(extraInputValue []User, extra []User) error {
|
||||
return nil
|
||||
},
|
||||
func(invalidArgType string) error {
|
||||
return nil
|
||||
},
|
||||
func(missingReturnType []User) {
|
||||
return
|
||||
},
|
||||
func(users []User) string {
|
||||
return ""
|
||||
},
|
||||
func(extraReturnValue []User) ([]User, error) {
|
||||
return nil, nil
|
||||
},
|
||||
func(notSliceOfStructs []string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
for _, fn := range funcs {
|
||||
err := c.QueryChunks(ctx, ChunkParser{
|
||||
Query: `SELECT * FROM users`,
|
||||
Params: []interface{}{},
|
||||
|
||||
ChunkSize: 2,
|
||||
ForEachChunk: fn,
|
||||
})
|
||||
assert.NotEqual(t, nil, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("should report error if the query is not valid", func(t *testing.T) {
|
||||
db := connectDB(t, driver)
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
c := newTestDB(db, driver, "users")
|
||||
err := c.QueryChunks(ctx, ChunkParser{
|
||||
Query: `SELECT * FROM not a valid query`,
|
||||
Params: []interface{}{},
|
||||
|
||||
ChunkSize: 2,
|
||||
ForEachChunk: func(buffer []User) error {
|
||||
return nil
|
||||
},
|
||||
})
|
||||
assert.NotEqual(t, nil, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue