Skip to content

Commit

Permalink
Merge pull request #166 from Cdayz/issue-#137
Browse files Browse the repository at this point in the history
  • Loading branch information
Cdayz committed Jun 22, 2022
2 parents 05a8de7 + 172951c commit 1c1ad34
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 208 deletions.
3 changes: 3 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,11 @@ Example:
dbResponse:
- '{"code":"GIFT100000-000002","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
- '{"code":"GIFT100000-000003","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
- '{"code":"$matchRegexp(GIFT([0-9]{6})-([0-9]{6}))","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
```

Как видно из примера, вы можете использовать Regexp для проверки ответа БД.

```yaml
...
dbResponse:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,11 @@ Example:
dbResponse:
- '{"code":"GIFT100000-000002","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
- '{"code":"GIFT100000-000003","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
- '{"code":"$matchRegexp(GIFT([0-9]{6})-([0-9]{6}))","purchase_date":"2330-02-02T13:15:11.912874","partner_id":1}'
```

As you can see in this example, you can use Regexp for checking db response body.

```yaml
...
dbResponse:
Expand Down
123 changes: 18 additions & 105 deletions checker/response_db/response_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/lamoda/gonkey/checker"
"github.com/lamoda/gonkey/compare"
"github.com/lamoda/gonkey/models"

"github.com/fatih/color"
Expand Down Expand Up @@ -84,129 +85,41 @@ func (c *ResponseDbChecker) check(
return errors, nil
}
// compare responses as json lists
var checkErrors []error
if ignoreOrdering {
checkErrors, err = compareDbRespWithoutOrdering(t.DbResponseJson(), actualDbResponse, testName)
} else {
checkErrors, err = compareDbResp(t.DbResponseJson(), actualDbResponse, testName, t.DbQueryString())
}
expectedItems, err := toJsonArray(t.DbResponseJson(), "expected", testName)
if err != nil {
return nil, err
}
errors = append(errors, checkErrors...)

return errors, nil
}

func compareDbRespWithoutOrdering(expected, actual []string, testName string) ([]error, error) {
var errors []error
var actualJsons []interface{}
var expectedJsons []interface{}

// gather expected and actual rows
for i, row := range expected {
// decode expected row
var expectedJson interface{}
if err := json.Unmarshal([]byte(row), &expectedJson); err != nil {
return nil, fmt.Errorf(
"invalid JSON in the expected DB response for test %s:\n row #%d:\n %s\n error:\n%s",
testName,
i,
row,
err.Error(),
)
}
expectedJsons = append(expectedJsons, expectedJson)
// decode actual row
var actualJson interface{}
if err := json.Unmarshal([]byte(actual[i]), &actualJson); err != nil {
return nil, fmt.Errorf(
"invalid JSON in the actual DB response for test %s:\n row #%d:\n %s\n error:\n%s",
testName,
i,
actual[i],
err.Error(),
)
}
actualJsons = append(actualJsons, actualJson)
}

remove := func(array []interface{}, i int) []interface{} {
array[i] = array[len(array)-1]
return array[:len(array)-1]
}

// compare actual and expected rows
for _, actualRow := range actualJsons {
for i, expectedRow := range expectedJsons {
if diff := pretty.Compare(expectedRow, actualRow); diff == "" {
expectedJsons = remove(expectedJsons, i)
break
}
}
actualItems, err := toJsonArray(actualDbResponse, "actual", testName)
if err != nil {
return nil, err
}

if len(expectedJsons) > 0 {
errorString := "missing expected items in database:"
errs := compare.Compare(expectedItems, actualItems, compare.CompareParams{
IgnoreArraysOrdering: ignoreOrdering,
})

for _, expectedRow := range expectedJsons {
expectedRowJson, _ := json.Marshal(expectedRow)
errorString += fmt.Sprintf("\n - %s", color.CyanString("%s", expectedRowJson))
}

errors = append(errors, fmt.Errorf(errorString))
}
errors = append(errors, errs...)

return errors, nil
}

func compareDbResp(expected, actual []string, testName string, query interface{}) ([]error, error) {
var errors []error
var actualJson interface{}
var expectedJson interface{}

for i, row := range expected {
// decode expected row
if err := json.Unmarshal([]byte(row), &expectedJson); err != nil {
func toJsonArray(items []string, qual, testName string) ([]interface{}, error) {
var itemJSONs []interface{}
for i, row := range items {
var itemJson interface{}
if err := json.Unmarshal([]byte(row), &itemJson); err != nil {
return nil, fmt.Errorf(
"invalid JSON in the expected DB response for test %s:\n row #%d:\n %s\n error:\n%s",
"invalid JSON in the %s DB response for test %s:\n row #%d:\n %s\n error:\n%s",
qual,
testName,
i,
row,
err.Error(),
)
}
// decode actual row
if err := json.Unmarshal([]byte(actual[i]), &actualJson); err != nil {
return nil, fmt.Errorf(
"invalid JSON in the actual DB response for test %s:\n row #%d:\n %s\n error:\n%s",
testName,
i,
actual[i],
err.Error(),
)
}

// compare responses row as jsons
if err := compareDbResponseRow(expectedJson, actualJson, query); err != nil {
errors = append(errors, err)
}
}

return errors, nil
}

func compareDbResponseRow(expected, actual, query interface{}) error {
var err error

if diff := pretty.Compare(expected, actual); diff != "" {
err = fmt.Errorf(
"items in database do not match (-expected: +actual):\n test query:\n%s\n result diff:\n%s",
color.CyanString("%v", query),
color.CyanString("%v", diff),
)
itemJSONs = append(itemJSONs, itemJson)
}
return err
return itemJSONs, nil
}

func compareDbResponseLength(expected, actual []string, query interface{}) error {
Expand Down
103 changes: 0 additions & 103 deletions checker/response_db/response_db_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions examples/with-db-example/cases/database-with-vars.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
SELECT id, name FROM testing WHERE id={{ $gonkey_id }}
dbResponse:
- '{"id": {{ $gonkey_id }}, "name": "gonkey"}'
- dbQuery: SELECT id, name FROM testing WHERE id={{ $gonkey_id }}
dbResponse:
- '{"id": {{ $gonkey_id }}, "name": "$matchRegexp(gonkey)"}'

0 comments on commit 1c1ad34

Please sign in to comment.