Skip to content

Commit

Permalink
Merge pull request #48 from ozontech/issue-47
Browse files Browse the repository at this point in the history
Issue 47 - new, new, new!
  • Loading branch information
siller174 authored Nov 20, 2023
2 parents cbfc834 + c1145a1 commit 8b574b1
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LOCAL_BIN:=$(CURDIR)/bin
##################### GOLANG-CI RELATED CHECKS #####################
# Check global GOLANGCI-LINT
GOLANGCI_BIN:=$(LOCAL_BIN)/golangci-lint
GOLANGCI_TAG:=1.42.1
GOLANGCI_TAG:=1.54.2

# Check local bin version
ifneq ($(wildcard $(GOLANGCI_BIN)),)
Expand Down
12 changes: 12 additions & 0 deletions allure.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func (it *cute) setLinksAllure(t linksAllureProvider) {
if it.allureLinks.link != nil {

Check failure on line 17 in allure.go

View workflow job for this annotation

GitHub Actions / golangci-lint

if statements should only be cuddled with assignments (wsl)
t.Link(it.allureLinks.link)
}
if it.allureLinks.tmsLink != "" {
t.TmsLink(it.allureLinks.tmsLink)
}
if len(it.allureLinks.tmsLinks) > 0 {
t.TmsLinks(it.allureLinks.tmsLinks...)
}
}

