-
Notifications
You must be signed in to change notification settings - Fork 305
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
Support nested paths in GraphQlTester #457
Comments
Do you not have an entity object for the data, such as |
Maybe sometimes, but otherwise we have to create entities just for matching a response, which is not something we want. The methods in the controller do data conversion quite often, so there isn't an entity that has that type defined, e.g. |
Okay I see. One other option comes to mind. The tester supports the full JSONPath syntax, so something like this should work:
We could consider support for setting a path prefix that applies to subsequent paths. For example:
|
We know, and it does come in handy in a lot of cases.
A internal inline fun <reified T : Any> GraphQlTester.Path.prefix(
path: String,
noinline consumer: (GraphQlTester.Path) -> GraphQlTester.Path
) = consumer(this.pathPrefix(path)).pathPrefix(null) |
@rstoyanchev any more thoughts on this? Have been writing stuff like this again today: .path("merchant.contentClassification.id").isEqualTo(
contentClassification.id!!.encodeGlobalId<ContentClassification>()
)
.path("merchant.contentClassification.name").isEqualTo(
contentClassification.name
)
.path("merchant.contentClassification.description").isEqualTo(
contentClassification.description
)
.path("merchant.contentClassification.excludeFromSitemap").isEqualTo(
contentClassification.excludeFromSitemap
) And would still love to use it more like this: .prefix("merchant.contentClassification") {
it
.path("id").isEqualTo(contentClassification.id!!.encodeGlobalId<ContentClassification>())
.path("name").isEqualTo(contentClassification.name)
.path("description").isEqualTo(contentClassification.description)
.path("excludeFromSitemap").isEqualTo(contentClassification.excludeFromSitemap)
} |
I do now realize that I could use something like this: val mapTypeRef = object : ParameterizedTypeReference<Map<String, Any>>() {}
// ...
.path("merchant.contentClassification").entity(mapTypeRef).isEqualTo(
mapOf(
"id" to contentClassification.id!!.encodeGlobalId<ContentClassification>(),
"name" to contentClassification.name,
"description" to contentClassification.description,
"excludeFromSitemap" to contentClassification.excludeFromSitemap,
"metaTags" to listOf(
mapOf("name" to "robots", "content" to "noindex")
)
)
) Which I can make less verbose by adding an extension like this; private val mapTypeRef = object : ParameterizedTypeReference<Map<String, Any>>() {}
internal fun GraphQlTester.Path.isEqualTo(value: Map<String, Any>) =
this.also { entity(mapTypeRef).isEqualTo(value) } And then use like; .path("merchant.contentClassification").isEqualTo(mapOf(...)) But this makes me think that this maybe should be a builtin matcher? Edit: this wouldn't be an exact replacement for the prefix functionality, because this requires all fields to be present in the map. |
We could add support for |
I like this suggestion, because with the extension I proposed above it always has to be an exact match, which is not always desired/useful I realized. |
Path#entityMap
to GraphQlTester
to assert data at given path as a Map
Path#entityMap
to GraphQlTester
to assert data at given path as a Map
Haven't had the chance to try this, but it looks like it works as I proposed in the issue description above, so that's great! |
When testing the response of a nested query it would be practical to perform assertions on a subpath.
To give an example;
This is obviously very verbose, and thus it would like to propose an api where you traverse paths and can perform assertions on a "subpath";
The text was updated successfully, but these errors were encountered: