2011-06-14 21:22:13 +04:00
|
|
|
"""Tests for the resource module"""
|
2012-01-11 20:16:22 +04:00
|
|
|
from django.db import models
|
2011-06-14 21:22:13 +04:00
|
|
|
from django.test import TestCase
|
2012-01-11 20:16:22 +04:00
|
|
|
from django.utils.translation import ugettext_lazy
|
2011-06-14 21:22:13 +04:00
|
|
|
from djangorestframework.serializer import Serializer
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import decimal
|
|
|
|
|
2011-12-29 17:31:12 +04:00
|
|
|
class TestObjectToData(TestCase):
|
2011-06-14 21:22:13 +04:00
|
|
|
"""
|
|
|
|
Tests for the Serializer class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.serializer = Serializer()
|
|
|
|
self.serialize = self.serializer.serialize
|
|
|
|
|
|
|
|
def test_decimal(self):
|
|
|
|
"""Decimals need to be converted to a string representation."""
|
2011-06-22 02:01:41 +04:00
|
|
|
self.assertEquals(self.serialize(decimal.Decimal('1.5')), decimal.Decimal('1.5'))
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
def test_function(self):
|
|
|
|
"""Functions with no arguments should be called."""
|
|
|
|
def foo():
|
|
|
|
return 1
|
|
|
|
self.assertEquals(self.serialize(foo), 1)
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
"""Methods with only a ``self`` argument should be called."""
|
|
|
|
class Foo(object):
|
|
|
|
def foo(self):
|
|
|
|
return 1
|
|
|
|
self.assertEquals(self.serialize(Foo().foo), 1)
|
|
|
|
|
|
|
|
def test_datetime(self):
|
2012-01-10 22:38:01 +04:00
|
|
|
"""datetime objects are left as-is."""
|
2011-06-14 21:22:13 +04:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
self.assertEquals(self.serialize(now), now)
|
|
|
|
|
2011-07-13 16:12:34 +04:00
|
|
|
def test_dict_method_name_collision(self):
|
|
|
|
"""dict with key that collides with dict method name"""
|
|
|
|
self.assertEquals(self.serialize({'items': 'foo'}), {'items': u'foo'})
|
|
|
|
self.assertEquals(self.serialize({'keys': 'foo'}), {'keys': u'foo'})
|
|
|
|
self.assertEquals(self.serialize({'values': 'foo'}), {'values': u'foo'})
|
|
|
|
|
2012-01-11 20:16:22 +04:00
|
|
|
def test_ugettext_lazy(self):
|
|
|
|
self.assertEquals(self.serialize(ugettext_lazy('foobar')), u'foobar')
|
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class TestFieldNesting(TestCase):
|
|
|
|
"""
|
|
|
|
Test nesting the fields in the Serializer class
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
self.serializer = Serializer()
|
|
|
|
self.serialize = self.serializer.serialize
|
|
|
|
|
|
|
|
class M1(models.Model):
|
2011-12-15 00:10:06 +04:00
|
|
|
field1 = models.CharField(max_length=256)
|
|
|
|
field2 = models.CharField(max_length=256)
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class M2(models.Model):
|
|
|
|
field = models.OneToOneField(M1)
|
|
|
|
|
|
|
|
class M3(models.Model):
|
|
|
|
field = models.ForeignKey(M1)
|
|
|
|
|
|
|
|
self.m1 = M1(field1='foo', field2='bar')
|
|
|
|
self.m2 = M2(field=self.m1)
|
|
|
|
self.m3 = M3(field=self.m1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_tuple_nesting(self):
|
|
|
|
"""
|
|
|
|
Test tuple nesting on `fields` attr
|
|
|
|
"""
|
|
|
|
class SerializerM2(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = (('field', ('field1',)),)
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class SerializerM3(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = (('field', ('field2',)),)
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
|
|
|
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
|
|
|
|
|
|
|
|
|
|
|
def test_serializer_class_nesting(self):
|
|
|
|
"""
|
|
|
|
Test related model serialization
|
|
|
|
"""
|
|
|
|
class NestedM2(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = ('field1', )
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class NestedM3(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = ('field2', )
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class SerializerM2(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = [('field', NestedM2)]
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class SerializerM3(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = [('field', NestedM3)]
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
|
|
|
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
|
|
|
|
2012-02-24 04:26:02 +04:00
|
|
|
def test_serializer_no_fields(self):
|
|
|
|
"""
|
|
|
|
Test related serializer works when the fields attr isn't present. Fix for
|
|
|
|
#178.
|
|
|
|
"""
|
|
|
|
class NestedM2(Serializer):
|
|
|
|
fields = ('field1', )
|
|
|
|
|
|
|
|
class NestedM3(Serializer):
|
|
|
|
fields = ('field2', )
|
|
|
|
|
|
|
|
class SerializerM2(Serializer):
|
|
|
|
include = [('field', NestedM2)]
|
|
|
|
exclude = ('id', )
|
|
|
|
|
|
|
|
class SerializerM3(Serializer):
|
|
|
|
fields = [('field', NestedM3)]
|
|
|
|
|
|
|
|
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
|
|
|
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
|
|
|
|
2011-06-14 21:22:13 +04:00
|
|
|
def test_serializer_classname_nesting(self):
|
|
|
|
"""
|
|
|
|
Test related model serialization
|
|
|
|
"""
|
|
|
|
class SerializerM2(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = [('field', 'NestedM2')]
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class SerializerM3(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = [('field', 'NestedM3')]
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class NestedM2(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = ('field1', )
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
class NestedM3(Serializer):
|
2011-06-15 17:09:01 +04:00
|
|
|
fields = ('field2', )
|
2011-06-14 21:22:13 +04:00
|
|
|
|
|
|
|
self.assertEqual(SerializerM2().serialize(self.m2), {'field': {'field1': u'foo'}})
|
|
|
|
self.assertEqual(SerializerM3().serialize(self.m3), {'field': {'field2': u'bar'}})
|
2012-01-10 22:38:01 +04:00
|
|
|
|
2012-01-11 18:56:12 +04:00
|
|
|
def test_serializer_overridden_hook_method(self):
|
2012-01-10 22:38:01 +04:00
|
|
|
"""
|
2012-01-11 18:56:12 +04:00
|
|
|
Test serializing a model instance which overrides a class method on the
|
|
|
|
serializer. Checks for correct behaviour in odd edge case.
|
2012-01-10 22:38:01 +04:00
|
|
|
"""
|
|
|
|
class SerializerM2(Serializer):
|
2012-01-11 18:56:12 +04:00
|
|
|
fields = ('overridden', )
|
|
|
|
|
|
|
|
def overridden(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.m2.overridden = True
|
|
|
|
self.assertEqual(SerializerM2().serialize_model(self.m2),
|
|
|
|
{'overridden': True})
|