2013-01-02 22:06:02 +04:00
|
|
|
from __future__ import unicode_literals
|
2012-11-15 00:42:30 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
2013-01-08 04:27:31 +04:00
|
|
|
from rest_framework.tests.models import ForeignKeyTarget, ForeignKeySource, NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource
|
2012-11-15 00:42:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ForeignKeySource
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('id', 'name', 'target')
|
|
|
|
depth = 1
|
2012-11-15 00:42:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ForeignKeyTarget
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('id', 'name', 'sources')
|
|
|
|
depth = 1
|
2012-11-15 00:42:30 +04:00
|
|
|
|
|
|
|
|
2012-12-29 17:19:05 +04:00
|
|
|
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = NullableForeignKeySource
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('id', 'name', 'target')
|
|
|
|
depth = 1
|
2013-01-08 04:27:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class NullableOneToOneTargetSerializer(serializers.ModelSerializer):
|
2013-01-05 00:11:03 +04:00
|
|
|
class Meta:
|
2013-01-08 04:27:31 +04:00
|
|
|
model = OneToOneTarget
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('id', 'name', 'nullable_source')
|
|
|
|
depth = 1
|
2013-01-05 00:11:03 +04:00
|
|
|
|
|
|
|
|
2012-11-15 00:42:30 +04:00
|
|
|
class ReverseForeignKeyTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
target = ForeignKeyTarget(name='target-1')
|
|
|
|
target.save()
|
|
|
|
new_target = ForeignKeyTarget(name='target-2')
|
|
|
|
new_target.save()
|
|
|
|
for idx in range(1, 4):
|
|
|
|
source = ForeignKeySource(name='source-%d' % idx, target=target)
|
|
|
|
source.save()
|
2012-12-21 13:15:28 +04:00
|
|
|
|
2012-12-29 17:10:43 +04:00
|
|
|
def test_foreign_key_retrieve(self):
|
|
|
|
queryset = ForeignKeySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(queryset, many=True)
|
2012-12-29 17:10:43 +04:00
|
|
|
expected = [
|
2013-01-02 22:06:02 +04:00
|
|
|
{'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}},
|
|
|
|
{'id': 2, 'name': 'source-2', 'target': {'id': 1, 'name': 'target-1'}},
|
|
|
|
{'id': 3, 'name': 'source-3', 'target': {'id': 1, 'name': 'target-1'}},
|
2012-12-29 17:10:43 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-29 17:10:43 +04:00
|
|
|
|
2012-12-21 13:15:28 +04:00
|
|
|
def test_reverse_foreign_key_retrieve(self):
|
|
|
|
queryset = ForeignKeyTarget.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeyTargetSerializer(queryset, many=True)
|
2012-12-21 13:15:28 +04:00
|
|
|
expected = [
|
2013-01-02 22:06:02 +04:00
|
|
|
{'id': 1, 'name': 'target-1', 'sources': [
|
|
|
|
{'id': 1, 'name': 'source-1', 'target': 1},
|
|
|
|
{'id': 2, 'name': 'source-2', 'target': 1},
|
|
|
|
{'id': 3, 'name': 'source-3', 'target': 1},
|
2012-12-21 13:15:28 +04:00
|
|
|
]},
|
2013-01-02 22:06:02 +04:00
|
|
|
{'id': 2, 'name': 'target-2', 'sources': [
|
2012-11-15 00:42:30 +04:00
|
|
|
]}
|
2012-12-21 13:15:28 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-29 17:19:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
class NestedNullableForeignKeyTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
target = ForeignKeyTarget(name='target-1')
|
|
|
|
target.save()
|
|
|
|
for idx in range(1, 4):
|
|
|
|
if idx == 3:
|
|
|
|
target = None
|
|
|
|
source = NullableForeignKeySource(name='source-%d' % idx, target=target)
|
|
|
|
source.save()
|
|
|
|
|
|
|
|
def test_foreign_key_retrieve_with_null(self):
|
|
|
|
queryset = NullableForeignKeySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = NullableForeignKeySourceSerializer(queryset, many=True)
|
2012-12-29 17:19:05 +04:00
|
|
|
expected = [
|
2013-01-02 22:06:02 +04:00
|
|
|
{'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}},
|
|
|
|
{'id': 2, 'name': 'source-2', 'target': {'id': 1, 'name': 'target-1'}},
|
|
|
|
{'id': 3, 'name': 'source-3', 'target': None},
|
2012-12-29 17:19:05 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2013-01-05 00:11:03 +04:00
|
|
|
|
2013-01-08 04:27:31 +04:00
|
|
|
|
|
|
|
class NestedNullableOneToOneTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
target = OneToOneTarget(name='target-1')
|
|
|
|
target.save()
|
|
|
|
new_target = OneToOneTarget(name='target-2')
|
|
|
|
new_target.save()
|
|
|
|
source = NullableOneToOneSource(name='source-1', target=target)
|
|
|
|
source.save()
|
|
|
|
|
2013-01-05 00:11:03 +04:00
|
|
|
def test_reverse_foreign_key_retrieve_with_null(self):
|
2013-01-08 04:27:31 +04:00
|
|
|
queryset = OneToOneTarget.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = NullableOneToOneTargetSerializer(queryset, many=True)
|
2013-01-05 00:11:03 +04:00
|
|
|
expected = [
|
2013-02-01 18:03:28 +04:00
|
|
|
{'id': 1, 'name': 'target-1', 'nullable_source': {'id': 1, 'name': 'source-1', 'target': 1}},
|
|
|
|
{'id': 2, 'name': 'target-2', 'nullable_source': None},
|
2013-01-05 00:11:03 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|