Add more tests for FillStructWith to improve code coverage

pull/16/head
Vinícius Garcia 2022-01-20 19:51:19 -03:00
parent 709d208b8e
commit eded1b5dfd
2 changed files with 48 additions and 1 deletions

View File

@ -44,7 +44,7 @@ func FillStructWith(record interface{}, dbRow map[string]interface{}) error {
if t.Kind() != reflect.Struct {
return fmt.Errorf(
"FillStructWith: expected input kind to be a struct but got %T",
"FillStructWith: expected input to be a pointer to a struct, but got %T",
record,
)
}

View File

@ -228,6 +228,53 @@ func TestFillStructWith(t *testing.T) {
tt.AssertEqual(t, 42, user.Age)
tt.AssertEqual(t, "should be untouched", user.Missing)
})
t.Run("should report error if input is not a pointer", func(t *testing.T) {
type User struct {
Name string `ksql:"name"`
Age int `ksql:"age"`
Missing string `ksql:"missing"`
}
var user User
err := FillStructWith(user, map[string]interface{}{
"name": "fake name",
"age": 42,
"extra_field": "some value",
})
tt.AssertErrContains(t, err, "FillStructWith", "expected input to be a pointer", "User")
})
t.Run("should report error if input is not a pointer to struct", func(t *testing.T) {
type User struct {
Name string `ksql:"name"`
Age int `ksql:"age"`
Missing string `ksql:"missing"`
}
var users []User
err := FillStructWith(&users, map[string]interface{}{
"name": "fake name",
"age": 42,
"extra_field": "some value",
})
tt.AssertErrContains(t, err, "FillStructWith", "expected input to be a pointer to a struct", "User")
})
t.Run("should report error if input and target types are incompatible", func(t *testing.T) {
type User struct {
Age int `ksql:"age"`
}
var user User
err := FillStructWith(&user, map[string]interface{}{
"age": "not compatible with integer type",
})
tt.AssertErrContains(t, err, "FillStructWith", "age", "string", "int")
})
}
func TestFillSliceWith(t *testing.T) {