Skip to content

Commit 00f2f72

Browse files
author
Ryan P Kilby
committed
Add tests for root urlconf
1 parent ebb92cf commit 00f2f72

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

tests/test_urlconf.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""
2+
Test rest framework assumptions about the configuration of the root urlconf.
3+
"""
4+
from django.conf.urls import include, url
5+
6+
from rest_framework import routers, viewsets
7+
from rest_framework.reverse import NoReverseMatch, reverse
8+
from rest_framework.test import APITestCase, URLPatternsTestCase
9+
10+
11+
class MockViewSet(viewsets.ViewSet):
12+
def list(self, request, *args, **kwargs):
13+
pass
14+
15+
16+
router = routers.DefaultRouter(trailing_slash=False)
17+
router.register(r'example', MockViewSet, base_name='example')
18+
19+
20+
class TestUrlsAppNameRequired(APITestCase, URLPatternsTestCase):
21+
urlpatterns = [
22+
url(r'^api/', include(router.urls)),
23+
]
24+
25+
def test_reverse(self):
26+
"""
27+
The 'rest_framework' namespace must be present.
28+
"""
29+
with self.assertRaises(NoReverseMatch):
30+
reverse('example-list')
31+
32+
33+
class TestUrlpatternsAppName(APITestCase, URLPatternsTestCase):
34+
urlpatterns = [
35+
url(r'^api/', include(router.urlpatterns)),
36+
]
37+
38+
def test_reverse(self):
39+
self.assertEqual(reverse('example-list'), '/api/example')
40+
41+
def test_app_name_reverse(self):
42+
self.assertEqual(reverse('rest_framework:example-list'), '/api/example')
43+
44+
45+
class TestUrlpatternsNamespace(APITestCase, URLPatternsTestCase):
46+
urlpatterns = [
47+
url(r'^api/v1/', include(router.urlpatterns, namespace='v1')),
48+
url(r'^api/v2/', include(router.urlpatterns, namespace='v2')),
49+
]
50+
51+
def test_reverse(self):
52+
self.assertEqual(reverse('example-list'), '/api/v2/example')
53+
54+
def test_app_name_reverse(self):
55+
self.assertEqual(reverse('rest_framework:example-list'), '/api/v2/example')
56+
57+
def test_namespace_reverse(self):
58+
self.assertEqual(reverse('v1:example-list'), '/api/v1/example')
59+
self.assertEqual(reverse('v2:example-list'), '/api/v2/example')
60+
61+
62+
class TestAppUrlpatternsAppName(APITestCase, URLPatternsTestCase):
63+
apppatterns = ([
64+
url(r'^api/', include(router.urlpatterns)),
65+
], 'api')
66+
67+
urlpatterns = [
68+
url(r'^', include(apppatterns)),
69+
]
70+
71+
def test_reverse(self):
72+
"""
73+
Nesting the router.urlpatterns in an app with an app_name will
74+
break url resolution.
75+
"""
76+
with self.assertRaises(NoReverseMatch):
77+
reverse('example-list')
78+
79+
80+
class TestAppUrlpatterns(APITestCase, URLPatternsTestCase):
81+
apppatterns = ([
82+
url(r'^api/', include(router.urlpatterns)),
83+
], None)
84+
85+
urlpatterns = [
86+
url(r'^', include(apppatterns)),
87+
]
88+
89+
def test_reverse(self):
90+
self.assertEqual(reverse('example-list'), '/api/example')
91+
92+
93+
class TestAppUrlsAppName(APITestCase, URLPatternsTestCase):
94+
apppatterns = ([
95+
url(r'^api/', include(router.urls)),
96+
], 'rest_framework')
97+
98+
urlpatterns = [
99+
url(r'^', include(apppatterns)),
100+
]
101+
102+
def test_reverse(self):
103+
self.assertEqual(reverse('example-list'), '/api/example')
104+
105+
def test_app_name_reverse(self):
106+
self.assertEqual(reverse('rest_framework:example-list'), '/api/example')
107+
108+
109+
class TestAppUrlsNamespace(APITestCase, URLPatternsTestCase):
110+
apppatterns = ([
111+
url(r'^', include(router.urls)),
112+
], 'rest_framework')
113+
114+
urlpatterns = [
115+
url(r'^api/v1/', include(apppatterns, namespace='v1')),
116+
url(r'^api/v2/', include(apppatterns, namespace='v2')),
117+
]
118+
119+
def test_reverse(self):
120+
self.assertEqual(reverse('example-list'), '/api/v2/example')
121+
122+
def test_app_name_reverse(self):
123+
self.assertEqual(reverse('rest_framework:example-list'), '/api/v2/example')
124+
125+
def test_namespace_reverse(self):
126+
self.assertEqual(reverse('v1:example-list'), '/api/v1/example')
127+
self.assertEqual(reverse('v2:example-list'), '/api/v2/example')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy