-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/lamoda/gonkey into featur…
…e/ignore-db-ordering
- Loading branch information
Showing
13 changed files
with
447 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.