diff --git a/tests/index_lifecycle.yaml b/tests/index_lifecycle.yaml index 79aae920b..7ea42963a 100644 --- a/tests/index_lifecycle.yaml +++ b/tests/index_lifecycle.yaml @@ -22,7 +22,7 @@ chapters: age: type: integer settings: - number_of_shard: 5 + number_of_shards: 5 number_of_replicas: 2 response: status: 200 diff --git a/tools/src/tester/ChapterEvaluator.ts b/tools/src/tester/ChapterEvaluator.ts index 4f97e9916..93d488716 100644 --- a/tools/src/tester/ChapterEvaluator.ts +++ b/tools/src/tester/ChapterEvaluator.ts @@ -16,55 +16,56 @@ export default class ChapterEvaluator { if (skipped) return { result: Result.SKIPPED, synopsis: this.chapter.synopsis } const operation = globalThis.spec_parser.locate_operation(this.chapter) const response = await globalThis.chapter_reader.read(this.chapter, true) + const params = this.#evaluate_parameters(operation) + const request_body = this.#evaluate_request_body(operation) + const status = this.#evaluate_status(response) + const payload = this.#evaluate_payload(operation, response) return { synopsis: this.chapter.synopsis, - request: { - parameters: this.#evaluate_parameters(operation), - requestBody: this.#evaluate_request_body(operation) - }, - response: { - status: this.#evaluate_status(response), - payload: this.#evaluate_payload(operation, response) - }, - result: this.result + request: { parameters: params, requestBody: request_body }, + response: { status, payload }, + result: this.#calculate_result(Object.values(params).concat([request_body, status, payload])) } } catch (error) { return { result: Result.ERROR, synopsis: this.chapter.synopsis, message: error as string } } } + #calculate_result (evaluations: Evaluation[]): Result { + if (evaluations.some(e => e.result === Result.ERROR)) return Result.ERROR + if (evaluations.some(e => e.result === Result.FAILED)) return Result.FAILED + if (evaluations.every(e => e.result === Result.SKIPPED)) return Result.SKIPPED + return Result.PASSED + } + #evaluate_parameters (operation: ParsedOperation): Record { return Object.fromEntries(Object.entries(this.chapter.parameters ?? {}).map(([name, parameter]) => { const schema = operation.parameters[name]?.schema const evaluation = globalThis.schema_validator.validate(schema, parameter) - if (evaluation.result !== Result.PASSED) this.result = Result.FAILED return [name, evaluation] })) } - #evaluate_request_body (operation: ParsedOperation): Evaluation | undefined { - if (!this.chapter.requestBody) return undefined + #evaluate_request_body (operation: ParsedOperation): Evaluation { + if (!this.chapter.requestBody) return { result: Result.SKIPPED } const schema = operation.requestBody?.content[this.chapter.requestBody?.content_type ?? '']?.schema if (schema == null) return { result: Result.FAILED, message: `Schema for "${this.chapter.requestBody.content_type}" request body not found.` } - const evaluation = globalThis.schema_validator.validate(schema, this.chapter.requestBody?.payload ?? {}) - if (evaluation.result !== Result.PASSED) this.result = Result.FAILED - return evaluation + return globalThis.schema_validator.validate(schema, this.chapter.requestBody?.payload ?? {}) } #evaluate_status (response: FullResponse): Evaluation { const expected_status = this.chapter.response?.status ?? 200 if (response.status === expected_status) return { result: Result.PASSED } this.skip_payload = true - this.result = Result.FAILED return { result: Result.FAILED, message: `Expected status ${expected_status}, but received ${response.status}` } } #evaluate_payload (operation: ParsedOperation, response: FullResponse): Evaluation { if (this.skip_payload) return { result: Result.SKIPPED } - const schema = operation.responses[response.status]?.content[response.content_type]?.schema + const content = operation.responses[response.status]?.content[response.content_type] + const schema = content?.schema + if (schema == null && content != null) return { result: Result.SKIPPED } if (schema == null) return { result: Result.FAILED, message: `Schema for "${response.status}: ${response.content_type}" response not found.` } - const evaluation = globalThis.schema_validator.validate(schema, response.payload) - if (evaluation.result !== Result.PASSED) this.result = Result.FAILED - return evaluation + return globalThis.schema_validator.validate(schema, response.payload) } }