Added test that makes sure that fields with

dictionaries as data are returned as expected and
not turned into string representations
This commit is contained in:
Jacob Magnusson 2012-11-03 00:32:02 +01:00
parent 19a218f859
commit 1b49615c00
2 changed files with 20 additions and 0 deletions

View File

@ -122,6 +122,13 @@ class Person(RESTFrameworkModel):
name = models.CharField(max_length=10) name = models.CharField(max_length=10)
age = models.IntegerField(null=True, blank=True) age = models.IntegerField(null=True, blank=True)
@property
def info(self):
return {
'name': self.name,
'age': self.age,
}
# Model for issue #324 # Model for issue #324
class BlankFieldModel(RESTFrameworkModel): class BlankFieldModel(RESTFrameworkModel):

View File

@ -46,8 +46,11 @@ class ActionItemSerializer(serializers.ModelSerializer):
class PersonSerializer(serializers.ModelSerializer): class PersonSerializer(serializers.ModelSerializer):
info = serializers.Field(source='info')
class Meta: class Meta:
model = Person model = Person
fields = ('name', 'age', 'info')
class BasicTests(TestCase): class BasicTests(TestCase):
@ -69,6 +72,9 @@ class BasicTests(TestCase):
'created': datetime.datetime(2012, 1, 1), 'created': datetime.datetime(2012, 1, 1),
'sub_comment': 'And Merry Christmas!' 'sub_comment': 'And Merry Christmas!'
} }
self.person_data = {'name': 'dwight', 'age': 35}
self.person = Person(**self.person_data)
self.person.save()
def test_empty(self): def test_empty(self):
serializer = CommentSerializer() serializer = CommentSerializer()
@ -100,6 +106,13 @@ class BasicTests(TestCase):
self.assertTrue(serializer.object is expected) self.assertTrue(serializer.object is expected)
self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!') self.assertEquals(serializer.data['sub_comment'], 'And Merry Christmas!')
def test_field_with_dictionary(self):
""" Make sure that dictionaries from fields are left intact
"""
serializer = PersonSerializer(instance=self.person)
expected = self.person_data
self.assertEquals(serializer.data['info'], expected)
class ValidationTests(TestCase): class ValidationTests(TestCase):
def setUp(self): def setUp(self):