Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor UsingURLPatterns to use override_settings(ROOT_URLCONF=...) …
…style
  • Loading branch information
lovelydinosaur committed Jun 1, 2016
commit 9c83ff44a575f3c1e685011504209b24aeef85dc
30 changes: 26 additions & 4 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +10,28 @@
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):
Expand Down Expand Up @@ -120,7 +142,7 @@ class FakeResolverMatch:
assert response.data == {'version': None}


class TestURLReversing(UsingURLPatterns, APITestCase):
class TestURLReversing(URLPatternsTestCase):
included = [
url(r'^namespaced/$', dummy_view, name='another'),
url(r'^example/(?P<pk>\d+)/$', dummy_pk_view, name='example-detail')
Expand Down Expand Up @@ -238,7 +260,7 @@ class FakeResolverMatch:
assert response.status_code == status.HTTP_404_NOT_FOUND


class TestHyperlinkedRelatedField(UsingURLPatterns, APITestCase):
class TestHyperlinkedRelatedField(URLPatternsTestCase):
included = [
url(r'^namespaced/(?P<pk>\d+)/$', dummy_pk_view, name='namespaced'),
]
Expand Down Expand Up @@ -270,7 +292,7 @@ def test_bug_2489(self):
self.field.to_internal_value('/v2/namespaced/3/')


class TestNamespaceVersioningHyperlinkedRelatedFieldScheme(UsingURLPatterns, APITestCase):
class TestNamespaceVersioningHyperlinkedRelatedFieldScheme(URLPatternsTestCase):
included = [
url(r'^namespaced/(?P<pk>\d+)/$', dummy_pk_view, name='namespaced'),
]
Expand Down
24 changes: 0 additions & 24 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@
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
Expand Down