Add tests to ParseInputFunc and move it to internal/

pull/16/head
Vinícius Garcia 2022-01-10 21:02:39 -03:00
parent 23efe48869
commit 3102a3d4c4
4 changed files with 91 additions and 4 deletions

View File

@ -1,4 +1,4 @@
package kstructs
package structs
import (
"fmt"
@ -7,7 +7,7 @@ import (
var errType = reflect.TypeOf(new(error)).Elem()
// ParseInputFunc is used exclusively for parsing
// parseInputFunc is used exclusively for parsing
// the ForEachChunk function used on the QueryChunks method.
func ParseInputFunc(fn interface{}) (reflect.Type, error) {
if fn == nil {
@ -36,5 +36,9 @@ func ParseInputFunc(fn interface{}) (reflect.Type, error) {
return nil, fmt.Errorf("the argument of the ForEachChunk callback must a slice of structs")
}
if argsType.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("the argument of the ForEachChunk callback must a slice of structs")
}
return argsType, nil
}

View File

@ -0,0 +1,80 @@
package structs_test
import (
"testing"
"github.com/vingarcia/ksql/internal/structs"
tt "github.com/vingarcia/ksql/internal/testtools"
)
type user struct {
ID int `ksql:"id"`
Name string `ksql:"name"`
}
func TestParseInputFunc(t *testing.T) {
t.Run("should parse a function correctly", func(t *testing.T) {
})
t.Run("should return errors correctly", func(t *testing.T) {
tests := []struct {
desc string
fn interface{}
expectErrToContain []string
}{
{
desc: "null input function",
fn: nil,
expectErrToContain: []string{"ForEachChunk", "cannot be nil"},
},
{
desc: "input is not a function",
fn: "not a function",
expectErrToContain: []string{"ForEachChunk", "must be a function"},
},
{
desc: "wrong number of arguments",
fn: func(users []user, foo int) error {
return nil
},
expectErrToContain: []string{"ForEachChunk", "must have 1 argument"},
},
{
desc: "wrong number of return values",
fn: func(users []user) (int, error) {
return 0, nil
},
expectErrToContain: []string{"ForEachChunk", "must have a single return value"},
},
{
desc: "return value is not an error",
fn: func(users []user) int {
return 0
},
expectErrToContain: []string{"ForEachChunk", "must be of type error"},
},
{
desc: "input function argument is not slice",
fn: func(users []string) error {
return nil
},
expectErrToContain: []string{"ForEachChunk", "must a slice"},
},
{
desc: "input function argument is not a slice of structs",
fn: func(users []string) error {
return nil
},
expectErrToContain: []string{"ForEachChunk", "must a slice of structs"},
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
_, err := structs.ParseInputFunc(test.fn)
tt.AssertErrContains(t, err, test.expectErrToContain...)
})
}
})
}

View File

@ -8,6 +8,7 @@ import (
"unicode"
"github.com/pkg/errors"
"github.com/vingarcia/ksql/internal/structs"
"github.com/vingarcia/ksql/kstructs"
)
@ -283,7 +284,7 @@ func (c DB) QueryChunks(
parser ChunkParser,
) error {
fnValue := reflect.ValueOf(parser.ForEachChunk)
chunkType, err := kstructs.ParseInputFunc(parser.ForEachChunk)
chunkType, err := structs.ParseInputFunc(parser.ForEachChunk)
if err != nil {
return err
}

View File

@ -5,6 +5,8 @@ import (
"reflect"
"github.com/pkg/errors"
"github.com/vingarcia/ksql/internal/structs"
)
// FillStructWith is meant to be used on unit tests to mock
@ -107,7 +109,7 @@ func FillSliceWith(entities interface{}, dbRows []map[string]interface{}) error
// 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)
chunkType, err := structs.ParseInputFunc(fn)
if err != nil {
return err
}