From c0327ab0b954e102705eb6f6cfbcffe0e2559026 Mon Sep 17 00:00:00 2001 From: vitaliypopel Date: Thu, 26 Sep 2024 14:30:28 +0300 Subject: [PATCH] Added two different url registration to tests --- tests/__main__.py | 33 +++++++++++++++++++++++++++---- tests/urls.py | 50 +++++++++++++++++++++++++++++++++++------------ tests/views.py | 30 +++++++++++++++++++--------- 3 files changed, 88 insertions(+), 25 deletions(-) diff --git a/tests/__main__.py b/tests/__main__.py index aa64c60..587a2f0 100644 --- a/tests/__main__.py +++ b/tests/__main__.py @@ -1,14 +1,19 @@ import unittest -from .urls import default_urlpatterns, routify_urlpatterns +from .urls import ( + default_with_trailing_urlpatterns, + routify_with_trailing_urlpatterns, + default_without_trailing_urlpatterns, + routify_without_trailing_urlpatterns, +) class DjangoRoutifyTests(unittest.TestCase): - def test(self): + def test_urls_with_trailing_slash(self): # If default urlpatterns != routify urlpatterns test will be failed. for default_urls_obj, routify_urls_obj in zip( - default_urlpatterns, - routify_urlpatterns, + default_with_trailing_urlpatterns, + routify_with_trailing_urlpatterns, ): for default_url, routify_url in zip( default_urls_obj.url_patterns, @@ -25,6 +30,26 @@ def test(self): # Checking if callbacks are the same is a bad idea, # because objects cannot be the same + def test_urls_without_trailing_slash(self): + for default_urls_obj, routify_urls_obj in zip( + default_without_trailing_urlpatterns, + routify_without_trailing_urlpatterns, + ): + for default_url, routify_url in zip( + default_urls_obj.url_patterns, + routify_urls_obj.url_patterns, + ): + self.assertEqual( + str(default_url.pattern), + str(routify_url.pattern), + ) + self.assertEqual( + default_url.pattern.name, + routify_url.pattern.name, + ) + # Checking if callbacks are the same is a bad idea, + # because objects cannot be the same + if __name__ == '__main__': # Run test diff --git a/tests/urls.py b/tests/urls.py index 7f6cca4..0f5d37a 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -1,15 +1,22 @@ -# Default Django's url registration +from django_routify import include_router from django.urls import path, include -from .views import home, HomeRedirectView, HelloNameView +from .views import ( + home, + HomeRedirectView, + HelloNameView, + router_with_trailing, + routify_without_trailing, +) -default_urlpatterns = [ +# Default Django's url registration with trailing slash +default_with_trailing_urlpatterns = [ path( - 'test/', # url prefix + 'test/', # url prefix '/test/' include(( [ - path('home/', home, name='home'), - path('', HomeRedirectView.as_view(), name='home_redirect'), - path('/', HelloNameView.as_view(), name='hello_name'), + path('home/', home, name='home'), # '/test/home/' + path('', HomeRedirectView.as_view(), name='home_redirect'), # '/test/' + path('/', HelloNameView.as_view(), name='hello_name'), # '/test//' ], # urlpatterns 'test', # app_name )) @@ -17,10 +24,29 @@ ] -# Django Routify url registration -from django_routify import include_router -from .views import router +# Django Routify url registration with trailing slash +routify_with_trailing_urlpatterns = [ + include_router(router_with_trailing), +] + + +# Default Django's url registration with trailing slash +default_without_trailing_urlpatterns = [ + path( + 'test', # url prefix '/test' + include(( + [ + path('/home', home, name='home'), # '/test/home' + path('', HomeRedirectView.as_view(), name='home_redirect'), # '/test' + path('/', HelloNameView.as_view(), name='hello_name'), # '/test/' + ], # urlpatterns + 'test', # app_name + )) + ), +] + -routify_urlpatterns = [ - include_router(router), +# Django Routify url registration with trailing slash +routify_without_trailing_urlpatterns = [ + include_router(routify_without_trailing), ] diff --git a/tests/views.py b/tests/views.py index 0f1932a..8784ad2 100644 --- a/tests/views.py +++ b/tests/views.py @@ -5,28 +5,40 @@ from django_routify import Router # Initializing new Router with needed settings -router = Router( - prefix='/test', +router_with_trailing = Router( + prefix='/test', # /test/ app_name='test', - auto_trailing_slash=True + auto_trailing_slash=True, + # auto_trailing_slash will remove not needed slashes + # before and after url_path and prefix, + # and will add slash to the end +) + +routify_without_trailing = Router( + prefix='/test', # /test + app_name='test', + # auto_trailing_slash by default equal False ) -# Register home view function with auto_naming 'home' and with url 'home/' -@router.route('/home') +# Register home view function with auto_naming 'home' +@router_with_trailing.route('/home') # /test/home/ +@routify_without_trailing.route('/home') # /test/home def home(request: HttpRequest) -> HttpResponse: return HttpResponse('Hello, World!') -# Register HomeRedirectView with auto_naming 'home_redirect' and with url '' -@router.route('/') +# Register HomeRedirectView with auto_naming 'home_redirect' +@router_with_trailing.route('/') # /test/ +@routify_without_trailing.route('') # /test class HomeRedirectView(RedirectView): def get_redirect_url(self, *args, **kwargs): return reverse('test:home') -# Register HelloWorldView with auto_naming 'hello_name' and with url '/' -@router.route('/') +# Register HelloWorldView with auto_naming 'hello_name' +@router_with_trailing.route('/') # /test// +@routify_without_trailing.route('/') # /test/ class HelloNameView(View): def get(self, request: HttpRequest, name: str) -> HttpResponse: return HttpResponse(f'Hello, {name.capitalize()}!')