Skip to content

Commit

Permalink
Add tests to validate the fix implemented by @mrheinen
Browse files Browse the repository at this point in the history
  • Loading branch information
VinGarcia committed Nov 6, 2024
1 parent 24b9313 commit f1c6f32
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions internal/structs/structs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package structs

import (
"reflect"
"testing"

tt "github.com/vingarcia/ksql/internal/testtools"
)

func TestGetTagInfo(t *testing.T) {
tests := []struct {
desc string
obj interface{}
expectedInfo StructInfo
expecteErrToContain []string
}{
{
desc: "should correctly parse all the fields tagged `ksql` on a struct",
obj: struct {
ShowStr string `ksql:"show_str"`
HideStr string
ShowInt int `ksql:"show_int"`
HideInt int
}{},
expectedInfo: StructInfo{
IsNestedStruct: false,
byIndex: map[int]*FieldInfo{
0: &FieldInfo{
AttrName: "ShowStr",
ColumnName: "show_str",
Index: 0,
Valid: true,
},
2: &FieldInfo{
AttrName: "ShowInt",
ColumnName: "show_int",
Index: 2,
Valid: true,
},
},
byName: map[string]*FieldInfo{
"show_str": &FieldInfo{
AttrName: "ShowStr",
ColumnName: "show_str",
Index: 0,
Valid: true,
},
"show_int": &FieldInfo{
AttrName: "ShowInt",
ColumnName: "show_int",
Index: 2,
Valid: true,
},
},
},
},
{
desc: "should correctly ignore private fields if they are not tagged",
obj: struct {
ShowStr string `ksql:"show_str"`
hideStr string
ShowInt int `ksql:"show_int"`
hideInt int
}{},
expectedInfo: StructInfo{
IsNestedStruct: false,
byIndex: map[int]*FieldInfo{
0: &FieldInfo{
AttrName: "ShowStr",
ColumnName: "show_str",
Index: 0,
Valid: true,
},
2: &FieldInfo{
AttrName: "ShowInt",
ColumnName: "show_int",
Index: 2,
Valid: true,
},
},
byName: map[string]*FieldInfo{
"show_str": &FieldInfo{
AttrName: "ShowStr",
ColumnName: "show_str",
Index: 0,
Valid: true,
},
"show_int": &FieldInfo{
AttrName: "ShowInt",
ColumnName: "show_int",
Index: 2,
Valid: true,
},
},
},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
info, err := GetTagInfo(reflect.TypeOf(test.obj))
if test.expecteErrToContain != nil {
tt.AssertErrContains(t, err, test.expecteErrToContain...)
t.Skip()
}
tt.AssertNoErr(t, err)

tt.AssertEqual(t, info, test.expectedInfo)
})
}
}

0 comments on commit f1c6f32

Please sign in to comment.