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

[1061] Add resolver_match to the mocked request object in the Ninja's TestClient #1060

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions ninja/testing/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,17 @@ def _resolve(
for url in self.urls:
match = url.resolve(url_path)
if match:
request = self._build_request(method, path, data, request_params)
request = self._build_request(method, path, data, request_params, match)
return match.func, request, match.kwargs
raise Exception(f'Cannot resolve "{path}"')

def _build_request(
self, method: str, path: str, data: Dict, request_params: Any
self,
method: str,
path: str,
data: Dict,
request_params: Any,
resolver_match: Any,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could probably be better typed, but honestly, since it's used internally and not exposed as the external testing client's API, I was personally fine with Any and be done with it.

) -> Mock:
request = Mock()
request.method = method
Expand All @@ -119,6 +124,7 @@ def _build_request(
request._dont_enforce_csrf_checks = True
request.is_secure.return_value = False
request.build_absolute_uri = build_absolute_uri
request.resolver_match = resolver_match

if "user" not in request_params:
request.user.is_authenticated = False
Expand Down
23 changes: 23 additions & 0 deletions tests/test_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
router = Router()


class ClientTestSchemaWithResolver(Schema):
id: int

@staticmethod
def resolve_id(obj, context):
return context["request"].resolver_match.kwargs.get("id")


@router.get("/request/build_absolute_uri")
def request_build_absolute_uri(request):
return request.build_absolute_uri()
Expand All @@ -27,6 +35,12 @@ def simple_get(request):
return "test"


@router.post("/test/{id}")
def simple_post_with_path_arg(request, id: int, data: ClientTestSchemaWithResolver):
assert id == data.id
return data


client = TestClient(router)


Expand Down Expand Up @@ -78,3 +92,12 @@ def test_json_as_body():
ClientTestSchema.model_validate_json(request.body).model_dump_json()
== schema_instance.model_dump_json()
)


def test_request_resolver_match():
with mock.patch.object(client, "_call") as call:
test_id = "123"
client.post(f"/test/{test_id}")
request = call.call_args[0][1]

assert request.resolver_match.kwargs.get("id") == test_id