Skip to content

Commit

Permalink
adding not_equals for arrays, removing erroneous statement in README
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hinderberger committed Jul 11, 2024
1 parent 322cf9e commit 82b23d2
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
16 changes: 14 additions & 2 deletions pkg/lib/compare/comparison_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
70 changes: 70 additions & 0 deletions pkg/lib/compare/comparison_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
90 changes: 75 additions & 15 deletions test/control/body/not_equal.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -165,11 +162,9 @@
"body": {
"body": {
"str:control": {
"not_equal": [
{
"invalid": true
}
]
"not_equal": {
"invalid": true
}
}
}
}
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -459,4 +519,4 @@
}
}
}
]
]

0 comments on commit 82b23d2

Please sign in to comment.