2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2015-06-11 01:51:33 +03:00
|
|
|
from django.conf.urls import url
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2014-09-19 18:46:32 +04:00
|
|
|
from django.utils import six
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
import rest_framework.utils.model_meta
|
2016-06-02 16:39:10 +03:00
|
|
|
from rest_framework.compat import _resolve_model
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
|
|
|
from rest_framework.views import APIView
|
2014-09-19 18:46:32 +04:00
|
|
|
from tests.models import BasicModel
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
class Root(APIView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceRoot(APIView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceInstance(APIView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NestedResourceRoot(APIView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NestedResourceInstance(APIView):
|
|
|
|
pass
|
|
|
|
|
2014-09-19 18:46:32 +04:00
|
|
|
|
2015-08-14 14:16:57 +03:00
|
|
|
class CustomNameResourceInstance(APIView):
|
|
|
|
def get_view_name(self):
|
|
|
|
return "Foo"
|
|
|
|
|
2014-09-19 18:46:32 +04:00
|
|
|
|
2015-06-11 01:45:23 +03:00
|
|
|
urlpatterns = [
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^$', Root.as_view()),
|
|
|
|
url(r'^resource/$', ResourceRoot.as_view()),
|
2015-08-14 14:16:57 +03:00
|
|
|
url(r'^resource/customname$', CustomNameResourceInstance.as_view()),
|
2012-09-20 16:06:27 +04:00
|
|
|
url(r'^resource/(?P<key>[0-9]+)$', ResourceInstance.as_view()),
|
|
|
|
url(r'^resource/(?P<key>[0-9]+)/$', NestedResourceRoot.as_view()),
|
|
|
|
url(r'^resource/(?P<key>[0-9]+)/(?P<other>[A-Za-z]+)$', NestedResourceInstance.as_view()),
|
2015-06-11 01:45:23 +03:00
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_utils')
|
2012-09-20 16:06:27 +04:00
|
|
|
class BreadcrumbTests(TestCase):
|
2014-09-19 18:46:32 +04:00
|
|
|
"""
|
|
|
|
Tests the breadcrumb functionality used by the HTML renderer.
|
|
|
|
"""
|
2012-09-20 16:06:27 +04:00
|
|
|
def test_root_breadcrumbs(self):
|
|
|
|
url = '/'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[('Root', '/')]
|
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_resource_root_breadcrumbs(self):
|
|
|
|
url = '/resource/'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[
|
|
|
|
('Root', '/'),
|
|
|
|
('Resource Root', '/resource/')
|
|
|
|
]
|
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_resource_instance_breadcrumbs(self):
|
|
|
|
url = '/resource/123'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[
|
|
|
|
('Root', '/'),
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
('Resource Instance', '/resource/123')
|
|
|
|
]
|
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2015-08-14 14:16:57 +03:00
|
|
|
def test_resource_instance_customname_breadcrumbs(self):
|
|
|
|
url = '/resource/customname'
|
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[
|
|
|
|
('Root', '/'),
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
('Foo', '/resource/customname')
|
|
|
|
]
|
2015-08-14 14:20:25 +03:00
|
|
|
)
|
2015-08-14 14:16:57 +03:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def test_nested_resource_breadcrumbs(self):
|
|
|
|
url = '/resource/123/'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[
|
|
|
|
('Root', '/'),
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
('Resource Instance', '/resource/123'),
|
|
|
|
('Nested Resource Root', '/resource/123/')
|
|
|
|
]
|
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_nested_resource_instance_breadcrumbs(self):
|
|
|
|
url = '/resource/123/abc'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[
|
|
|
|
('Root', '/'),
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
('Resource Instance', '/resource/123'),
|
|
|
|
('Nested Resource Root', '/resource/123/'),
|
|
|
|
('Nested Resource Instance', '/resource/123/abc')
|
|
|
|
]
|
|
|
|
)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
def test_broken_url_breadcrumbs_handled_gracefully(self):
|
|
|
|
url = '/foobar'
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
get_breadcrumbs(url),
|
|
|
|
[('Root', '/')]
|
|
|
|
)
|
2014-09-19 18:46:32 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ResolveModelTests(TestCase):
|
|
|
|
"""
|
|
|
|
`_resolve_model` should return a Django model class given the
|
|
|
|
provided argument is a Django model class itself, or a properly
|
|
|
|
formatted string representation of one.
|
|
|
|
"""
|
|
|
|
def test_resolve_django_model(self):
|
|
|
|
resolved_model = _resolve_model(BasicModel)
|
|
|
|
self.assertEqual(resolved_model, BasicModel)
|
|
|
|
|
|
|
|
def test_resolve_string_representation(self):
|
|
|
|
resolved_model = _resolve_model('tests.BasicModel')
|
|
|
|
self.assertEqual(resolved_model, BasicModel)
|
|
|
|
|
|
|
|
def test_resolve_unicode_representation(self):
|
|
|
|
resolved_model = _resolve_model(six.text_type('tests.BasicModel'))
|
|
|
|
self.assertEqual(resolved_model, BasicModel)
|
|
|
|
|
|
|
|
def test_resolve_non_django_model(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
_resolve_model(TestCase)
|
|
|
|
|
|
|
|
def test_resolve_improper_string_representation(self):
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
_resolve_model('BasicModel')
|
2014-11-18 09:26:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
class ResolveModelWithPatchedDjangoTests(TestCase):
|
|
|
|
"""
|
|
|
|
Test coverage for when Django's `get_model` returns `None`.
|
|
|
|
|
|
|
|
Under certain circumstances Django may return `None` with `get_model`:
|
|
|
|
http://git.io/get-model-source
|
|
|
|
|
|
|
|
It usually happens with circular imports so it is important that DRF
|
|
|
|
excepts early, otherwise fault happens downstream and is much more
|
|
|
|
difficult to debug.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Monkeypatch get_model."""
|
2016-06-02 16:39:10 +03:00
|
|
|
self.get_model = rest_framework.compat.apps.get_model
|
2014-11-18 09:26:23 +03:00
|
|
|
|
|
|
|
def get_model(app_label, model_name):
|
|
|
|
return None
|
|
|
|
|
2016-06-02 16:39:10 +03:00
|
|
|
rest_framework.compat.apps.get_model = get_model
|
2014-11-18 09:26:23 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Revert monkeypatching."""
|
2016-06-02 16:39:10 +03:00
|
|
|
rest_framework.compat.apps.get_model = self.get_model
|
2014-11-18 09:26:23 +03:00
|
|
|
|
|
|
|
def test_blows_up_if_model_does_not_resolve(self):
|
2014-11-28 18:36:04 +03:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
2014-11-18 09:26:23 +03:00
|
|
|
_resolve_model('tests.BasicModel')
|