forked from opensearch-project/opensearch-api-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evaluate payload body in prologues and epilogues. (opensearch-project…
…#772) Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
7 changed files
with
167 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import _ from "lodash" | ||
import { Evaluation, Result } from './types/eval.types' | ||
import { Logger } from "../Logger" | ||
import { to_json } from "../helpers" | ||
import { ActualResponse, Payload } from "./types/story.types" | ||
import { atomizeChangeset, diff, Operation } from "json-diff-ts" | ||
|
||
export default class ResponsePayloadEvaluator { | ||
private readonly logger: Logger | ||
|
||
constructor(logger: Logger) { | ||
this.logger = logger | ||
} | ||
|
||
evaluate(response: ActualResponse, expected_payload?: Payload): Evaluation { | ||
if (expected_payload == null) return { result: Result.PASSED } | ||
const payload = response.payload | ||
this.logger.info(`${to_json(payload)}`) | ||
const delta = atomizeChangeset(diff(expected_payload, payload)) | ||
const messages: string[] = _.compact(delta.map((value, _index, _array) => { | ||
switch (value.type) { | ||
case Operation.UPDATE: | ||
return `expected ${value.path.replace('$.', '')}='${value.oldValue}', got '${value.value}'` | ||
case Operation.REMOVE: | ||
return `missing ${value.path.replace('$.', '')}='${value.value}'` | ||
} | ||
})) | ||
return messages.length > 0 ? { result: Result.FAILED, message: _.join(messages, ', ') } : { result: Result.PASSED } | ||
} | ||
} |
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,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { Result } from "tester/types/eval.types"; | ||
import ResponsePayloadEvaluator from "tester/ResponsePayloadEvaluator"; | ||
import { Logger } from "Logger"; | ||
import { ActualResponse } from "tester/types/story.types"; | ||
|
||
function create_response(payload: any): ActualResponse { | ||
return { | ||
status: 200, | ||
content_type: 'application/json', | ||
payload | ||
} | ||
} | ||
|
||
describe('ResponsePayloadEvaluator', () => { | ||
const evaluator = new ResponsePayloadEvaluator(new Logger()) | ||
|
||
describe('evaluate', () => { | ||
test('succeeds without an expected payload', () => { | ||
expect(evaluator.evaluate(create_response({}), undefined)).toEqual({ result: Result.PASSED }) | ||
}) | ||
|
||
test('fails with a non-matching payload', () => { | ||
expect(evaluator.evaluate(create_response({}), { x: 1 })).toEqual({ result: Result.FAILED, message: "missing x='1'" }) | ||
}) | ||
|
||
test('succeeds with a matching payload', () => { | ||
expect(evaluator.evaluate(create_response({ x: 1 }), { x: 1 })).toEqual({ result: Result.PASSED }) | ||
}) | ||
}) | ||
}) |
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