[1061] Add resolver_match
to the mocked request object in the Ninja's TestClient
#1060
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem
Previously, the Ninja's
TestClient
did not include theresolver_match
in the mocked request object. That's causing issue when having the following setup:The above schema will infer the
id
property from the path. So, assuming you have the following path:the
EntityUpdateSchema
will be populated with theid
received from the path. I find this useful for when we need to do some validation against the object and we need to query the object that we're currently working with.And the good news is - this works perfectly! When I call the endpoint, everything is behaving as it should :chef-kiss:
So, what's the problem?
When writing tests, though, and using the
TestClient
from Ninja, this is not working at all. Doingclient.patch("/entities/123")
will never populate the schema field, and we will get a validation error back, saying the id field is missing.The solution
The solution for this is quite simple - when we build the mock request object, we need to set the
resolver_match
to the ninja resolver that we get back from the URL. By doing that, the schema is again able to infer the id and everything works as it should, even in tests.While I could just create my own TestClient that would inherit from Ninja's test client, and override the
_resolve
method, I assume this would be beneficial to others as well. Hence, creating a PR with the fix in hopes that others will find it useful as well.