Refactor 'the response should contain list'

This commit is contained in:
2021-04-03 00:17:29 +02:00
parent a48cd52652
commit 2bb90ced03
6 changed files with 19 additions and 15 deletions

View File

@@ -0,0 +1,7 @@
package assert
import kotlin.test.assertTrue
infix fun IntProgression.assertContain(expected: Int) {
assertTrue(this.contains(expected), "Expected $this less than $expected")
}

View File

@@ -38,7 +38,7 @@ class `Article routes` : BaseTest() {
`And the response should contain pattern`("$.result[1].createdBy.name.firstName", "firstName.+") `And the response should contain pattern`("$.result[1].createdBy.name.firstName", "firstName.+")
`And the response should contain pattern`("$.result[2].createdBy.name.firstName", "firstName.+") `And the response should contain pattern`("$.result[2].createdBy.name.firstName", "firstName.+")
`And the response should not contain`("$.result[3]") `And the response should not contain`("$.result[3]")
`And the response should contain list`("$.result", 3, 3) `And the response should contain list`("$.result", 3)
} }
} }
} }

View File

@@ -57,7 +57,7 @@ class `Comment constitutions routes` : BaseTest() {
`And the response should contain`("$.limit", 50) `And the response should contain`("$.limit", 50)
`And the response should contain`("$.result[0].createdBy.id", "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5") `And the response should contain`("$.result[0].createdBy.id", "46e0bda9-ca6a-4c65-a58b-7e7267a0bbc5")
`And the response should contain`("$.result[0].target.id", "34ddd50a-da00-4a90-a869-08baa2a121be") `And the response should contain`("$.result[0].target.id", "34ddd50a-da00-4a90-a869-08baa2a121be")
`And the response should contain list`("$.result[*]", 1, 1) `And the response should contain list`("$.result[*]", 1)
} }
} }
} }

View File

@@ -137,7 +137,7 @@ class `Opinion routes` : BaseTest() {
`authenticated as`("Albert", "Einstein") `authenticated as`("Albert", "Einstein")
} `Then the response should be` OK and { } `Then the response should be` OK and {
`And the response should contain`("$.result[0].name", "Opinion9") `And the response should contain`("$.result[0].name", "Opinion9")
`And the response should contain list`("$.result[*]", 1, 1) `And the response should contain list`("$.result[*]", 1)
} }
} }
} }

View File

@@ -119,7 +119,7 @@ class `Workgroup routes` : BaseTest() {
`And the response should contain`("$.description", "Une petite souris") `And the response should contain`("$.description", "Une petite souris")
`And have property`("$.members") `And have property`("$.members")
`And the response should contain list`("$.members", 3, 3) `And the response should contain list`("$.members", 3)
`And the response should contain`("$.members.[1]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9") `And the response should contain`("$.members.[1]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9")
`And the response should contain`("$.members.[2]citizen.id", "87909ba3-2069-431c-9924-219fd8411cf2") `And the response should contain`("$.members.[2]citizen.id", "87909ba3-2069-431c-9924-219fd8411cf2")
} }
@@ -215,7 +215,7 @@ class `Workgroup routes` : BaseTest() {
] ]
""" """
} `Then the response should be` OK and { } `Then the response should be` OK and {
`And the response should contain list`("$", 2, 2) `And the response should contain list`("$", 2)
`And the response should contain`("$.[0]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9") `And the response should contain`("$.[0]citizen.id", "94f92424-c257-4582-907c-98564a8c4ac9")
`And the response should contain`("$.[1]citizen.id", "1baf48bb-02bc-4d8f-ac86-33335354f5e7") `And the response should contain`("$.[1]citizen.id", "1baf48bb-02bc-4d8f-ac86-33335354f5e7")
} }
@@ -252,7 +252,7 @@ class `Workgroup routes` : BaseTest() {
""" """
) )
} `Then the response should be` OK and { } `Then the response should be` OK and {
`And the response should contain list`("$", 2, 2) `And the response should contain list`("$", 2)
`And the response should contain`("$.[0]citizen.id", "be3b0926-8628-4426-804a-75188a6eb315") `And the response should contain`("$.[0]citizen.id", "be3b0926-8628-4426-804a-75188a6eb315")
`And the response should contain`("$.[1]citizen.id", "b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1") `And the response should contain`("$.[1]citizen.id", "b49e20c1-8393-45d6-a6a0-3fa5c71cbdc1")
} }

View File

@@ -1,7 +1,6 @@
package integration.steps.then package integration.steps.then
import assert.assertGreaterThan import assert.assertContain
import assert.assertLessThan
import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.JsonPath
import com.jayway.jsonpath.PathNotFoundException import com.jayway.jsonpath.PathNotFoundException
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
@@ -85,15 +84,13 @@ fun TestApplicationResponse.`And the response should contain pattern`(path: Stri
} }
} }
fun TestApplicationResponse.`And the response should contain list`(path: String, min: Int? = null, max: Int? = null) { fun TestApplicationResponse.`And the response should contain list`(path: String, exactCount: Int) =
`And the response should contain list`(path, IntRange(exactCount, exactCount))
fun TestApplicationResponse.`And the response should contain list`(path: String, range: IntRange) {
JsonPath.read<JSONArray?>(content, path).also { JsonPath.read<JSONArray?>(content, path).also {
assertNotNull(it) assertNotNull(it)
if (min != null) { range assertContain it.size
it.size assertGreaterThan min
}
if (max != null) {
it.size assertLessThan max
}
} }
} }