Skip to content

Commit

Permalink
Fix JSON comparisons (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
chilland authored Jul 31, 2017
1 parent 01b9ee6 commit 2e12382
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 88 deletions.
70 changes: 46 additions & 24 deletions kazaam_int_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kazaam_test

import (
"encoding/json"
"reflect"
"testing"

"github.com/buger/jsonparser"
Expand All @@ -10,6 +12,22 @@ import (

const testJSONInput = `{"rating":{"example":{"value":3},"primary":{"value":3}}}`

func checkJSONStringsEqual(item1, item2 string) (bool, error) {
var out1, out2 interface{}

err := json.Unmarshal([]byte(item1), &out1)
if err != nil {
return false, nil
}

err = json.Unmarshal([]byte(item2), &out2)
if err != nil {
return false, nil
}

return reflect.DeepEqual(out1, out2), nil
}

func TestKazaamBadInput(t *testing.T) {
jsonOut := ``
spec := `[{"operation": "shift","spec": {"Rating": "rating.primary.value","example.old": "rating.example"}}]`
Expand Down Expand Up @@ -66,18 +84,20 @@ func TestKazaamMultipleTransforms(t *testing.T) {

transform1, _ := kazaam.NewKazaam(spec1)
kazaamOut1, _ := transform1.TransformJSONStringToString(testJSONInput)
areEqual1, _ := checkJSONStringsEqual(kazaamOut1, jsonOut1)

transform2, _ := kazaam.NewKazaam(spec2)
kazaamOut2, _ := transform2.TransformJSONStringToString(testJSONInput)
areEqual2, _ := checkJSONStringsEqual(kazaamOut2, jsonOut2)

if kazaamOut1 != jsonOut1 {
if !areEqual1 {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut1)
t.Log("Actual: ", kazaamOut1)
t.FailNow()
}

if kazaamOut2 != jsonOut2 {
if !areEqual2 {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut2)
t.Log("Actual: ", kazaamOut2)
Expand All @@ -95,13 +115,14 @@ func TestKazaamMultipleTransformsRequire(t *testing.T) {

transform2, _ := kazaam.NewKazaam(spec2)
kazaamOut2, _ := transform2.TransformJSONStringToString(testJSONInput)
areEqual2, _ := checkJSONStringsEqual(kazaamOut2, jsonOut2)

if out1Err == nil {
t.Error("Transform path does not exist in message and should throw an error.")
t.FailNow()
}

if kazaamOut2 != jsonOut2 {
if !areEqual2 {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut2)
t.Log("Actual: ", kazaamOut2)
Expand All @@ -115,8 +136,9 @@ func TestKazaamNoTransform(t *testing.T) {

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(testJSONInput)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -132,16 +154,13 @@ func TestKazaamCoalesceTransformAndShift(t *testing.T) {
"operation": "shift",
"spec": {"rating.foo": "foo", "rating.example.value": "rating.primary.value"}
}]`

// for some reason, keys are inserted in different order on different runs locally and in CI
// so without the alt we get sporadic failures.
jsonOut := `{"rating":{"foo":{"value":3},"example":{"value":3}}}`
altJsonOut := `{"rating":{"example":{"value":3},"foo":{"value":3}}}`

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(testJSONInput)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut && kazaamOut != altJsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -157,17 +176,14 @@ func TestKazaamShiftTransformWithTimestamp(t *testing.T) {
"operation": "timestamp",
"spec": {"newTimestamp":{"inputFormat":"Mon Jan _2 15:04:05 -0700 2006","outputFormat":"2006-01-02T15:04:05-0700"}}
}]`

// for some reason, keys are inserted in different order on different runs locally and in CI
// so without the alt we get sporadic failures.
jsonIn := `{"oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017"}`
jsonOut := `{"oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017","newTimestamp":"2017-07-21T08:15:27+0000"}`
altJsonOut := `{"newTimestamp":"2017-07-21T08:15:27+0000","oldTimestamp":"Fri Jul 21 08:15:27 +0000 2017"}`

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut && kazaamOut != altJsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -191,7 +207,8 @@ func TestShiftWithOverAndWildcard(t *testing.T) {
t.FailNow()
}

if kazaamOut != jsonOut {
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -213,8 +230,9 @@ func TestKazaamTransformMultiOpWithOver(t *testing.T) {

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -229,8 +247,9 @@ func TestShiftWithOver(t *testing.T) {

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down Expand Up @@ -283,20 +302,21 @@ func TestMissingRequiredField(t *testing.T) {
func TestKazaamNoModify(t *testing.T) {
spec := `[{"operation": "shift","spec": {"Rating": "rating.primary.value","example.old": "rating.example"}}]`
msgOut := `{"Rating":3,"example":{"old":{"value":3}}}`
altMsgOut := `{"example":{"old":{"value":3}},"Rating":3}`
tf, _ := kazaam.NewKazaam(spec)
data := []byte(testJSONInput)
jsonOut, _ := tf.Transform(data)

jsonOutStr := string(jsonOut)
areEqual1, _ := checkJSONStringsEqual(string(jsonOut), msgOut)

if !(jsonOutStr == msgOut || jsonOutStr == altMsgOut) || jsonOutStr == testJSONInput {
if !areEqual1 {
t.Error("Unexpected transformation result")
t.Error("Actual:", jsonOutStr)
t.Error("Actual:", string(jsonOut))
t.Error("Expected:", msgOut)
}

if string(data) != testJSONInput {
areEqual2, _ := checkJSONStringsEqual(string(data), testJSONInput)

if !areEqual2 {
t.Error("Unexpected modification")
t.Error("Actual:", string(data))
t.Error("Expected:", testJSONInput)
Expand All @@ -313,7 +333,8 @@ func TestConfigdKazaamGet3rdPartyTransform(t *testing.T) {

k, _ := kazaam.New(`[{"operation": "3rd-party"}]`, kc)
kazaamOut, _ := k.TransformJSONStringToString(`{"test":"data"}`)
if kazaamOut != msgOut {
areEqual, _ := checkJSONStringsEqual(kazaamOut, msgOut)
if !areEqual {
t.Error("Unexpected transform output")
t.Log("Actual: ", kazaamOut)
t.Log("Expected: ", msgOut)
Expand All @@ -339,8 +360,9 @@ func TestKazaamTransformThreeOpWithOver(t *testing.T) {

kazaamTransform, _ := kazaam.NewKazaam(spec)
kazaamOut, _ := kazaamTransform.TransformJSONStringToString(jsonIn)
areEqual, _ := checkJSONStringsEqual(kazaamOut, jsonOut)

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down
9 changes: 6 additions & 3 deletions transform/coalesce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ func TestCoalesce(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -35,8 +36,9 @@ func TestCoalesceWithMulti(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -50,8 +52,9 @@ func TestCoalesceWithNotFound(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Coalesce, cfg, testJSONInput)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down
28 changes: 18 additions & 10 deletions transform/concat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ func TestConcat(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down Expand Up @@ -64,10 +65,11 @@ func TestConcatWithReplaceSimplePath(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Expected: ", []byte(jsonOut))
t.Log("Actual: ", kazaamOut)
t.FailNow()
}
Expand All @@ -80,8 +82,9 @@ func TestConcatWithNoDelimiter(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -96,8 +99,9 @@ func TestConcatWithWildcard(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -112,8 +116,9 @@ func TestConcatWithWildcardNested(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -128,8 +133,9 @@ func TestConcatWithBadPath(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -147,7 +153,7 @@ func TestConcatWithBadSpec(t *testing.T) {
cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)

if kazaamOut != jsonOut {
if string(kazaamOut) != jsonOut {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -162,8 +168,9 @@ func TestConcatWithMultiMulti(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand All @@ -178,8 +185,9 @@ func TestConcatWithLargeNumbers(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Concat, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down
4 changes: 3 additions & 1 deletion transform/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func TestDefault(t *testing.T) {
t.Log("Error: ", err.Error())
t.FailNow()
}
if kazaamOut != jsonOut {

areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down
5 changes: 3 additions & 2 deletions transform/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ func TestExtract(t *testing.T) {

cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Extract, cfg, jsonIn)
areEqual, _ := checkJSONBytesEqual(kazaamOut, []byte(jsonOut))

if kazaamOut != jsonOut {
if !areEqual {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestExtractWithBadPath(t *testing.T) {
cfg := getConfig(spec, false)
kazaamOut, _ := getTransformTestWrapper(Extract, cfg, jsonIn)

if kazaamOut != jsonOut {
if string(kazaamOut) != jsonOut {
t.Error("Transformed data does not match expectation.")
t.Log("Expected: ", jsonOut)
t.Log("Actual: ", kazaamOut)
Expand Down
Loading

0 comments on commit 2e12382

Please sign in to comment.