From 9c83ff44a575f3c1e685011504209b24aeef85dc Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 1 Jun 2016 12:26:06 +0100 Subject: [PATCH] Refactor UsingURLPatterns to use override_settings(ROOT_URLCONF=...) style --- tests/test_versioning.py | 30 ++++++++++++++++++++++++++---- tests/utils.py | 24 ------------------------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/tests/test_versioning.py b/tests/test_versioning.py index 4029e0e43..d4d8cfccb 100644 --- a/tests/test_versioning.py +++ b/tests/test_versioning.py @@ -1,5 +1,6 @@ import pytest from django.conf.urls import include, url +from django.test import override_settings from rest_framework import serializers, status, versioning from rest_framework.decorators import APIView @@ -9,7 +10,28 @@ from rest_framework.reverse import reverse from rest_framework.test import APIRequestFactory, APITestCase from rest_framework.versioning import NamespaceVersioning -from .utils import UsingURLPatterns + +@override_settings(ROOT_URLCONF='tests.test_versioning') +class URLPatternsTestCase(APITestCase): + """ + Isolates URL patterns used during testing on the test class itself. + For example: + + class MyTestCase(URLPatternsTestCase): + urlpatterns = [ + ... + ] + + def test_something(self): + ... + """ + def setUp(self): + global urlpatterns + urlpatterns = self.urlpatterns + + def tearDown(self): + global urlpatterns + urlpatterns = [] class RequestVersionView(APIView): @@ -120,7 +142,7 @@ class TestRequestVersion: assert response.data == {'version': None} -class TestURLReversing(UsingURLPatterns, APITestCase): +class TestURLReversing(URLPatternsTestCase): included = [ url(r'^namespaced/$', dummy_view, name='another'), url(r'^example/(?P\d+)/$', dummy_pk_view, name='example-detail') @@ -238,7 +260,7 @@ class TestInvalidVersion: assert response.status_code == status.HTTP_404_NOT_FOUND -class TestHyperlinkedRelatedField(UsingURLPatterns, APITestCase): +class TestHyperlinkedRelatedField(URLPatternsTestCase): included = [ url(r'^namespaced/(?P\d+)/$', dummy_pk_view, name='namespaced'), ] @@ -270,7 +292,7 @@ class TestHyperlinkedRelatedField(UsingURLPatterns, APITestCase): self.field.to_internal_value('/v2/namespaced/3/') -class TestNamespaceVersioningHyperlinkedRelatedFieldScheme(UsingURLPatterns, APITestCase): +class TestNamespaceVersioningHyperlinkedRelatedFieldScheme(URLPatternsTestCase): included = [ url(r'^namespaced/(?P\d+)/$', dummy_pk_view, name='namespaced'), ] diff --git a/tests/utils.py b/tests/utils.py index b90349967..5b2d75864 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -2,30 +2,6 @@ from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import NoReverseMatch -class UsingURLPatterns(object): - """ - Isolates URL patterns used during testing on the test class itself. - For example: - - class MyTestCase(UsingURLPatterns, TestCase): - urlpatterns = [ - ... - ] - - def test_something(self): - ... - """ - urls = __name__ - - def setUp(self): - global urlpatterns - urlpatterns = self.urlpatterns - - def tearDown(self): - global urlpatterns - urlpatterns = [] - - class MockObject(object): def __init__(self, **kwargs): self._kwargs = kwargs