Skip to content

Commit

Permalink
Merge pull request #56 from JeJutic/issue-19
Browse files Browse the repository at this point in the history
[ISSUE-19] Add assert for json response
  • Loading branch information
siller174 authored Jan 18, 2024
2 parents df2546e + b510e1b commit c94d785
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 8 deletions.
16 changes: 16 additions & 0 deletions asserts/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ func NotEqual(expression string, expect interface{}) cute.AssertBody {
}
}

// EqualJSON is a function to check json path expression value is equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func EqualJSON(expression string, expect []byte) cute.AssertBody {
return func(body []byte) error {
return equalJSON(body, expression, expect)
}
}

// NotEqualJSON is a function to check json path expression value is not equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func NotEqualJSON(expression string, expect []byte) cute.AssertBody {
return func(body []byte) error {
return notEqualJSON(body, expression, expect)
}
}

// Length is a function to asserts that value is the expected length
// About expression - https://goessner.net/articles/JsonPath/
func Length(expression string, expectLength int) cute.AssertBody {
Expand Down
190 changes: 190 additions & 0 deletions asserts/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,196 @@ func TestNotEqual(t *testing.T) {
}
}

func TestEqualJSON(t *testing.T) {
tests := []jsonTest{
{
caseName: "valid json",
data: `{"first": 777, "second": [{"key_1": "some_key", "value": "some_value"}]}`,
expression: "$.second[0].value",
expect: `"some_value"`,
IsNilErr: true,
},
{
caseName: "not valid json",
data: "{not_valid_json}",
},
{
caseName: "3rd party key",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.l",
},
{
caseName: "not array",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.b[bs]",
},
{
caseName: "valid array",
data: `{"arr": ["one","two"]}`,
expression: "$.arr",
expect: `["one", "two"]`,
IsNilErr: true,
},
{
caseName: "check equal map",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.b",
expect: `{"bs": "sb"}`,
IsNilErr: true,
},
{
caseName: "check equal string",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.a",
expect: `"as"`,
IsNilErr: true,
},
{
caseName: "check equal not correct string",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.a",
expect: `"not_correct"`,
},
{
caseName: "check deep equal",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$",
expect: `{ "b": {"bs": "sb"}, "a":"as" }`,
IsNilErr: true,
},
{
caseName: "check deep equal not correct",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$",
expect: `{ "b": {"sb": "bs"}, "a":"as" }`,
},
{
caseName: "check 186135434",
data: `{"a":186135434, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "186135434",
IsNilErr: true,
},
{
caseName: "check float",
data: `{"a":1.0000001, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "1.0000001",
IsNilErr: true,
},
{
caseName: "check float 2",
data: `{"a":999.0000001, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "999.0000001",
IsNilErr: true,
},
}

for _, test := range tests {
exp, _ := test.expect.(string)
err := EqualJSON(test.expression, []byte(exp))([]byte(test.data))

if test.IsNilErr {
require.NoError(t, err, "failed test %v", test.caseName)
} else {
require.Error(t, err, "failed test %v", test.caseName)
}
}
}

func TestNotEqualJSON(t *testing.T) {
tests := []jsonTest{
{
caseName: "valid json",
data: `{"first": 777, "second": [{"key_1": "some_key", "value": "some_value"}]}`,
expression: "$.second[0].value",
expect: `"some_value"`,
},
{
caseName: "not valid json",
data: "{not_valid_json}",
},
{
caseName: "3rd party key",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.l",
},
{
caseName: "not array",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.b[bs]",
},
{
caseName: "valid array",
data: `{"arr": ["one","two"]}`,
expression: "$.arr",
expect: `["one", "two"]`,
},
{
caseName: "check equal map",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.b",
expect: `{"bs": "sb"}`,
},
{
caseName: "check equal string",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.a",
expect: `"as"`,
},
{
caseName: "check equal not correct string",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$.a",
expect: `"not_correct"`,
IsNilErr: true,
},
{
caseName: "check deep equal",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$",
expect: `{ "b": {"bs": "sb"}, "a":"as" }`,
},
{
caseName: "check deep equal not correct",
data: `{"a":"as", "b":{"bs":"sb"}}`,
expression: "$",
expect: `{ "b": {"sb": "bs"}, "a":"as" }`,
IsNilErr: true,
},
{
caseName: "check 186135434",
data: `{"a":186135434, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "186135434",
},
{
caseName: "check float",
data: `{"a":1.0000001, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "1.0000001",
},
{
caseName: "check float 2",
data: `{"a":999.0000001, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: "999.0000001",
},
}

for _, test := range tests {
exp, _ := test.expect.(string)
err := NotEqualJSON(test.expression, []byte(exp))([]byte(test.data))

if test.IsNilErr {
require.NoError(t, err, "failed test %v", test.caseName)
} else {
require.Error(t, err, "failed test %v", test.caseName)
}
}
}

func TestGetValueFromJSON(t *testing.T) {
testCases := []struct {
name string
Expand Down
47 changes: 39 additions & 8 deletions asserts/json/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"strings"

"github.com/ohler55/ojg/oj"
"github.com/ozontech/cute/errors"
)

Expand All @@ -32,40 +33,70 @@ func contains(data []byte, expression string, expect interface{}) error {
return nil
}

// Equal is a function to assert that a jsonpath expression matches the given value
// About expression - https://goessner.net/articles/JsonPath/
func equal(data []byte, expression string, expect interface{}) error {
func equalAbstract(data []byte, expression string, expect interface{}, name string) error {
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

for _, value := range values {
if !objectsAreEqual(value, expect) {
return errors.NewAssertError("Equal", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
return errors.NewAssertError(name, fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
}
}

return nil
}

// NotEqual is a function to check json path expression value is not equal to given value
// About expression - https://goessner.net/articles/JsonPath/
func notEqual(data []byte, expression string, expect interface{}) error {
func notEqualAbstract(data []byte, expression string, expect interface{}, name string) error {
values, err := GetValueFromJSON(data, expression)
if err != nil {
return err
}

for _, value := range values {
if objectsAreEqual(value, expect) {
return errors.NewAssertError("NotEqual", fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
return errors.NewAssertError(name, fmt.Sprintf("on path %v. expect %v, but actual %v", expression, expect, value), value, expect)
}
}

return nil
}

// Equal is a function to assert that a jsonpath expression matches the given value
// About expression - https://goessner.net/articles/JsonPath/
func equal(data []byte, expression string, expect interface{}) error {
return equalAbstract(data, expression, expect, "Equal")
}

// NotEqual is a function to check json path expression value is not equal to given value
// About expression - https://goessner.net/articles/JsonPath/
func notEqual(data []byte, expression string, expect interface{}) error {
return notEqualAbstract(data, expression, expect, "NotEqual")
}

// EqualJSON is a function to check json path expression value is equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func equalJSON(data []byte, expression string, expect []byte) error {
obj, err := oj.Parse(expect)
if err != nil {
return fmt.Errorf("could not parse json in EqualJSON error: '%s'", err)
}

return equalAbstract(data, expression, obj, "EqualJSON")
}

// NotEqualJSON is a function to check json path expression value is not equal to given json
// About expression - https://goessner.net/articles/JsonPath/
func notEqualJSON(data []byte, expression string, expect []byte) error {
obj, err := oj.Parse(expect)
if err != nil {
return fmt.Errorf("could not parse json in NotEqualJSON error: '%s'", err)
}

return notEqualAbstract(data, expression, obj, "NotEqualJSON")
}

// Length is a function to asserts that value is the expected length
// About expression - https://goessner.net/articles/JsonPath/
func length(data []byte, expression string, expectLength int) error {
Expand Down

0 comments on commit c94d785

Please sign in to comment.