diff --git a/README.md b/README.md index 1197a08b..9ceed33c 100644 --- a/README.md +++ b/README.md @@ -1072,7 +1072,7 @@ E.g. the following response would **fail** as `"iShouldNotExist"` is in the actu Check if a field is not equal to a specific value. -This check is available for the types `string`, `number` and `bool`. It implicitly also checks `must_exist` for the value. +This check is available for the types `string`, `number`, `array` and `bool`. This control can be used without the actual key. So only the `:control` key is present. diff --git a/pkg/lib/compare/comparison_functions.go b/pkg/lib/compare/comparison_functions.go index 025de519..45c5a215 100755 --- a/pkg/lib/compare/comparison_functions.go +++ b/pkg/lib/compare/comparison_functions.go @@ -213,7 +213,7 @@ func fillComparisonContext(in util.JsonObject) (out *ComparisonContext, err erro } else { // only allow the not_equal for types string, number, bool switch getJsonType(v) { - case "String", "Number", "Bool": + case "String", "Number", "Bool", "Array": out.notEqual = &v default: err = fmt.Errorf("not_equal has invalid type %s", getJsonType(v)) @@ -633,10 +633,22 @@ func keyChecks(right any, rOK bool, control ComparisonContext) (err error) { if control.notEqual != nil { controlJsonType := getJsonType(*control.notEqual) jsonType := getJsonType(right) - // only compare value if type is equal and a low level json type (string, number, bool) + // only compare value if type is equal and a low level json type (string, number, bool) or array // different type is always not_equal if jsonType == controlJsonType { switch jsonType { + case "Array": + leftMar, err := json.Marshal((*control.notEqual).(util.JsonArray)) + if err != nil { + return fmt.Errorf("could not marshal left: %w", err) + } + rightMar, err := json.Marshal(right.(util.JsonArray)) + if err != nil { + return fmt.Errorf("could not marshal right: %w", err) + } + if string(leftMar) == string(rightMar) { + return fmt.Errorf("is equal to %s %s, should not be equal", jsonType, string(leftMar)) + } case "String": if (*control.notEqual).(util.JsonString) == right.(util.JsonString) { return fmt.Errorf("is equal to %s '%s', should not be equal", jsonType, (*control.notEqual).(util.JsonString)) diff --git a/pkg/lib/compare/comparison_functions_test.go b/pkg/lib/compare/comparison_functions_test.go index 4bce4022..11cb4579 100644 --- a/pkg/lib/compare/comparison_functions_test.go +++ b/pkg/lib/compare/comparison_functions_test.go @@ -891,6 +891,19 @@ func TestComparison(t *testing.T) { eEqual: true, eFailures: nil, }, + { + name: "check control not_equal (different types number, array)", + left: util.JsonObject{ + "v:control": util.JsonObject{ + "not_equal": util.JsonArray([]any{"right"}), + }, + }, + right: util.JsonObject{ + "v": 123.456, + }, + eEqual: true, + eFailures: nil, + }, { name: "check control not_equal (different types string, number)", left: util.JsonObject{ @@ -974,6 +987,32 @@ func TestComparison(t *testing.T) { eEqual: true, eFailures: nil, }, + { + name: "Check not_equal with null and array", + left: util.JsonObject{ + "v:control": util.JsonObject{ + "not_equal": nil, + }, + }, + right: util.JsonObject{ + "v": util.JsonArray([]any{"not null"}), + }, + eEqual: true, + eFailures: nil, + }, + { + name: "Check not_equal with array and null", + left: util.JsonObject{ + "v:control": util.JsonObject{ + "not_equal": util.JsonArray([]any{"not null"}), + }, + }, + right: util.JsonObject{ + "v": nil, + }, + eEqual: true, + eFailures: nil, + }, { name: "Check not_equal: string with different value", left: util.JsonObject{ @@ -1005,6 +1044,37 @@ func TestComparison(t *testing.T) { }, }, }, + { + name: "Check not_equal: array with different value", + left: util.JsonObject{ + "v:control": util.JsonObject{ + "not_equal": util.JsonArray([]any{"left", "right"}), + }, + }, + right: util.JsonObject{ + "v": util.JsonArray([]any{"right", "left"}), + }, + eEqual: true, + eFailures: nil, + }, + { + name: "Check not_equal: array with same value", + left: util.JsonObject{ + "v:control": util.JsonObject{ + "not_equal": util.JsonArray([]any{"left", "right"}), + }, + }, + right: util.JsonObject{ + "v": util.JsonArray([]any{"left", "right"}), + }, + eEqual: false, + eFailures: []CompareFailure{ + { + Key: "v", + Message: `is equal to Array ["left","right"], should not be equal`, + }, + }, + }, { name: "Check not_equal: number with different value", left: util.JsonObject{ diff --git a/test/control/body/not_equal.json b/test/control/body/not_equal.json index a8ff6d80..09b40bd6 100644 --- a/test/control/body/not_equal.json +++ b/test/control/body/not_equal.json @@ -127,31 +127,28 @@ } }, { - "name": "check control not_equal (invalid type object, must fail)", + "name": "check control not_equal (different types array, string)", "request": { "server_url": "http://localhost:9999", "endpoint": "bounce-json", "method": "POST", "body": { - "str": "left" + "array": ["foo"] } }, "response": { "statuscode": 200, "body": { "body": { - "str:control": { - "not_equal": { - "invalid": true - } + "array:control": { + "not_equal": "foo" } } } - }, - "reverse_test_result":true + } }, { - "name": "check control not_equal (invalid type array, must fail)", + "name": "check control not_equal (invalid type object, must fail)", "request": { "server_url": "http://localhost:9999", "endpoint": "bounce-json", @@ -165,11 +162,9 @@ "body": { "body": { "str:control": { - "not_equal": [ - { - "invalid": true - } - ] + "not_equal": { + "invalid": true + } } } } @@ -221,6 +216,27 @@ } } }, + { + "name": "check control not_equal (different types array, null)", + "request": { + "server_url": "http://localhost:9999", + "endpoint": "bounce-json", + "method": "POST", + "body": { + "array": "left" + } + }, + "response": { + "statuscode": 200, + "body": { + "body": { + "array:control": { + "not_equal": null + } + } + } + } + }, { "name": "check control not_equal (different types null, string)", "request": { @@ -285,6 +301,50 @@ } }, + // not_equal, compare with array + { + "name": "check control not_equal (same type array)", + "request": { + "server_url": "http://localhost:9999", + "endpoint": "bounce-json", + "method": "POST", + "body": { + "array": ["right", "left"] + } + }, + "response": { + "statuscode": 200, + "body": { + "body": { + "array:control": { + "not_equal": ["left", "right"] + } + } + } + } + }, + { + "name": "check control not_equal (same type array) (equal value, should fail)", + "request": { + "server_url": "http://localhost:9999", + "endpoint": "bounce-json", + "method": "POST", + "body": { + "array": ["left", "right"] + } + }, + "response": { + "statuscode": 200, + "body": { + "body": { + "array:control": { + "not_equal": ["left", "right"] + } + } + } + }, + "reverse_test_result": true + }, // not_equal, compare with string { "name": "check control not_equal (same type string)", @@ -459,4 +519,4 @@ } } } -] \ No newline at end of file +]