Files
cloud-services/pkg/testhelper/json_compare_test.go

40 lines
1.8 KiB
Go

package testhelper_test
import (
"testing"
th "github.com/fiskerinc/cloud-services/pkg/testhelper"
)
func TestJSONCompare(t *testing.T) {
comparer := th.JSONComparer{
IgnoreProps: []string{"prop3"},
}
defer comparer.Close()
ref := []byte(`{"prop1": 1, "prop2": "x", "prop3": true, "prop4": true, "prop5": {"prop1": 1, "prop2": "x", "prop3": true, "prop4": true}, "prop10": 1, "prop11": "x", "prop12": true, "prop13": true, "prop14": {"prop10": 1, "prop11": "x", "prop12": true, "prop13": true}}`)
comp := []byte(`{"prop1": 2, "prop2": "X", "prop3": false, "prop4": false, "prop5": {"prop1": 3, "prop2": "Y", "prop3": false, "prop4": false}, "prop10": 1, "prop11": "x", "prop12": true, "prop13": true, "prop14": {"prop10": 1, "prop11": "x", "prop12": true, "prop13": true}}`)
diff, err := comparer.GetDiff(ref, comp)
if err != nil {
t.Errorf(th.TestErrorTemplate, "JSONComparer.GetDiff error", nil, err)
return
}
th.MapHasValue(t, "prop1 = 1", diff, "prop1", float64(1))
th.MapHasValue(t, "prop2 = X", diff, "prop2", "x")
th.MapKeyNotExists(t, "prop3 ignored", diff, "prop3")
th.MapHasValue(t, "prop4 = true", diff, "prop4", true)
th.MapHasValue(t, "prop5.prop1 = 1", diff, "prop5.prop1", float64(1))
th.MapHasValue(t, "prop5.prop2 = X", diff, "prop5.prop2", "x")
th.MapHasValue(t, "prop5.prop4 = true", diff, "prop5.prop4", true)
th.MapKeyNotExists(t, "no prop10", diff, "prop10")
th.MapKeyNotExists(t, "no prop11", diff, "prop11")
th.MapKeyNotExists(t, "no prop12", diff, "prop12")
th.MapKeyNotExists(t, "no prop13", diff, "prop13")
th.MapKeyNotExists(t, "no prop14", diff, "prop14")
th.MapKeyNotExists(t, "no prop14.prop10", diff, "prop14.prop10")
th.MapKeyNotExists(t, "no prop14.prop11", diff, "prop14.prop11")
th.MapKeyNotExists(t, "no prop14.prop12", diff, "prop14.prop12")
th.MapKeyNotExists(t, "no prop14.prop13", diff, "prop14.prop13")
}