Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/lamoda/gonkey into featur…
Browse files Browse the repository at this point in the history
…e/ignore-db-ordering
  • Loading branch information
Lev Marder committed May 12, 2022
2 parents 7433636 + cba2087 commit 1aba9d5
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 45 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
# v1.15.0 (Thu May 12 2022)

#### 🚀 Enhancement

- New: regexp in query matching [#132](https://github.com/lamoda/gonkey/pull/132) (lev.marder@lamoda.ru)

#### 🏠 Internal

- Bump github.com/stretchr/testify from 1.7.0 to 1.7.1 [#156](https://github.com/lamoda/gonkey/pull/156) ([@dependabot[bot]](https://github.com/dependabot[bot]))
- #133 | fix data race [#155](https://github.com/lamoda/gonkey/pull/155) ([@architectv](https://github.com/architectv))

#### Authors: 3

- [@dependabot[bot]](https://github.com/dependabot[bot])
- Alexey Vasyukov ([@architectv](https://github.com/architectv))
- Lev Marder ([@ikramanop](https://github.com/ikramanop))

---

# v1.14.1 (Wed May 11 2022)

#### 🐛 Bug Fix

- new: add comparisonParams to BodyMatches for Json and XML [#117](https://github.com/lamoda/gonkey/pull/117) (Alexey.Tyuryumov@acronis.com [@Alexey19](https://github.com/Alexey19))

#### Authors: 2

- [@Alexey19](https://github.com/Alexey19)
- Alexey Tyuryumov (Alexey.Tyuryumov@acronis.com)

---

# v1.14.0 (Fri Feb 25 2022)

#### 🚀 Enhancement
Expand Down
19 changes: 19 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,25 @@ runner.RunWithTesting(t, &runner.RunWithTestingParams{
...
```

###### queryMatchesRegexp

Расширяет `queryMatches` так, чтобы можно было использовать проверку по регулярному выражению.

Параметры:
- `expectedQuery` (обязательный) - строка параметров с которой будет сверяться запрос. Порядок параметров не имеет значения.

Пример:
```yaml
...
mocks:
service1:
requestConstraints:
# работает аналогично queryMatches с добавлением возможности вызова $matchRegexp
- kind: queryMatchesRegexp
expectedQuery: key1=value1&key2=$matchRegexp(\\d+)&key1=value11
...
```

###### methodIs

Проверяет, что метод запроса соответствует заданному.
Expand Down
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ Checks that the GET request parameters correspond to the ones defined in the `qu
Parameters:
- `expectedQuery` (mandatory) - a list of parameters to compare the parameter string to. The order of parameters is not important.

Example:
Examples:
```yaml
...
mocks:
Expand All @@ -772,6 +772,25 @@ Example:
...
```

###### queryMatchesRegexp

Expands `queryMatches` so it can be used with regexp pattern matching.

Parameters:
- `expectedQuery` (mandatory) - a list of parameters to compare the parameter string to. The order of parameters is not important.

Example:
```yaml
...
mocks:
service1:
requestConstraints:
# works similarly to queryMatches with an addition of $matchRegexp usage
- kind: queryMatchesRegexp
expectedQuery: key1=value1&key2=$matchRegexp(\\d+)&key1=value11
...
```

###### methodIs

Checks that the request method corresponds to the expected one.
Expand All @@ -783,7 +802,7 @@ There are also 2 short variations that don't require `method` parameter:
- `methodIsGET`
- `methodIsPOST`

Examples:
Example:
```yaml
...
mocks:
Expand Down
7 changes: 4 additions & 3 deletions compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

type CompareParams struct {
IgnoreValues bool
IgnoreArraysOrdering bool
DisallowExtraFields bool
IgnoreValues bool `json:"ignoreValues" yaml:"ignoreValues"`
IgnoreArraysOrdering bool `json:"ignoreArraysOrdering" yaml:"ignoreArraysOrdering"`
DisallowExtraFields bool `json:"disallowExtraFields" yaml:"disallowExtraFields"`
IgnoreDbOrdering bool `json:"IgnoreDbOrdering" yaml:"ignoreDbOrdering"`
failFast bool // End compare operation after first error
}

Expand Down
57 changes: 57 additions & 0 deletions compare/compare_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package compare

import (
"fmt"
"regexp"
)

func CompareQuery(expected, actual []string) (bool, error) {
if len(expected) != len(actual) {
return false, fmt.Errorf("expected and actual query params have different lengths")
}

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

var expectedCopy = make([]string, len(expected))
copy(expectedCopy, expected)
var actualCopy = make([]string, len(actual))
copy(actualCopy, actual)

for len(expectedCopy) != 0 {
found := false

for i, expectedValue := range expectedCopy {
for j, actualValue := range actualCopy {
if matches := regexExprRx.FindStringSubmatch(expectedValue); matches != nil {
rx, err := regexp.Compile(matches[1])
if err != nil {
return false, err
}

found = rx.MatchString(actualValue)
} else {
found = expectedValue == actualValue
}

if found {
expectedCopy = remove(expectedCopy, i)
actualCopy = remove(actualCopy, j)
break
}
}

if found {
break
}
}

if !found {
return false, nil
}
}

return true, nil
}
49 changes: 49 additions & 0 deletions compare/compare_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package compare

import "testing"

func TestCompareQuery(t *testing.T) {
tests := []struct {
name string
expectedQuery []string
actualQuery []string
}{
{
name: "simple expected and actual",
expectedQuery: []string{"cake"},
actualQuery: []string{"cake"},
},
{
name: "expected and actual with two values",
expectedQuery: []string{"cake", "tea"},
actualQuery: []string{"cake", "tea"},
},
{
name: "expected and actual with two values and different order",
expectedQuery: []string{"cake", "tea"},
actualQuery: []string{"tea", "cake"},
},
{
name: "expected and actual with same values",
expectedQuery: []string{"tea", "cake", "tea"},
actualQuery: []string{"cake", "tea", "tea"},
},
{
name: "expected and actual with regexp",
expectedQuery: []string{"tea", "$matchRegexp(^c\\w+)"},
actualQuery: []string{"cake", "tea"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ok, err := CompareQuery(tt.expectedQuery, tt.actualQuery)
if err != nil {
t.Error(err)
}
if !ok {
t.Errorf("expected and actual queries do not match")
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/kylelemons/godebug v1.1.0
github.com/lib/pq v1.3.0
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.7.1
github.com/tidwall/gjson v1.13.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=
github.com/tidwall/gjson v1.6.0/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.13.0 h1:3TFY9yxOQShrvmjdM76K+jc66zJeT6D3/VFFYCGQf7M=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
Expand Down
Loading

0 comments on commit 1aba9d5

Please sign in to comment.