mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Nested tuples serialisation test
This commit is contained in:
parent
253663aee8
commit
cf637e886b
|
@ -2,6 +2,8 @@
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from djangorestframework.resources import _object_to_data
|
from djangorestframework.resources import _object_to_data
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
|
|
||||||
|
@ -29,3 +31,30 @@ class TestObjectToData(TestCase):
|
||||||
"""datetime objects are left as-is."""
|
"""datetime objects are left as-is."""
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
self.assertEquals(_object_to_data(now), now)
|
self.assertEquals(_object_to_data(now), now)
|
||||||
|
|
||||||
|
def test_tuples(self):
|
||||||
|
""" Test tuple serialisation """
|
||||||
|
class M1(models.Model):
|
||||||
|
field1 = models.CharField()
|
||||||
|
field2 = models.CharField()
|
||||||
|
|
||||||
|
class M2(models.Model):
|
||||||
|
field = models.OneToOneField(M1)
|
||||||
|
|
||||||
|
class M3(models.Model):
|
||||||
|
field = models.ForeignKey(M1)
|
||||||
|
|
||||||
|
m1 = M1(field1='foo', field2='bar')
|
||||||
|
m2 = M2(field=m1)
|
||||||
|
m3 = M3(field=m1)
|
||||||
|
|
||||||
|
Resource = type('Resource', (object,), {'fields':(), 'include':(), 'exclude':()})
|
||||||
|
|
||||||
|
r = Resource()
|
||||||
|
r.fields = (('field', ('field1')),)
|
||||||
|
|
||||||
|
self.assertEqual(_object_to_data(m2, r), dict(field=dict(field1=u'foo')))
|
||||||
|
|
||||||
|
r.fields = (('field', ('field2')),)
|
||||||
|
self.assertEqual(_object_to_data(m3, r), dict(field=dict(field2=u'bar')))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user