Skip to content

Commit

Permalink
Added two different url registration to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliypopel committed Sep 26, 2024
1 parent 0c26090 commit c0327ab
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 25 deletions.
33 changes: 29 additions & 4 deletions tests/__main__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
50 changes: 38 additions & 12 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
# 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 '<host:port>/test/'
include((
[
path('home/', home, name='home'),
path('', HomeRedirectView.as_view(), name='home_redirect'),
path('<slug:name>/', HelloNameView.as_view(), name='hello_name'),
path('home/', home, name='home'), # '<host:port>/test/home/'
path('', HomeRedirectView.as_view(), name='home_redirect'), # '<host:port>/test/'
path('<slug:name>/', HelloNameView.as_view(), name='hello_name'), # '<host:port>/test/<slug:name>/'
], # urlpatterns
'test', # app_name
))
),
]


# 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 '<host:port>/test'
include((
[
path('/home', home, name='home'), # '<host:port>/test/home'
path('', HomeRedirectView.as_view(), name='home_redirect'), # '<host:port>/test'
path('/<slug:name>', HelloNameView.as_view(), name='hello_name'), # '<host:port>/test/<slug:name>'
], # 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),
]
30 changes: 21 additions & 9 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', # <host:port>/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', # <host:port>/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') # <host:port>/test/home/
@routify_without_trailing.route('/home') # <host:port>/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('/') # <host:port>/test/
@routify_without_trailing.route('') # <host:port>/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 '<slug:name>/'
@router.route('/<slug:name>')
# Register HelloWorldView with auto_naming 'hello_name'
@router_with_trailing.route('/<slug:name>') # <host:port>/test/<slug:name>/
@routify_without_trailing.route('/<slug:name>') # <host:port>/test/<slug:name>
class HelloNameView(View):
def get(self, request: HttpRequest, name: str) -> HttpResponse:
return HttpResponse(f'Hello, {name.capitalize()}!')

0 comments on commit c0327ab

Please sign in to comment.