aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois2015-03-13 01:07:20 +0100
committerRaphaël Barrois2015-03-13 01:07:20 +0100
commit8d0dbc8092a754e1f0f7d80d93506072556f35a2 (patch)
treeacf473ed37fce19c8f62b4d365d0b67d564d49f8 /tests
parenta02098b855aad0847cddacc4bed2d280017b46c5 (diff)
downloaddjango-rest-framework-8d0dbc8092a754e1f0f7d80d93506072556f35a2.tar.bz2
Fix lookup_url_kwarg handling in viewsets.
The ``lookup_url_kwarg`` is intended to set the name of a field in the URL regexps when using custom ``lookup_field``, but the routers ignore it altogether.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_routers.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/test_routers.py b/tests/test_routers.py
index 08c58ec7..19eeb868 100644
--- a/tests/test_routers.py
+++ b/tests/test_routers.py
@@ -32,6 +32,13 @@ class NoteViewSet(viewsets.ModelViewSet):
lookup_field = 'uuid'
+class KWargedNoteViewSet(viewsets.ModelViewSet):
+ queryset = RouterTestModel.objects.all()
+ serializer_class = NoteSerializer
+ lookup_field = 'text__contains'
+ lookup_url_kwarg = 'text'
+
+
class MockViewSet(viewsets.ModelViewSet):
queryset = None
serializer_class = None
@@ -40,6 +47,9 @@ class MockViewSet(viewsets.ModelViewSet):
notes_router = SimpleRouter()
notes_router.register(r'notes', NoteViewSet)
+kwarged_notes_router = SimpleRouter()
+kwarged_notes_router.register(r'notes', KWargedNoteViewSet)
+
namespaced_router = DefaultRouter()
namespaced_router.register(r'example', MockViewSet, base_name='example')
@@ -47,6 +57,7 @@ urlpatterns = [
url(r'^non-namespaced/', include(namespaced_router.urls)),
url(r'^namespaced/', include(namespaced_router.urls, namespace='example')),
url(r'^example/', include(notes_router.urls)),
+ url(r'^example2/', include(kwarged_notes_router.urls)),
]
@@ -177,6 +188,33 @@ class TestLookupValueRegex(TestCase):
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)
+class TestLookupUrlKwargs(TestCase):
+ """
+ Ensure the router honors lookup_url_kwarg.
+
+ Setup a deep lookup_field, but map it to a simple URL kwarg.
+ """
+ urls = 'tests.test_routers'
+
+ def setUp(self):
+ RouterTestModel.objects.create(uuid='123', text='foo bar')
+
+ def test_custom_lookup_url_kwarg_route(self):
+ detail_route = kwarged_notes_router.urls[-1]
+ detail_url_pattern = detail_route.regex.pattern
+ self.assertIn('^notes/(?P<text>', detail_url_pattern)
+
+ def test_retrieve_lookup_url_kwarg_detail_view(self):
+ response = self.client.get('/example2/notes/fo/')
+ self.assertEqual(
+ response.data,
+ {
+ "url": "http://testserver/example/notes/123/",
+ "uuid": "123", "text": "foo bar"
+ }
+ )
+
+
class TestTrailingSlashIncluded(TestCase):
def setUp(self):
class NoteViewSet(viewsets.ModelViewSet):