Skip to content

Commit

Permalink
fix: 'str' object has no attribute 'route' in RequestRouteMiddleware.…
Browse files Browse the repository at this point in the history
…process_view
  • Loading branch information
ecarrara committed Dec 8, 2023
1 parent f0d9020 commit 1a49163
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions django_query_prefixer/middlewares/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django_query_prefixer import set_prefix, remove_prefix
from django_query_prefixer import remove_prefix, set_prefix


class RequestRouteMiddleware:
Expand All @@ -13,7 +13,7 @@ def __call__(self, request):

def process_view(self, request, view_func, view_args, view_kwargs):
set_prefix(key="view_name", value=f"{view_func.__module__}.{view_func.__name__}")
set_prefix(key="route", value=escape_comment_markers(request.resolver_match.route.route))
set_prefix(key="route", value=escape_comment_markers(request.resolver_match.route))


def request_route(get_response):
Expand Down
21 changes: 13 additions & 8 deletions tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_request_route_middleware():
middleware = request_route(get_response=get_response)

request = mock.MagicMock()
request.resolver_match.route.route = "/hello"
request.resolver_match.route = "/hello"

def hello_world():
pass
Expand All @@ -20,10 +20,15 @@ def hello_world():
middleware.process_view(request, hello_world, [], {})
assert middleware(request) == response

assert mock_set_prefix.call_args_list[0].kwargs == {
"key": "view_name",
"value": f"{hello_world.__module__}.hello_world",
}
assert mock_set_prefix.call_args_list[1].kwargs == {"key": "route", "value": "/hello"}
assert mock_remove_prefix.call_args_list[0].args[0] == "view_name"
assert mock_remove_prefix.call_args_list[1].args[0] == "route"
mock_set_prefix.assert_has_calls(
[
mock.call(key="view_name", value=f"test_middlewares.hello_world"),
mock.call(key="route", value=f"/hello"),
]
)
mock_remove_prefix.assert_has_calls(
[
mock.call("view_name"),
mock.call("route"),
],
)

0 comments on commit 1a49163

Please sign in to comment.