mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 13:30:12 +03:00
fixed tests
This commit is contained in:
parent
2e8e9b8f4c
commit
09edaf50ed
|
@ -1196,6 +1196,7 @@ class HiddenField(Field):
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
class RecursiveField(Field):
|
class RecursiveField(Field):
|
||||||
"""
|
"""
|
||||||
A field that gets its representation from its parent.
|
A field that gets its representation from its parent.
|
||||||
|
@ -1208,13 +1209,18 @@ class RecursiveField(Field):
|
||||||
Above all, beware of cyclical references.
|
Above all, beware of cyclical references.
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
class TreeSerializer(self):
|
class TreeSerializer(self):
|
||||||
children = ListField(child=RecursiveField())
|
children = ListField(child=RecursiveField())
|
||||||
|
|
||||||
class ListSerializer(self):
|
class ListSerializer(self):
|
||||||
next = RecursiveField(allow_null=True)
|
next = RecursiveField()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
kwargz = {'required': False}
|
||||||
|
kwargz.update(kwargs)
|
||||||
|
super(RecursiveField, self).__init__(*args, **kwargz)
|
||||||
|
|
||||||
def _get_parent(self):
|
def _get_parent(self):
|
||||||
if hasattr(self.parent, 'child') and self.parent.child is self:
|
if hasattr(self.parent, 'child') and self.parent.child is self:
|
||||||
|
@ -1229,6 +1235,7 @@ class RecursiveField(Field):
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
return self._get_parent().to_internal_value(data)
|
return self._get_parent().to_internal_value(data)
|
||||||
|
|
||||||
|
|
||||||
class SerializerMethodField(Field):
|
class SerializerMethodField(Field):
|
||||||
"""
|
"""
|
||||||
A read-only field that get its representation from calling a method on the
|
A read-only field that get its representation from calling a method on the
|
||||||
|
|
|
@ -312,7 +312,7 @@ class TestRecursiveField:
|
||||||
'name': 'first',
|
'name': 'first',
|
||||||
'next': {
|
'next': {
|
||||||
'name': 'second',
|
'name': 'second',
|
||||||
'next':{
|
'next': {
|
||||||
'name': 'third',
|
'name': 'third',
|
||||||
'next': None,
|
'next': None,
|
||||||
}
|
}
|
||||||
|
@ -327,17 +327,17 @@ class TestRecursiveField:
|
||||||
# test deserialization
|
# test deserialization
|
||||||
serializer = self.link_serializer(data=value)
|
serializer = self.link_serializer(data=value)
|
||||||
assert serializer.is_valid(), \
|
assert serializer.is_valid(), \
|
||||||
'cannot validate on deserialization'
|
'cannot validate on deserialization: %s' % dict(serializer.errors)
|
||||||
assert serializer.validated_data == value, \
|
assert serializer.validated_data == value, \
|
||||||
'deserialized data does not match input'
|
'deserialized data does not match input'
|
||||||
|
|
||||||
def test_node_serializer(self):
|
def test_node_serializer(self):
|
||||||
value = {
|
value = {
|
||||||
'name': 'root',
|
'name': 'root',
|
||||||
'children': [{
|
'children': [{
|
||||||
'name': 'first child',
|
'name': 'first child',
|
||||||
'children': [],
|
'children': [],
|
||||||
},{
|
}, {
|
||||||
'name': 'second child',
|
'name': 'second child',
|
||||||
'children': [],
|
'children': [],
|
||||||
}]
|
}]
|
||||||
|
@ -349,9 +349,9 @@ class TestRecursiveField:
|
||||||
'serialized data does not match input'
|
'serialized data does not match input'
|
||||||
|
|
||||||
# deserialization
|
# deserialization
|
||||||
serializer = self.link_serializer(data=value)
|
serializer = self.node_serializer(data=value)
|
||||||
assert serializer.is_valid(), \
|
assert serializer.is_valid(), \
|
||||||
'cannot validate on deserialization'
|
'cannot validate on deserialization: %s' % dict(serializer.errors)
|
||||||
assert serializer.validated_data == value, \
|
assert serializer.validated_data == value, \
|
||||||
'deserialized data does not match input'
|
'deserialized data does not match input'
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user