func (it *cute) setLabelsAllure(t labelsAllureProvider) {
Expand Down Expand Up @@ -65,6 +71,9 @@ func (it *cute) setLabelsAllure(t labelsAllureProvider) {
if len(it.allureLabels.tags) != 0 {
t.Tags(it.allureLabels.tags...)
}
if it.allureLabels.layer != "" {
t.Layer(it.allureLabels.layer)
}
}

func (it *cute) setInfoAllure(t infoAllureProvider) {
Expand All @@ -74,4 +83,7 @@ func (it *cute) setInfoAllure(t infoAllureProvider) {
if it.allureInfo.description != "" {
t.Description(it.allureInfo.description)
}
if it.allureInfo.stage != "" {
t.Stage(it.allureInfo.stage)
}
}
54 changes: 44 additions & 10 deletions asserts/json/json.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
package json

import (
"encoding/json"
"fmt"

"github.com/PaesslerAG/jsonpath"
jd "github.com/josephburnett/jd/lib"
"github.com/ohler55/ojg/jp"
"github.com/ohler55/ojg/oj"
"github.com/ozontech/cute"
cuteErrors "github.com/ozontech/cute/errors"
)

// Diff is a function to compare two jsons
func Diff(original string) cute.AssertBody {
return func(body []byte) error {
originalJSON, err := jd.ReadJsonString(original)
if err != nil {
return fmt.Errorf("could not parse original json in Diff error: '%s'", err)
}

bodyJSON, err := jd.ReadJsonString(string(body))
if err != nil {
return fmt.Errorf("could not parse body json in Diff error: '%s'", err)
}

diff := originalJSON.Diff(bodyJSON).Render()
if diff != "" {
cErr := cuteErrors.NewEmptyAssertError("JSON Diff", "JSON is not the same")
cErr.PutAttachment(&cuteErrors.Attachment{
Name: "JSON diff",
MimeType: "text/plain",
Content: []byte(diff),
})

return cErr
}

return nil
}
}

// Contains is a function to assert that a jsonpath expression extracts a value in an array
// About expression - https://goessner.net/articles/JsonPath/
func Contains(expression string, expect interface{}) cute.AssertBody {
Expand Down Expand Up @@ -99,19 +130,22 @@ func NotPresent(expression string) cute.AssertBody {
}

// GetValueFromJSON is function for get value from json
// TODO create tests
func GetValueFromJSON(js []byte, expression string) (interface{}, error) {
v := interface{}(nil)

err := json.Unmarshal(js, &v)
obj, err := oj.Parse(js)
if err != nil {
return nil, err
return nil, fmt.Errorf("could not parse json in GetValueFromJSON error: '%s'", err)
}

value, err := jsonpath.Get(expression, v)
jsonPath, err := jp.ParseString(expression)
if err != nil {
return nil, fmt.Errorf("evaluating '%s' resulted in error: '%s'", expression, err)
return nil, fmt.Errorf("could not parse path in GetValueFromJSON error: '%s'", err)
}

res := jsonPath.Get(obj)

if len(res) == 0 {
return nil, fmt.Errorf("could not find element by path %v in JSON", expression)
}

return value, nil
return res[0], nil
}
127 changes: 124 additions & 3 deletions asserts/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package json
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -14,6 +15,61 @@ type jsonTest struct {
IsNilErr bool
}

func TestDiff(t *testing.T) {
testCases := []struct {
name string
originalJSON string
bodyJSON string
expectedError string
}{
{
name: "SameJSON",
originalJSON: `{"key1": "value1", "key2": "value2"}`,
bodyJSON: `{"key1": "value1", "key2": "value2"}`,
expectedError: "", // No error expected, JSONs are the same
},
{
name: "DifferentValueJSON",
originalJSON: `{"key1": "value1", "key2": "value2"}`,
bodyJSON: `{"key1": "value1", "key2": "value3"}`,
expectedError: "JSON is not the same",
},
{
name: "MissingKeyJSON",
originalJSON: `{"key1": "value1", "key2": "value2"}`,
bodyJSON: `{"key1": "value1"}`,
expectedError: "JSON is not the same",
},
{
name: "ExtraKeyJSON",
originalJSON: `{"key1": "value1"}`,
bodyJSON: `{"key1": "value1", "key2": "value2"}`,
expectedError: "JSON is not the same",
},
{
name: "EmptyJSON",
originalJSON: `{}`,
bodyJSON: `{}`,
expectedError: "", // No error expected, empty JSONs are the same
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
// Call the Diff function with the test input
err := Diff(testCase.originalJSON)([]byte(testCase.bodyJSON))

// Check if the error message matches the expected result
if testCase.expectedError == "" {
assert.NoError(t, err) // No error expected
} else {
assert.Error(t, err) // Error expected
assert.Contains(t, err.Error(), testCase.expectedError)
}
})
}
}

func TestNotPresent(t *testing.T) {
tests := []jsonTest{
{
Expand All @@ -35,7 +91,7 @@ func TestNotPresent(t *testing.T) {
{
caseName: "correct check map",
data: `{"o":[{"1":"a"}, {"2":"b"}, {"3":"c"}]}`,
expression: "$.o[0][1]",
expression: "$.o[0]['1']",
},
}

Expand Down Expand Up @@ -72,7 +128,7 @@ func TestPresent(t *testing.T) {
{
caseName: "correct check map",
data: `{"o":[{"1":"a"}, {"2":"b"}, {"3":"c"}]}`,
expression: "$.o[0][1]",
expression: "$.o[0]['1']",
IsNilErr: true,
},
{
Expand Down Expand Up @@ -133,7 +189,7 @@ func TestNotEmpty(t *testing.T) {
{
caseName: "correct check map",
data: `{"o":[{"1":"a"}, {"2":"b"}, {"3":"c"}]}`,
expression: "$.o[0][1]",
expression: "$.o[0]['1']",
IsNilErr: true,
},
{
Expand Down Expand Up @@ -561,6 +617,7 @@ func TestEqual(t *testing.T) {
data: `{"a":186135434, "b":{"bs":"sb"}}`,
expression: "$.a",
expect: 186135434,
IsNilErr: true,
},
{
caseName: "check float",
Expand Down Expand Up @@ -640,3 +697,67 @@ func TestNotEqual(t *testing.T) {
}
}
}

func TestGetValueFromJSON(t *testing.T) {
testCases := []struct {
name string
inputJSON string
expression string
expectedValue interface{}
expectedError string
}{
{
name: "ValidExpressionObject",
inputJSON: `{"key1": "value1", "key2": {"key3": "value3"}}`,
expression: "key2.key3",
expectedValue: "value3",
expectedError: "", // No error expected
},
{
name: "ValidExpressionArray",
inputJSON: `{"key1": "value1", "key2": [1, 2, 3]}`,
expression: "key2[1]",
expectedValue: int64(2),
expectedError: "", // No error expected
},
{
name: "ValidExpressionMap",
inputJSON: `{"key1": "value1", "key2": {"subkey1": "subvalue1"}}`,
expression: "key2",
expectedValue: map[string]interface{}{"subkey1": "subvalue1"},
expectedError: "", // No error expected
},
{
name: "InvalidJSON",
inputJSON: `invalid json`,
expression: "key1",
expectedValue: nil,
expectedError: "could not parse json",
},
{
name: "InvalidExpression",
inputJSON: `{"key1": "value1"}`,
expression: "key2",
expectedValue: nil,
expectedError: "could not find element by path key2 in JSON",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
// Call the GetValueFromJSON function with the test input
value, err := GetValueFromJSON([]byte(testCase.inputJSON), testCase.expression)

// Check if the error message matches the expected result
if testCase.expectedError == "" {
assert.NoError(t, err) // No error expected
} else {
assert.Error(t, err) // Error expected
assert.Contains(t, err.Error(), testCase.expectedError)
}

// Check if the returned value matches the expected result
assert.Equal(t, testCase.expectedValue, value)
})
}
}
Loading

0 comments on commit 8b574b1

Please sign in to comment.