2012-11-15 00:42:30 +04:00
|
|
|
from django.db import models
|
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
2013-01-03 13:23:13 +04:00
|
|
|
from rest_framework.tests.models import ForeignKeyTarget, ForeignKeySource, NullableForeignKeySource
|
2012-11-15 00:42:30 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
2012-12-29 17:10:43 +04:00
|
|
|
class Meta:
|
|
|
|
depth = 1
|
|
|
|
model = ForeignKeySource
|
|
|
|
|
|
|
|
|
|
|
|
class FlatForeignKeySourceSerializer(serializers.ModelSerializer):
|
2012-11-15 00:42:30 +04:00
|
|
|
class Meta:
|
|
|
|
model = ForeignKeySource
|
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
|
2012-12-29 17:10:43 +04:00
|
|
|
sources = FlatForeignKeySourceSerializer()
|
2012-11-15 00:42:30 +04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = ForeignKeyTarget
|
|
|
|
|
|
|
|
|
2012-12-29 17:19:05 +04:00
|
|
|
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
depth = 1
|
|
|
|
model = NullableForeignKeySource
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
serializer = ForeignKeySourceSerializer(queryset)
|
|
|
|
expected = [
|
|
|
|
{'id': 1, 'name': u'source-1', 'target': {'id': 1, 'name': u'target-1'}},
|
|
|
|
{'id': 2, 'name': u'source-2', 'target': {'id': 1, 'name': u'target-1'}},
|
|
|
|
{'id': 3, 'name': u'source-3', 'target': {'id': 1, 'name': u'target-1'}},
|
|
|
|
]
|
|
|
|
self.assertEquals(serializer.data, expected)
|
|
|
|
|
2012-12-21 13:15:28 +04:00
|
|
|
def test_reverse_foreign_key_retrieve(self):
|
|
|
|
queryset = ForeignKeyTarget.objects.all()
|
|
|
|
serializer = ForeignKeyTargetSerializer(queryset)
|
|
|
|
expected = [
|
|
|
|
{'id': 1, 'name': u'target-1', 'sources': [
|
2012-11-15 00:42:30 +04:00
|
|
|
{'id': 1, 'name': u'source-1', 'target': 1},
|
|
|
|
{'id': 2, 'name': u'source-2', 'target': 1},
|
|
|
|
{'id': 3, 'name': u'source-3', 'target': 1},
|
2012-12-21 13:15:28 +04:00
|
|
|
]},
|
|
|
|
{'id': 2, 'name': u'target-2', 'sources': [
|
2012-11-15 00:42:30 +04:00
|
|
|
]}
|
2012-12-21 13:15:28 +04:00
|
|
|
]
|
|
|
|
self.assertEquals(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()
|
|
|
|
serializer = NullableForeignKeySourceSerializer(queryset)
|
|
|
|
expected = [
|
|
|
|
{'id': 1, 'name': u'source-1', 'target': {'id': 1, 'name': u'target-1'}},
|
|
|
|
{'id': 2, 'name': u'source-2', 'target': {'id': 1, 'name': u'target-1'}},
|
|
|
|
{'id': 3, 'name': u'source-3', 'target': None},
|
|
|
|
]
|
|
|
|
self.assertEquals(serializer.data, expected)
|