From cf637e886b9b293e44de55b9f829b738fb286365 Mon Sep 17 00:00:00 2001 From: Alen Mujezinovic Date: Wed, 8 Jun 2011 16:03:01 +0100 Subject: [PATCH] Nested tuples serialisation test --- djangorestframework/tests/resources.py | 31 +++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/djangorestframework/tests/resources.py b/djangorestframework/tests/resources.py index fd1226be3..088e3159e 100644 --- a/djangorestframework/tests/resources.py +++ b/djangorestframework/tests/resources.py @@ -2,6 +2,8 @@ from django.test import TestCase from djangorestframework.resources import _object_to_data +from django.db import models + import datetime import decimal @@ -28,4 +30,31 @@ class TestObjectToData(TestCase): def test_datetime(self): """datetime objects are left as-is.""" now = datetime.datetime.now() - self.assertEquals(_object_to_data(now), now) \ No newline at end of file + 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'))) +