mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 08:14:16 +03:00
add tests for dotted lookup in RelatedField, PrimaryKeyRelatedField, and HyperlinkedRelatedField. #694
This commit is contained in:
parent
930bd4d0e1
commit
c992b600f7
|
@ -80,3 +80,21 @@ class RelatedFieldSourceTests(TestCase):
|
||||||
obj = ClassWithQuerysetMethod()
|
obj = ClassWithQuerysetMethod()
|
||||||
value = field.field_to_native(obj, 'field_name')
|
value = field.field_to_native(obj, 'field_name')
|
||||||
self.assertEqual(value, ['BlogPost object'])
|
self.assertEqual(value, ['BlogPost object'])
|
||||||
|
|
||||||
|
def test_dotted_source(self):
|
||||||
|
"""
|
||||||
|
Source argument should support dotted.source notation.
|
||||||
|
"""
|
||||||
|
BlogPost.objects.create(title='blah')
|
||||||
|
field = serializers.RelatedField(many=True, source='a.b.c')
|
||||||
|
|
||||||
|
class ClassWithQuerysetMethod(object):
|
||||||
|
a = {
|
||||||
|
'b': {
|
||||||
|
'c': BlogPost.objects.all()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = ClassWithQuerysetMethod()
|
||||||
|
value = field.field_to_native(obj, 'field_name')
|
||||||
|
self.assertEqual(value, ['BlogPost object'])
|
||||||
|
|
|
@ -499,3 +499,26 @@ class HyperlinkedRelatedFieldSourceTests(TestCase):
|
||||||
obj = ClassWithQuerysetMethod()
|
obj = ClassWithQuerysetMethod()
|
||||||
value = field.field_to_native(obj, 'field_name')
|
value = field.field_to_native(obj, 'field_name')
|
||||||
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|
||||||
|
|
||||||
|
def test_dotted_source(self):
|
||||||
|
"""
|
||||||
|
Source argument should support dotted.source notation.
|
||||||
|
"""
|
||||||
|
BlogPost.objects.create(title='blah')
|
||||||
|
field = serializers.HyperlinkedRelatedField(
|
||||||
|
many=True,
|
||||||
|
source='a.b.c',
|
||||||
|
view_name='dummy-url',
|
||||||
|
)
|
||||||
|
field.context = {'request': request}
|
||||||
|
|
||||||
|
class ClassWithQuerysetMethod(object):
|
||||||
|
a = {
|
||||||
|
'b': {
|
||||||
|
'c': BlogPost.objects.all()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = ClassWithQuerysetMethod()
|
||||||
|
value = field.field_to_native(obj, 'field_name')
|
||||||
|
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|
||||||
|
|
|
@ -458,3 +458,21 @@ class PrimaryKeyRelatedFieldSourceTests(TestCase):
|
||||||
obj = ClassWithQuerysetMethod()
|
obj = ClassWithQuerysetMethod()
|
||||||
value = field.field_to_native(obj, 'field_name')
|
value = field.field_to_native(obj, 'field_name')
|
||||||
self.assertEqual(value, [1])
|
self.assertEqual(value, [1])
|
||||||
|
|
||||||
|
def test_dotted_source(self):
|
||||||
|
"""
|
||||||
|
Source argument should support dotted.source notation.
|
||||||
|
"""
|
||||||
|
BlogPost.objects.create(title='blah')
|
||||||
|
field = serializers.PrimaryKeyRelatedField(many=True, source='a.b.c')
|
||||||
|
|
||||||
|
class ClassWithQuerysetMethod(object):
|
||||||
|
a = {
|
||||||
|
'b': {
|
||||||
|
'c': BlogPost.objects.all()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = ClassWithQuerysetMethod()
|
||||||
|
value = field.field_to_native(obj, 'field_name')
|
||||||
|
self.assertEqual(value, [1])
|
||||||
|
|
Loading…
Reference in New Issue
Block a user