mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Reorganize tests.
This commit is contained in:
parent
f3067a7fab
commit
030f01afdb
|
@ -1,27 +1,11 @@
|
|||
from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset
|
||||
from django.conf.urls import patterns, url, include
|
||||
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.datastructures import MultiValueDict
|
||||
from rest_framework import serializers
|
||||
from rest_framework.fields import empty
|
||||
from rest_framework.test import APISimpleTestCase, APIRequestFactory
|
||||
from rest_framework.versioning import NamespaceVersioning
|
||||
from rest_framework.test import APISimpleTestCase
|
||||
import pytest
|
||||
|
||||
factory = APIRequestFactory()
|
||||
|
||||
dummy_view = lambda request, pk: None
|
||||
|
||||
included_patterns = [
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
]
|
||||
|
||||
urlpatterns = patterns(
|
||||
'',
|
||||
url(r'^v1/', include(included_patterns, namespace='v1')),
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
)
|
||||
|
||||
|
||||
class TestStringRelatedField(APISimpleTestCase):
|
||||
def setUp(self):
|
||||
|
@ -64,35 +48,6 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase):
|
|||
assert representation == self.instance.pk
|
||||
|
||||
|
||||
class TestHyperlinkedRelatedField(APISimpleTestCase):
|
||||
urls = 'tests.test_relations'
|
||||
|
||||
def setUp(self):
|
||||
class HyperlinkedMockQueryset(MockQueryset):
|
||||
def get(self, **lookup):
|
||||
for item in self.items:
|
||||
if item.pk == int(lookup.get('pk', -1)):
|
||||
return item
|
||||
raise ObjectDoesNotExist()
|
||||
|
||||
self.queryset = HyperlinkedMockQueryset([
|
||||
MockObject(pk=1, name='foo'),
|
||||
MockObject(pk=2, name='bar'),
|
||||
MockObject(pk=3, name='baz')
|
||||
])
|
||||
self.field = serializers.HyperlinkedRelatedField(
|
||||
view_name='example-detail',
|
||||
queryset=self.queryset
|
||||
)
|
||||
request = factory.post('/')
|
||||
request.versioning_scheme = NamespaceVersioning()
|
||||
self.field._context = {'request': request}
|
||||
|
||||
def test_bug_2489(self):
|
||||
self.field.to_internal_value('/example/3/')
|
||||
self.field.to_internal_value('/v1/example/3/')
|
||||
|
||||
|
||||
class TestHyperlinkedIdentityField(APISimpleTestCase):
|
||||
def setUp(self):
|
||||
self.instance = MockObject(pk=1, name='foo')
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
from .utils import MockObject, MockQueryset
|
||||
from django.conf.urls import include, url
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from rest_framework import serializers
|
||||
from rest_framework import status, versioning
|
||||
from rest_framework.decorators import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.reverse import reverse
|
||||
from rest_framework.test import APIRequestFactory, APITestCase
|
||||
from rest_framework.test import APIRequestFactory, APITestCase, APISimpleTestCase
|
||||
from rest_framework.versioning import NamespaceVersioning
|
||||
|
||||
|
||||
class RequestVersionView(APIView):
|
||||
|
@ -29,15 +33,18 @@ class RequestInvalidVersionView(APIView):
|
|||
factory = APIRequestFactory()
|
||||
|
||||
mock_view = lambda request: None
|
||||
dummy_view = lambda request, pk: None
|
||||
|
||||
included_patterns = [
|
||||
url(r'^namespaced/$', mock_view, name='another'),
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
]
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^v1/', include(included_patterns, namespace='v1')),
|
||||
url(r'^another/$', mock_view, name='another'),
|
||||
url(r'^(?P<version>[^/]+)/another/$', mock_view, name='another')
|
||||
url(r'^(?P<version>[^/]+)/another/$', mock_view, name='another'),
|
||||
url(r'^example/(?P<pk>\d+)/$', dummy_view, name='example-detail')
|
||||
]
|
||||
|
||||
|
||||
|
@ -221,3 +228,33 @@ class TestInvalidVersion:
|
|||
request.resolver_match = FakeResolverMatch
|
||||
response = view(request, version='v3')
|
||||
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||
|
||||
|
||||
class TestHyperlinkedRelatedField(APISimpleTestCase):
|
||||
urls = 'tests.test_versioning'
|
||||
|
||||
def setUp(self):
|
||||
|
||||
class HyperlinkedMockQueryset(MockQueryset):
|
||||
def get(self, **lookup):
|
||||
for item in self.items:
|
||||
if item.pk == int(lookup.get('pk', -1)):
|
||||
return item
|
||||
raise ObjectDoesNotExist()
|
||||
|
||||
self.queryset = HyperlinkedMockQueryset([
|
||||
MockObject(pk=1, name='foo'),
|
||||
MockObject(pk=2, name='bar'),
|
||||
MockObject(pk=3, name='baz')
|
||||
])
|
||||
self.field = serializers.HyperlinkedRelatedField(
|
||||
view_name='example-detail',
|
||||
queryset=self.queryset
|
||||
)
|
||||
request = factory.post('/', urlconf='tests.test_versioning')
|
||||
request.versioning_scheme = NamespaceVersioning()
|
||||
self.field._context = {'request': request}
|
||||
|
||||
def test_bug_2489(self):
|
||||
self.field.to_internal_value('/example/3/')
|
||||
self.field.to_internal_value('/v1/example/3/')
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""
|
||||
Blank URLConf just to keep the test suite happy
|
||||
"""
|
||||
from tests import test_relations
|
||||
from django.conf.urls import patterns
|
||||
|
||||
urlpatterns = test_relations.urlpatterns
|
||||
urlpatterns = patterns('')
|
||||
|
|
Loading…
Reference in New Issue
Block a user