diff --git a/examples/example_service/example_service_test.go b/examples/example_service/example_service_test.go index 33b2f2c..1cb465c 100644 --- a/examples/example_service/example_service_test.go +++ b/examples/example_service/example_service_test.go @@ -5,7 +5,6 @@ import ( "testing" gomock "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" "github.com/tj/assert" "github.com/vingarcia/ksql" "github.com/vingarcia/ksql/nullable" @@ -198,19 +197,17 @@ func TestStreamAllUsers(t *testing.T) { mockDB.EXPECT().QueryChunks(gomock.Any(), gomock.Any()). DoAndReturn(func(ctx context.Context, parser ksql.ChunkParser) error { - fn, ok := parser.ForEachChunk.(func(users []UserEntity) error) - require.True(t, ok) // Chunk 1: - err := fn([]UserEntity{ + err := ksql.CallFunctionWithRows(parser.ForEachChunk, []map[string]interface{}{ { - ID: 1, - Name: nullable.String("fake name"), - Age: nullable.Int(42), + "id": 1, + "name": "fake name", + "age": 42, }, { - ID: 2, - Name: nullable.String("another fake name"), - Age: nullable.Int(43), + "id": 2, + "name": "another fake name", + "age": 43, }, }) if err != nil { @@ -218,11 +215,11 @@ func TestStreamAllUsers(t *testing.T) { } // Chunk 2: - err = fn([]UserEntity{ + err = ksql.CallFunctionWithRows(parser.ForEachChunk, []map[string]interface{}{ { - ID: 3, - Name: nullable.String("yet another fake name"), - Age: nullable.Int(44), + "id": 3, + "name": "yet another fake name", + "age": 44, }, }) return err diff --git a/structs/structs.go b/structs/structs.go index 3106d62..7690555 100644 --- a/structs/structs.go +++ b/structs/structs.go @@ -262,7 +262,7 @@ func FillSliceWith(entities interface{}, dbRows []map[string]interface{}) error sliceType := sliceRef.Type() if sliceType.Kind() != reflect.Ptr { return fmt.Errorf( - "FillSliceWith: expected input to be a pointer to struct but got %v", + "FillSliceWith: expected input to be a pointer to a slice of structs but got %v", sliceType, ) } diff --git a/test_helpers.go b/test_helpers.go new file mode 100644 index 0000000..65fff68 --- /dev/null +++ b/test_helpers.go @@ -0,0 +1,31 @@ +package ksql + +import ( + "reflect" + + "github.com/vingarcia/ksql/structs" +) + +// CallFunctionWithRows was created for helping test the QueryChunks method +func CallFunctionWithRows(fn interface{}, rows []map[string]interface{}) error { + fnValue := reflect.ValueOf(fn) + chunkType, err := parseInputFunc(fn) + if err != nil { + return err + } + + chunk := reflect.MakeSlice(chunkType, 0, len(rows)) + + // Create a pointer to a slice (required by FillSliceWith) + chunkPtr := reflect.New(chunkType) + chunkPtr.Elem().Set(chunk) + + err = structs.FillSliceWith(chunkPtr.Interface(), rows) + if err != nil { + return err + } + + err, _ = fnValue.Call([]reflect.Value{chunkPtr.Elem()})[0].Interface().(error) + + return err +}