Add TestCallFunctionWithRows to increase test coverage

pull/16/head
Vinícius Garcia 2022-01-20 19:32:54 -03:00
parent 53c48aecce
commit bfceec04e2
1 changed files with 69 additions and 0 deletions

View File

@ -1,9 +1,11 @@
package kstructs package kstructs
import ( import (
"fmt"
"testing" "testing"
"github.com/ditointernet/go-assert" "github.com/ditointernet/go-assert"
tt "github.com/vingarcia/ksql/internal/testtools"
"github.com/vingarcia/ksql/nullable" "github.com/vingarcia/ksql/nullable"
) )
@ -253,3 +255,70 @@ func TestFillSliceWith(t *testing.T) {
assert.Equal(t, "Breno", users[2].Name) assert.Equal(t, "Breno", users[2].Name)
}) })
} }
func TestCallFunctionWithRows(t *testing.T) {
t.Run("should call the function correctly", func(t *testing.T) {
type User struct {
Name string `ksql:"name"`
Age int `ksql:"age"`
}
var inputUsers []User
fn := func(users []User) error {
inputUsers = users
return nil
}
err := CallFunctionWithRows(fn, []map[string]interface{}{
{
"name": "fake-name1",
"age": 42,
},
{
"name": "fake-name2",
"age": 43,
},
})
tt.AssertNoErr(t, err)
tt.AssertEqual(t, inputUsers, []User{
{
Name: "fake-name1",
Age: 42,
},
{
Name: "fake-name2",
Age: 43,
},
})
})
t.Run("should forward errors correctly", func(t *testing.T) {
type User struct {
Name string `ksql:"name"`
Age int `ksql:"age"`
}
fn := func(users []User) error {
return fmt.Errorf("fake-error-msg")
}
err := CallFunctionWithRows(fn, []map[string]interface{}{{
"name": "fake-name1",
"age": 42,
}})
tt.AssertErrContains(t, err, "fake-error-msg")
})
t.Run("should report error if the input function is invalid", func(t *testing.T) {
type User struct {
Name string `ksql:"name"`
Age int `ksql:"age"`
}
err := CallFunctionWithRows(func() {}, []map[string]interface{}{{
"name": "fake-name1",
"age": 42,
}})
tt.AssertErrContains(t, err)
})
}