Skip to content

Commit

Permalink
Merge pull request #20 from programmfabrik/fix_control_in_array
Browse files Browse the repository at this point in the history
Fix control in array
  • Loading branch information
martinrode authored Jul 2, 2020
2 parents 7549b3a + 824b23a commit b422a56
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test:
go test ./...

apitest:
./apitest -d test/
./apitest --stop-on-fail -d test/

gox:
go get github.com/mitchellh/gox
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ Manifest is loaded as **template**, so you can use variables, Go **range** and *
"collect_response": [
"@continue_response_pending.json",
"@continue_response_processing.json"
]
],

// If set to true, the test case will consider its failure as a success, and the other way around
"reverse_test_result": false
}
```

Expand Down
18 changes: 13 additions & 5 deletions api_testcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Case struct {
standardHeaderFromStore map[string]string

ServerURL string `json:"server_url"`
ReverseTestResult bool `json:"reverse_test_result"`
}

func (testCase Case) runAPITestCase(parentReportElem *report.ReportElement) bool {
Expand Down Expand Up @@ -98,6 +99,11 @@ func (testCase Case) runAPITestCase(parentReportElem *report.ReportElement) bool
success = false
}

// Reverse if needed
if testCase.ReverseTestResult {
success = !success
}

if !success {
logrus.WithFields(logrus.Fields{"elapsed": elapsed.Seconds()}).Warnf(" [%2d] failure", testCase.index)
} else {
Expand Down Expand Up @@ -285,7 +291,7 @@ func (testCase Case) executeRequest(counter int) (compare.CompareResult, api.Req
func (testCase Case) LogResp(response api.Response) {
errString := fmt.Sprintf("[RESPONSE]:\n%s\n\n", limitLines(response.ToString(), Config.Apitest.Limit.Response))

if testCase.LogNetwork != nil && !*testCase.LogNetwork && !testCase.ContinueOnFailure {
if !testCase.ReverseTestResult && testCase.LogNetwork != nil && !*testCase.LogNetwork && !testCase.ContinueOnFailure {
testCase.ReportElem.SaveToReportLogF(errString)
logrus.Debug(errString)
}
Expand All @@ -295,7 +301,7 @@ func (testCase Case) LogResp(response api.Response) {
func (testCase Case) LogReq(req api.Request) {
errString := fmt.Sprintf("[REQUEST]:\n%s\n\n", limitLines(req.ToString(logCurl), Config.Apitest.Limit.Request))

if !testCase.ContinueOnFailure && testCase.LogNetwork != nil && *testCase.LogNetwork == false {
if !testCase.ReverseTestResult && !testCase.ContinueOnFailure && testCase.LogNetwork != nil && *testCase.LogNetwork == false {
testCase.ReportElem.SaveToReportLogF(errString)
logrus.Debug(errString)
}
Expand Down Expand Up @@ -396,9 +402,11 @@ func (testCase Case) run() (bool, error) {
}

if !responsesMatch.Equal || timedOutFlag {
for _, v := range responsesMatch.Failures {
logrus.Errorf("[%s] %s", v.Key, v.Message)
r.SaveToReportLog(fmt.Sprintf("[%s] %s", v.Key, v.Message))
if !testCase.ReverseTestResult {
for _, v := range responsesMatch.Failures {
logrus.Errorf("[%s] %s", v.Key, v.Message)
r.SaveToReportLog(fmt.Sprintf("[%s] %s", v.Key, v.Message))
}
}

collectArray, ok := testCase.CollectResponse.(util.JsonArray)
Expand Down
19 changes: 18 additions & 1 deletion pkg/lib/compare/comparison_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,24 @@ func arrayComparison(left, right util.JsonArray, noExtra, orderMaters bool, cont
continue
}

tmp, err := JsonEqual(lv, rv, control)
// We need to check the left interface against the right one multiple times
// JsonEqual modifies such interface (it deletes it afterwards)
// Therefore we need a copy of it for this case
var (
err error
tmp CompareResult
)
switch jo := lv.(type) {
case util.JsonObject:
lvv := util.JsonObject{}
for k, v := range jo {
lvv[k] = v
}
tmp, err = JsonEqual(lvv, rv, control)
default:
tmp, err = JsonEqual(lv, rv, control)
}

if err != nil {
return CompareResult{}, err
}
Expand Down
142 changes: 142 additions & 0 deletions test/control/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"http_server": {
"addr": ":9999",
"dir": "../_res",
"testmode": false
},
"name": "check control structures in array",
"tests": [
{
"name": "check control structures in array",
"request": {
"server_url": "http://localhost:9999",
"endpoint": "bounce-json",
"method": "POST",
"body": {
"_files": [
{
"path": "Test XML+CSV+JSON/1-2.csv"
},
{
"path": "Test XML+CSV+JSON/1-2.json"
},
{
"path": "files/no-1-sid-10-berlin.jpg"
},
{
"path": "files/no-2-sid-10-mona-lisa-1.jpg"
},
{
"path": "files/no-3-sid-9-Gustav_Klimt_016.jpg"
},
{
"path": "files/no-4-sid-9-art-exposition-735518_1920_big_small.png"
},
{
"path": "files/so-henk-was-here_small.png"
}
]
}
},
"response": {
"statuscode": 200,
"body": {
"body": {
"_files": [
{
"path:control": {
"match": "^.*Gustav_Klimt_016.*$"
}
},
{
"path:control": {
"match": "^.*art-exposition-735518_1920.*$"
}
},
{
"path:control": {
"match": "^.*berlin.*$"
}
},
{
"path:control": {
"match": "^.*mona-lisa-1.*$"
}
},
{
"path:control": {
"match": "^.*henk.*$"
}
}
]
}
}
}
},
{
"name": "check control structures in array (should fail)",
"request": {
"server_url": "http://localhost:9999",
"endpoint": "bounce-json",
"method": "POST",
"body": {
"_files": [
{
"path": "Test XML+CSV+JSON/1-2.csv"
},
{
"path": "Test XML+CSV+JSON/1-2.json"
},
{
"path": "files/no-1-sid-10-berlin.jpg"
},
{
"path": "files/no-2-sid-10-mona-lisa-1.jpg"
},
{
"path": "files/no-3-sid-9-Gustav_Klimt_016.jpg"
},
{
"path": "files/no-4-sid-9-art-exposition-735518_1920_big_small.png"
}
]
}
},
"response": {
"statuscode": 200,
"body": {
"body": {
"_files": [
{
"path:control": {
"match": "^.*Gustav_Klimt_016.*$"
}
},
{
"path:control": {
"match": "^.*art-exposition-735518_1920.*$"
}
},
{
"path:control": {
"match": "^.*berlin.*$"
}
},
{
"path:control": {
"match": "^.*mona-lisa-1.*$"
}
},
{
"path:control": {
"match": "^.*henk.*$"
}
}
]
}
}
},
"reverse_test_result": true
}
]
}

0 comments on commit b422a56

Please sign in to comment.