Skip to content

Commit 4b2bf17

Browse files
committed
Add some tests
1 parent 8fc7439 commit 4b2bf17

File tree

1 file changed

+87
-62
lines changed

1 file changed

+87
-62
lines changed

schema_test.go

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -552,68 +552,8 @@ func TestCompareMapToStruct_MissingFieldsTags(t *testing.T) {
552552
}
553553
}
554554

555-
// Tests that Errors returns the expected error map.
556-
func TestCompareResults_Errors(t *testing.T) {
557-
tests := []struct {
558-
srcJson string
559-
expected error
560-
}{
561-
{
562-
srcJson: `{"Foo":null}`,
563-
expected: schema.MismatchError(map[string]interface{}{
564-
"Foo": `expected a string but it's null`,
565-
}),
566-
},
567-
{
568-
srcJson: `{"Foo":1.23,"Bar":true}`,
569-
expected: schema.MismatchError(map[string]interface{}{
570-
"Foo": `expected a string but it's a float64`,
571-
"Bar": `expected an int but it's a bool`,
572-
}),
573-
},
574-
}
575-
576-
for _, test := range tests {
577-
// Unmarshal the json into a map.
578-
src := make(map[string]interface{})
579-
json.Unmarshal([]byte(test.srcJson), &src)
580-
581-
r, _ := schema.CompareMapToStruct(&TestStruct{}, src, nil)
582-
583-
require.Equal(t, test.expected, r.Errors(), test.srcJson)
584-
585-
// Test marshaling the error to JSON.
586-
require.JSONEq(t, toJson(test.expected), toJson(r.Errors()), test.srcJson)
587-
}
588-
}
589-
590-
// Tests that Errors returns nil when there are no type mismatches.
591-
func TestCompareResults_ErrorsReturnsNil(t *testing.T) {
592-
tests := []struct {
593-
srcJson string
594-
}{
595-
{
596-
srcJson: `{}`,
597-
},
598-
{
599-
srcJson: `{"Foo":"hi"}`,
600-
},
601-
{
602-
srcJson: `{"Foo":"hi","Bar":1,"Baz":3.14}`,
603-
},
604-
}
605-
606-
for _, test := range tests {
607-
// Unmarshal the json into a map.
608-
src := make(map[string]interface{})
609-
json.Unmarshal([]byte(test.srcJson), &src)
610-
611-
r, _ := schema.CompareMapToStruct(&TestStruct{}, src, nil)
612-
613-
require.Nil(t, r.Errors())
614-
}
615-
}
616-
555+
// Tests that CompareMapToStruct identifies and returns a list of fields that are in
556+
// dst but not src, and correctly works with nested structs.
617557
func TestCompareMapToStruct_MismatchedFieldsNested(t *testing.T) {
618558
tests := []struct {
619559
srcJson string
@@ -714,3 +654,88 @@ func TestCompareMapToStruct_MissingFieldsNested(t *testing.T) {
714654
require.Empty(t, r.MismatchedFields)
715655
}
716656
}
657+
658+
// Tests that Errors returns the expected error map.
659+
func TestCompareResults_Errors(t *testing.T) {
660+
tests := []struct {
661+
srcJson string
662+
dst interface{}
663+
expected error
664+
}{
665+
{
666+
srcJson: `{"Foo":null}`,
667+
dst: &TestStruct{},
668+
expected: schema.MismatchError(map[string]interface{}{
669+
"Foo": `expected a string but it's null`,
670+
}),
671+
},
672+
{
673+
srcJson: `{"Foo":1.23,"Bar":true}`,
674+
dst: &TestStruct{},
675+
expected: schema.MismatchError(map[string]interface{}{
676+
"Foo": `expected a string but it's a float64`,
677+
"Bar": `expected an int but it's a bool`,
678+
}),
679+
},
680+
{
681+
srcJson: `{"Foo":1.23,"Bar":true,"Butt":"hi"}`,
682+
dst: &TestStructEmbedded{},
683+
expected: schema.MismatchError(map[string]interface{}{
684+
"Foo": `expected a string but it's a float64`,
685+
"Bar": `expected an int but it's a bool`,
686+
"Butt": `expected a bool but it's a string`,
687+
}),
688+
},
689+
{
690+
srcJson: `{"User": {"Foo":"foo", "Bar":13, "Baz":12}, "Cat":{"A":{"Baz":true}, "B":true, "C":"c"}}`,
691+
dst: &TestStructNested{},
692+
expected: schema.MismatchError(map[string]interface{}{
693+
"Cat": map[string]interface{}{
694+
"A": map[string]interface{}{
695+
"Baz": `expected a string but it's a bool`,
696+
},
697+
},
698+
}),
699+
},
700+
}
701+
702+
for _, test := range tests {
703+
// Unmarshal the json into a map.
704+
src := make(map[string]interface{})
705+
json.Unmarshal([]byte(test.srcJson), &src)
706+
707+
r, _ := schema.CompareMapToStruct(test.dst, src, nil)
708+
709+
require.Equal(t, test.expected, r.Errors(), test.srcJson)
710+
711+
// Test marshaling the error to JSON.
712+
require.JSONEq(t, toJson(test.expected), toJson(r.Errors()), test.srcJson)
713+
}
714+
}
715+
716+
// Tests that Errors returns nil when there are no type mismatches.
717+
func TestCompareResults_ErrorsReturnsNil(t *testing.T) {
718+
tests := []struct {
719+
srcJson string
720+
}{
721+
{
722+
srcJson: `{}`,
723+
},
724+
{
725+
srcJson: `{"Foo":"hi"}`,
726+
},
727+
{
728+
srcJson: `{"Foo":"hi","Bar":1,"Baz":3.14}`,
729+
},
730+
}
731+
732+
for _, test := range tests {
733+
// Unmarshal the json into a map.
734+
src := make(map[string]interface{})
735+
json.Unmarshal([]byte(test.srcJson), &src)
736+
737+
r, _ := schema.CompareMapToStruct(&TestStruct{}, src, nil)
738+
739+
require.Nil(t, r.Errors())
740+
}
741+
}

0 commit comments

Comments
 (0)