Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for OperationDescription.responseOutcomes #554

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ extension OperationDescription {
var outcomes = operation.responseOutcomes
// if .default is present and not already last
if let index = outcomes.firstIndex(where: { $0.status == .default }), index != (outcomes.count - 1) {
//then we move it to be last
// then we move it to be last
let defaultResp = outcomes.remove(at: index)
outcomes.append(defaultResp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Test_OperationDescription: Test_Core {
get: .init(parameters: [operationLevelParameter], requestBody: .b(.init(content: [:])), responses: [:]),
vendorExtensions: [:]
)
let allParameters = try _test(pathItem)
let allParameters = try makeOperationDescription(pathItem)?.allParameters

XCTAssertEqual(allParameters, [operationLevelParameter])
}
Expand All @@ -48,7 +48,7 @@ final class Test_OperationDescription: Test_Core {
get: .init(parameters: [operationLevelParameter], requestBody: .b(.init(content: [:])), responses: [:]),
vendorExtensions: [:]
)
let allParameters = try _test(pathItem)
let allParameters = try makeOperationDescription(pathItem)?.allParameters

XCTAssertEqual(allParameters, [pathLevelParameter, operationLevelParameter])
}
Expand All @@ -73,25 +73,78 @@ final class Test_OperationDescription: Test_Core {
),
vendorExtensions: [:]
)
let allParameters = try _test(pathItem)
let allParameters = try makeOperationDescription(pathItem)?.allParameters

XCTAssertEqual(allParameters, [pathLevelParameter, duplicatedParameter, operationLevelParameter])
}

private func _test(_ pathItem: OpenAPI.PathItem) throws -> [UnresolvedParameter] {
func testResponseOutcomes_without_default_response() {
let responses: OpenAPI.Response.Map = [
.status(code: 200): .b(.init(description: "200")), .status(code: 404): .b(.init(description: "404")),
]

let pathItem = OpenAPI.PathItem(get: .init(requestBody: .b(.init(content: [:])), responses: responses))
let responseOutcomes = makeOperationDescription(pathItem)?.responseOutcomes

XCTAssertEqual(
responseOutcomes,
[
OpenAPI.Operation.ResponseOutcome(status: responses[0].key, response: responses[0].value),
OpenAPI.Operation.ResponseOutcome(status: responses[1].key, response: responses[1].value),
]
)
}

func testResponseOutcomes_with_default_response_on_last() {
let responses: OpenAPI.Response.Map = [
.status(code: 200): .b(.init(description: "200")), .status(code: 404): .b(.init(description: "404")),
.default: .b(.init(description: "default")),
]

let pathItem = OpenAPI.PathItem(get: .init(requestBody: .b(.init(content: [:])), responses: responses))
let responseOutcomes = makeOperationDescription(pathItem)?.responseOutcomes

XCTAssertEqual(
responseOutcomes,
[
OpenAPI.Operation.ResponseOutcome(status: responses[0].key, response: responses[0].value),
OpenAPI.Operation.ResponseOutcome(status: responses[1].key, response: responses[1].value),
OpenAPI.Operation.ResponseOutcome(status: responses[2].key, response: responses[2].value),
]
)
}

func testResponseOutcomes_with_default_response_on_first() {
let responses: OpenAPI.Response.Map = [
.default: .b(.init(description: "default")), .status(code: 200): .b(.init(description: "200")),
.status(code: 404): .b(.init(description: "404")),
]

let pathItem = OpenAPI.PathItem(get: .init(requestBody: .b(.init(content: [:])), responses: responses))
let responseOutcomes = makeOperationDescription(pathItem)?.responseOutcomes

XCTAssertEqual(
responseOutcomes,
[
OpenAPI.Operation.ResponseOutcome(status: responses[1].key, response: responses[1].value),
OpenAPI.Operation.ResponseOutcome(status: responses[2].key, response: responses[2].value),
OpenAPI.Operation.ResponseOutcome(status: responses[0].key, response: responses[0].value),
]
)
}

private func makeOperationDescription(_ pathItem: OpenAPI.PathItem) -> OperationDescription? {
guard let endpoint = pathItem.endpoints.first else {
XCTFail("Unable to retrieve the path item first endpoint.")
return []
return nil
}

let operationDescription = OperationDescription(
return OperationDescription(
path: .init(["/test"]),
endpoint: endpoint,
pathParameters: pathItem.parameters,
components: .init(),
asSwiftSafeName: { $0 }
)

return try operationDescription.allParameters
}
}