From c30a336b578cfb819f279c1470e2907916a77095 Mon Sep 17 00:00:00 2001 From: Ryan P Kilby Date: Thu, 21 Dec 2017 23:22:53 -0500 Subject: [PATCH] Cleanup router tests urlpatterns --- tests/test_routers.py | 59 +++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/tests/test_routers.py b/tests/test_routers.py index 5a1cfe8f4..55ccc647b 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -14,7 +14,7 @@ from rest_framework.compat import get_regex_pattern from rest_framework.decorators import detail_route, list_route from rest_framework.response import Response 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 factory = APIRequestFactory() @@ -90,23 +90,10 @@ namespaced_router.register(r'example', MockViewSet, base_name='example') empty_prefix_router = SimpleRouter() 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.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): def list(self, request, *args, **kwargs): @@ -156,8 +143,12 @@ class TestSimpleRouter(TestCase): assert route.mapping[method] == endpoint -@override_settings(ROOT_URLCONF='tests.test_routers') -class TestRootView(TestCase): +class TestRootView(URLPatternsTestCase, 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): response = self.client.get('/namespaced/') assert response.data == {"example": "http://testserver/namespaced/example/"} @@ -167,11 +158,15 @@ class TestRootView(TestCase): assert response.data == {"example": "http://testserver/non-namespaced/example/"} -@override_settings(ROOT_URLCONF='tests.test_routers') -class TestCustomLookupFields(TestCase): +class TestCustomLookupFields(URLPatternsTestCase, TestCase): """ 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): RouterTestModel.objects.create(uuid='123', text='foo bar') RouterTestModel.objects.create(uuid='a b', text='baz qux') @@ -219,12 +214,17 @@ class TestLookupValueRegex(TestCase): @override_settings(ROOT_URLCONF='tests.test_routers') -class TestLookupUrlKwargs(TestCase): +class TestLookupUrlKwargs(URLPatternsTestCase, TestCase): """ Ensure the router honors lookup_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): RouterTestModel.objects.create(uuid='123', text='foo bar') @@ -408,8 +408,11 @@ class TestDynamicListAndDetailRouter(TestCase): self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet) -@override_settings(ROOT_URLCONF='tests.test_routers') -class TestEmptyPrefix(TestCase): +class TestEmptyPrefix(URLPatternsTestCase, TestCase): + urlpatterns = [ + url(r'^empty-prefix/', include(empty_prefix_router.urls)), + ] + def test_empty_prefix_list(self): response = self.client.get('/empty-prefix/') assert response.status_code == 200 @@ -422,8 +425,11 @@ class TestEmptyPrefix(TestCase): assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'} -@override_settings(ROOT_URLCONF='tests.test_routers') -class TestRegexUrlPath(TestCase): +class TestRegexUrlPath(URLPatternsTestCase, TestCase): + urlpatterns = [ + url(r'^regex/', include(regex_url_path_router.urls)), + ] + def test_regex_url_path_list(self): kwarg = '1234' 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} -@override_settings(ROOT_URLCONF='tests.test_routers') -class TestViewInitkwargs(TestCase): +class TestViewInitkwargs(URLPatternsTestCase, TestCase): + urlpatterns = [ + url(r'^example/', include(notes_router.urls)), + ] + def test_suffix(self): match = resolve('/example/notes/') initkwargs = match.func.initkwargs