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

Runs linter #9

Merged
merged 1 commit into from
Dec 7, 2023
Merged
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: 3 additions & 7 deletions django_query_prefixer/middlewares/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from django.urls import resolve
from django_query_prefixer import set_prefix, remove_prefix


class RequestRouteMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
Expand All @@ -14,10 +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.route))


def request_route(get_response):
Expand Down
11 changes: 8 additions & 3 deletions tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ def test_request_route_middleware():

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

def hello_world():
pass

with mock.patch("django_query_prefixer.middlewares.set_prefix") as mock_set_prefix, \
mock.patch("django_query_prefixer.middlewares.remove_prefix") as mock_remove_prefix:
with mock.patch("django_query_prefixer.middlewares.set_prefix") as mock_set_prefix, mock.patch(
"django_query_prefixer.middlewares.remove_prefix"
) as mock_remove_prefix:
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[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"