2013-05-12 06:26:34 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-05-31 14:50:37 +04:00
|
|
|
from django.db import models
|
2013-05-12 06:26:34 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.test.client import RequestFactory
|
2013-05-31 14:50:37 +04:00
|
|
|
from rest_framework import serializers, viewsets
|
|
|
|
from rest_framework.compat import include, patterns, url
|
2013-05-12 06:26:34 +04:00
|
|
|
from rest_framework.decorators import link, action
|
2013-05-31 14:50:37 +04:00
|
|
|
from rest_framework.response import Response
|
2013-05-12 06:26:34 +04:00
|
|
|
from rest_framework.routers import SimpleRouter
|
|
|
|
|
|
|
|
factory = RequestFactory()
|
|
|
|
|
2013-05-31 14:50:37 +04:00
|
|
|
urlpatterns = patterns('',)
|
|
|
|
|
2013-05-12 06:26:34 +04:00
|
|
|
|
|
|
|
class BasicViewSet(viewsets.ViewSet):
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'list'})
|
|
|
|
|
|
|
|
@action()
|
|
|
|
def action1(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action1'})
|
|
|
|
|
|
|
|
@action()
|
|
|
|
def action2(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2013-06-02 23:40:56 +04:00
|
|
|
@action(methods=['post', 'delete'])
|
|
|
|
def action3(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2013-05-12 06:26:34 +04:00
|
|
|
@link()
|
|
|
|
def link1(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link1'})
|
|
|
|
|
|
|
|
@link()
|
|
|
|
def link2(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link2'})
|
|
|
|
|
|
|
|
|
|
|
|
class TestSimpleRouter(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
|
|
|
|
def test_link_and_action_decorator(self):
|
|
|
|
routes = self.router.get_routes(BasicViewSet)
|
|
|
|
decorator_routes = routes[2:]
|
2013-05-12 17:22:14 +04:00
|
|
|
# Make sure all these endpoints exist and none have been clobbered
|
2013-06-02 23:40:56 +04:00
|
|
|
for i, endpoint in enumerate(['action1', 'action2', 'action3', 'link1', 'link2']):
|
2013-05-12 17:22:14 +04:00
|
|
|
route = decorator_routes[i]
|
|
|
|
# check url listing
|
|
|
|
self.assertEqual(route.url,
|
|
|
|
'^{{prefix}}/{{lookup}}/{0}/$'.format(endpoint))
|
|
|
|
# check method to function mapping
|
2013-06-02 23:40:56 +04:00
|
|
|
if endpoint == 'action3':
|
|
|
|
methods_map = ['post', 'delete']
|
|
|
|
elif endpoint.startswith('action'):
|
|
|
|
methods_map = ['post']
|
2013-05-12 17:22:14 +04:00
|
|
|
else:
|
2013-06-02 23:40:56 +04:00
|
|
|
methods_map = ['get']
|
|
|
|
for method in methods_map:
|
|
|
|
self.assertEqual(route.mapping[method], endpoint)
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
|
|
|
|
class RouterTestModel(models.Model):
|
|
|
|
uuid = models.CharField(max_length=20)
|
|
|
|
text = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
|
|
|
|
class TestCustomLookupFields(TestCase):
|
|
|
|
"""
|
|
|
|
Ensure that custom lookup fields are correctly routed.
|
|
|
|
"""
|
|
|
|
urls = 'rest_framework.tests.test_routers'
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
class NoteSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = RouterTestModel
|
|
|
|
lookup_field = 'uuid'
|
|
|
|
fields = ('url', 'uuid', 'text')
|
|
|
|
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = RouterTestModel.objects.all()
|
|
|
|
serializer_class = NoteSerializer
|
|
|
|
lookup_field = 'uuid'
|
|
|
|
|
|
|
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
self.router.register(r'notes', NoteViewSet)
|
|
|
|
|
|
|
|
from rest_framework.tests import test_routers
|
|
|
|
urls = getattr(test_routers, 'urlpatterns')
|
|
|
|
urls += patterns('',
|
|
|
|
url(r'^', include(self.router.urls)),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_custom_lookup_field_route(self):
|
|
|
|
detail_route = self.router.urls[-1]
|
|
|
|
detail_url_pattern = detail_route.regex.pattern
|
|
|
|
self.assertIn('<uuid>', detail_url_pattern)
|
|
|
|
|
|
|
|
def test_retrieve_lookup_field_list_view(self):
|
|
|
|
response = self.client.get('/notes/')
|
|
|
|
self.assertEquals(response.data,
|
|
|
|
[{
|
|
|
|
"url": "http://testserver/notes/123/",
|
|
|
|
"uuid": "123", "text": "foo bar"
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_retrieve_lookup_field_detail_view(self):
|
|
|
|
response = self.client.get('/notes/123/')
|
|
|
|
self.assertEquals(response.data,
|
|
|
|
{
|
|
|
|
"url": "http://testserver/notes/123/",
|
|
|
|
"uuid": "123", "text": "foo bar"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|