Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <[email protected]>
  • Loading branch information
nhtruong committed May 21, 2024
1 parent 82afc67 commit a208abf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion tests/index_lifecycle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ chapters:
age:
type: integer
settings:
number_of_shard: 5
number_of_shards: 5
number_of_replicas: 2
response:
status: 200
Expand Down
41 changes: 21 additions & 20 deletions tools/src/tester/ChapterEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Evaluation> {
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)
}
}

0 comments on commit a208abf

Please sign in to comment.