mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
add tests for related field source for HyperlinkedRelatedField. #694
This commit is contained in:
parent
0cd7c80e6e
commit
930bd4d0e1
|
@ -4,6 +4,7 @@ from django.test.client import RequestFactory
|
|||
from rest_framework import serializers
|
||||
from rest_framework.compat import patterns, url
|
||||
from rest_framework.tests.models import (
|
||||
BlogPost,
|
||||
ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource,
|
||||
NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource
|
||||
)
|
||||
|
@ -16,6 +17,7 @@ def dummy_view(request, pk):
|
|||
pass
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^dummyurl/(?P<pk>[0-9]+)/$', dummy_view, name='dummy-url'),
|
||||
url(r'^manytomanysource/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanysource-detail'),
|
||||
url(r'^manytomanytarget/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanytarget-detail'),
|
||||
url(r'^foreignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeysource-detail'),
|
||||
|
@ -451,3 +453,49 @@ class HyperlinkedNullableOneToOneTests(TestCase):
|
|||
{'url': 'http://testserver/onetoonetarget/2/', 'name': 'target-2', 'nullable_source': None},
|
||||
]
|
||||
self.assertEqual(serializer.data, expected)
|
||||
|
||||
|
||||
# Regression tests for #694 (`source` attribute on related fields)
|
||||
|
||||
class HyperlinkedRelatedFieldSourceTests(TestCase):
|
||||
urls = 'rest_framework.tests.relations_hyperlink'
|
||||
|
||||
def test_related_manager_source(self):
|
||||
"""
|
||||
Relational fields should be able to use manager-returning methods as their source.
|
||||
"""
|
||||
BlogPost.objects.create(title='blah')
|
||||
field = serializers.HyperlinkedRelatedField(
|
||||
many=True,
|
||||
source='get_blogposts_manager',
|
||||
view_name='dummy-url',
|
||||
)
|
||||
field.context = {'request': request}
|
||||
|
||||
class ClassWithManagerMethod(object):
|
||||
def get_blogposts_manager(self):
|
||||
return BlogPost.objects
|
||||
|
||||
obj = ClassWithManagerMethod()
|
||||
value = field.field_to_native(obj, 'field_name')
|
||||
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|
||||
|
||||
def test_related_queryset_source(self):
|
||||
"""
|
||||
Relational fields should be able to use queryset-returning methods as their source.
|
||||
"""
|
||||
BlogPost.objects.create(title='blah')
|
||||
field = serializers.HyperlinkedRelatedField(
|
||||
many=True,
|
||||
source='get_blogposts_queryset',
|
||||
view_name='dummy-url',
|
||||
)
|
||||
field.context = {'request': request}
|
||||
|
||||
class ClassWithQuerysetMethod(object):
|
||||
def get_blogposts_queryset(self):
|
||||
return BlogPost.objects.all()
|
||||
|
||||
obj = ClassWithQuerysetMethod()
|
||||
value = field.field_to_native(obj, 'field_name')
|
||||
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|
||||
|
|
Loading…
Reference in New Issue
Block a user