mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 20:10:10 +03:00
Cleanup router tests urlpatterns
This commit is contained in:
parent
44c2a5ce39
commit
c30a336b57
|
@ -14,7 +14,7 @@ from rest_framework.compat import get_regex_pattern
|
||||||
from rest_framework.decorators import detail_route, list_route
|
from rest_framework.decorators import detail_route, list_route
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.routers import DefaultRouter, SimpleRouter
|
from rest_framework.routers import DefaultRouter, SimpleRouter
|
||||||
from rest_framework.test import APIRequestFactory
|
from rest_framework.test import APIRequestFactory, URLPatternsTestCase
|
||||||
from rest_framework.utils import json
|
from rest_framework.utils import json
|
||||||
|
|
||||||
factory = APIRequestFactory()
|
factory = APIRequestFactory()
|
||||||
|
@ -90,23 +90,10 @@ namespaced_router.register(r'example', MockViewSet, base_name='example')
|
||||||
|
|
||||||
empty_prefix_router = SimpleRouter()
|
empty_prefix_router = SimpleRouter()
|
||||||
empty_prefix_router.register(r'', EmptyPrefixViewSet, base_name='empty_prefix')
|
empty_prefix_router.register(r'', EmptyPrefixViewSet, base_name='empty_prefix')
|
||||||
empty_prefix_urls = [
|
|
||||||
url(r'^', include(empty_prefix_router.urls)),
|
|
||||||
]
|
|
||||||
|
|
||||||
regex_url_path_router = SimpleRouter()
|
regex_url_path_router = SimpleRouter()
|
||||||
regex_url_path_router.register(r'', RegexUrlPathViewSet, base_name='regex')
|
regex_url_path_router.register(r'', RegexUrlPathViewSet, base_name='regex')
|
||||||
|
|
||||||
urlpatterns = [
|
|
||||||
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
|
||||||
url(r'^namespaced/', include((namespaced_router.urls, 'example'), namespace='example')),
|
|
||||||
url(r'^example/', include(notes_router.urls)),
|
|
||||||
url(r'^example2/', include(kwarged_notes_router.urls)),
|
|
||||||
|
|
||||||
url(r'^empty-prefix/', include(empty_prefix_urls)),
|
|
||||||
url(r'^regex/', include(regex_url_path_router.urls))
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class BasicViewSet(viewsets.ViewSet):
|
class BasicViewSet(viewsets.ViewSet):
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
|
@ -156,8 +143,12 @@ class TestSimpleRouter(TestCase):
|
||||||
assert route.mapping[method] == endpoint
|
assert route.mapping[method] == endpoint
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
class TestRootView(URLPatternsTestCase, TestCase):
|
||||||
class TestRootView(TestCase):
|
urlpatterns = [
|
||||||
|
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
||||||
|
url(r'^namespaced/', include((namespaced_router.urls, 'namespaced'), namespace='namespaced')),
|
||||||
|
]
|
||||||
|
|
||||||
def test_retrieve_namespaced_root(self):
|
def test_retrieve_namespaced_root(self):
|
||||||
response = self.client.get('/namespaced/')
|
response = self.client.get('/namespaced/')
|
||||||
assert response.data == {"example": "http://testserver/namespaced/example/"}
|
assert response.data == {"example": "http://testserver/namespaced/example/"}
|
||||||
|
@ -167,11 +158,15 @@ class TestRootView(TestCase):
|
||||||
assert response.data == {"example": "http://testserver/non-namespaced/example/"}
|
assert response.data == {"example": "http://testserver/non-namespaced/example/"}
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
class TestCustomLookupFields(URLPatternsTestCase, TestCase):
|
||||||
class TestCustomLookupFields(TestCase):
|
|
||||||
"""
|
"""
|
||||||
Ensure that custom lookup fields are correctly routed.
|
Ensure that custom lookup fields are correctly routed.
|
||||||
"""
|
"""
|
||||||
|
urlpatterns = [
|
||||||
|
url(r'^example/', include(notes_router.urls)),
|
||||||
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
||||||
RouterTestModel.objects.create(uuid='a b', text='baz qux')
|
RouterTestModel.objects.create(uuid='a b', text='baz qux')
|
||||||
|
@ -219,12 +214,17 @@ class TestLookupValueRegex(TestCase):
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
@override_settings(ROOT_URLCONF='tests.test_routers')
|
||||||
class TestLookupUrlKwargs(TestCase):
|
class TestLookupUrlKwargs(URLPatternsTestCase, TestCase):
|
||||||
"""
|
"""
|
||||||
Ensure the router honors lookup_url_kwarg.
|
Ensure the router honors lookup_url_kwarg.
|
||||||
|
|
||||||
Setup a deep lookup_field, but map it to a simple URL kwarg.
|
Setup a deep lookup_field, but map it to a simple URL kwarg.
|
||||||
"""
|
"""
|
||||||
|
urlpatterns = [
|
||||||
|
url(r'^example/', include(notes_router.urls)),
|
||||||
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
||||||
|
|
||||||
|
@ -408,8 +408,11 @@ class TestDynamicListAndDetailRouter(TestCase):
|
||||||
self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet)
|
self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet)
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
class TestEmptyPrefix(URLPatternsTestCase, TestCase):
|
||||||
class TestEmptyPrefix(TestCase):
|
urlpatterns = [
|
||||||
|
url(r'^empty-prefix/', include(empty_prefix_router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
def test_empty_prefix_list(self):
|
def test_empty_prefix_list(self):
|
||||||
response = self.client.get('/empty-prefix/')
|
response = self.client.get('/empty-prefix/')
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
@ -422,8 +425,11 @@ class TestEmptyPrefix(TestCase):
|
||||||
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
class TestRegexUrlPath(URLPatternsTestCase, TestCase):
|
||||||
class TestRegexUrlPath(TestCase):
|
urlpatterns = [
|
||||||
|
url(r'^regex/', include(regex_url_path_router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
def test_regex_url_path_list(self):
|
def test_regex_url_path_list(self):
|
||||||
kwarg = '1234'
|
kwarg = '1234'
|
||||||
response = self.client.get('/regex/list/{}/'.format(kwarg))
|
response = self.client.get('/regex/list/{}/'.format(kwarg))
|
||||||
|
@ -438,8 +444,11 @@ class TestRegexUrlPath(TestCase):
|
||||||
assert json.loads(response.content.decode('utf-8')) == {'pk': pk, 'kwarg': kwarg}
|
assert json.loads(response.content.decode('utf-8')) == {'pk': pk, 'kwarg': kwarg}
|
||||||
|
|
||||||
|
|
||||||
@override_settings(ROOT_URLCONF='tests.test_routers')
|
class TestViewInitkwargs(URLPatternsTestCase, TestCase):
|
||||||
class TestViewInitkwargs(TestCase):
|
urlpatterns = [
|
||||||
|
url(r'^example/', include(notes_router.urls)),
|
||||||
|
]
|
||||||
|
|
||||||
def test_suffix(self):
|
def test_suffix(self):
|
||||||
match = resolve('/example/notes/')
|
match = resolve('/example/notes/')
|
||||||
initkwargs = match.func.initkwargs
|
initkwargs = match.func.initkwargs
|
||||||
|
|
Loading…
Reference in New Issue
Block a user