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

Externalise examples should override inline examples with same name #1336

Merged
merged 6 commits into from
Oct 14, 2024
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
18 changes: 14 additions & 4 deletions core/src/main/kotlin/io/specmatic/core/Scenario.kt
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,10 @@ data class Scenario(
)
}

fun useExamples(externalisedJSONExamples: Map<OpenApiSpecification.OperationIdentifier, List<Row>>): Scenario {
val matchingTestData: Map<OpenApiSpecification.OperationIdentifier, List<Row>> = matchingRows(externalisedJSONExamples)
fun useExamples(rawExternalisedExamples: Map<OpenApiSpecification.OperationIdentifier, List<Row>>): Scenario {
val matchingRawExternalisedEamples: Map<OpenApiSpecification.OperationIdentifier, List<Row>> = matchingRows(rawExternalisedExamples)

val newExamples: List<Examples> = matchingTestData.map { (operationId, rows) ->
val externalisedExamples: List<Examples> = matchingRawExternalisedEamples.map { (operationId, rows) ->
if(rows.isEmpty())
return@map emptyList()

Expand All @@ -567,7 +567,7 @@ data class Scenario(
listOf(Examples(columns, rowsWithPathData))
}.flatten()

return this.copy(examples = this.examples + newExamples)
return this.copy(examples = inlineExamplesThatAreNotOverridden(externalisedExamples) + externalisedExamples)
}

private fun matchingRows(externalisedJSONExamples: Map<OpenApiSpecification.OperationIdentifier, List<Row>>): Map<OpenApiSpecification.OperationIdentifier, List<Row>> {
Expand Down Expand Up @@ -615,6 +615,16 @@ data class Scenario(

return Result.fromResults(listOf(requestMatch, responseMatch))
}

private fun inlineExamplesThatAreNotOverridden(externalisedExamples: List<Examples>): List<Examples> {
val externalisedExampleNames = externalisedExamples.flatMap { it.rows.map { row -> row.name } }.toSet()

return this.examples.mapNotNull {
val rowsThatAreNotOverridden = it.rows.filter { row -> row.name !in externalisedExampleNames }
if(rowsThatAreNotOverridden.isEmpty()) return@mapNotNull null
it.copy(rows = rowsThatAreNotOverridden)
}
}
}

fun newExpectedServerStateBasedOn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,38 @@ class LoadTestsFromExternalisedFiles {
}
})

assertThat(idsSeen).containsExactlyInAnyOrder("123", "456")
assertThat(results.testCount).isEqualTo(2)
assertThat(idsSeen).contains("123", "456")
assertThat(results.testCount).isEqualTo(3)
}

@Test
fun `external example should override the inline example with the same name and should restrict it from running as a test`() {
val feature = OpenApiSpecification
.fromFile("src/test/resources/openapi/has_overriding_external_examples.yaml")
.toFeature()
.loadExternalisedExamples()

val idsSeen = mutableListOf<String>()

val result = feature.executeTests(object : TestExecutor {
override fun execute(request: HttpRequest): HttpResponse {
val path = request.path ?: fail("Path expected")
idsSeen.add(path.split("/").last())

return HttpResponse(200, parsedJSONObject("""{"id": 10, "name": "Jack"}""")).also {
println("---")
println(request.toLogString())
println(it.toLogString())
println()
}
}
})

assertThat(idsSeen).contains("overriding_external_id")
assertThat(idsSeen).doesNotContain("overridden_inline_id")

assertThat(idsSeen).hasSize(2)
assertThat(result.testCount).isEqualTo(2)
}

@Test
Expand All @@ -303,8 +333,8 @@ class LoadTestsFromExternalisedFiles {
}
})

assertThat(idsSeen).containsExactlyInAnyOrder("123", "456")
assertThat(results.testCount).isEqualTo(2)
assertThat(idsSeen).contains("123", "456")
assertThat(results.testCount).isEqualTo(3)
}

@Test
Expand Down Expand Up @@ -335,7 +365,7 @@ class LoadTestsFromExternalisedFiles {
}
})

assertThat(results.testCount).isEqualTo(2)
assertThat(results.testCount).isEqualTo(3)
}

@Test
Expand Down Expand Up @@ -372,7 +402,7 @@ class LoadTestsFromExternalisedFiles {

println(results.report())

assertThat(results.testCount).isEqualTo(2)
assertThat(results.failureCount).isEqualTo(2)
assertThat(results.testCount).isEqualTo(3)
assertThat(results.failureCount).isEqualTo(3)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ paths:
required: true
description: Unique identifier of the person
schema:
type: integer
type: string
examples:
johnDoe:
summary: Example ID for John Doe
value: 123
get_person:
summary: Example ID for Unknown
value: inline_id
responses:
'200':
description: Successful response
Expand All @@ -40,3 +43,8 @@ paths:
value:
id: 123
name: John Doe
get_person:
summary: Example response for Unknown
value:
id: 1000
name: Unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"http-request": {
"method": "GET",
"path": "/person/external_id"
},
"http-response": {
"status": 200,
"body": {
"id": 789,
"name": "John External Doe"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.3
info:
title: Person API
description: API for retrieving person details
version: 1.0.0
paths:
/person/{id}:
get:
summary: Get person details by ID
description: Retrieves the details of a person based on their unique identifier
parameters:
- name: id
in: path
required: true
description: Unique identifier of the person
schema:
type: string
examples:
johnDoe:
summary: Example ID for John Doe
value: 123
get_person:
summary: Example ID for Unknown
value: overridden_inline_id
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
id:
type: integer
name:
type: string
required:
- id
- name
examples:
johnDoe:
summary: Example response for John Doe
value:
id: 123
name: John Doe
get_person:
summary: Example response for Unknown
value:
id: 1000
name: Unknown
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"http-request": {
"method": "GET",
"path": "/person/overriding_external_id"
},
"http-response": {
"status": 200,
"body": {
"id": 789,
"name": "John External Doe"
}
}
}