django-rest-framework/rest_framework/tests/relations_nested.py

126 lines
5.9 KiB
Python
Raw Normal View History

2013-01-02 22:06:02 +04:00
from __future__ import unicode_literals
2013-03-15 23:17:54 +04:00
from django.db import models
from django.test import TestCase
from rest_framework import serializers
2013-03-15 23:17:54 +04:00
class OneToOneTarget(models.Model):
name = models.CharField(max_length=100)
2012-12-29 17:10:43 +04:00
2013-03-15 23:17:54 +04:00
class OneToOneTargetSource(models.Model):
name = models.CharField(max_length=100)
target = models.OneToOneField(OneToOneTarget, null=True, blank=True,
related_name='target_source')
2013-03-15 23:17:54 +04:00
class OneToOneSource(models.Model):
name = models.CharField(max_length=100)
target_source = models.OneToOneField(OneToOneTargetSource, related_name='source')
2013-03-15 23:17:54 +04:00
class OneToOneSourceSerializer(serializers.ModelSerializer):
class Meta:
2013-03-15 23:17:54 +04:00
model = OneToOneSource
exclude = ('target_source', )
2013-03-15 23:17:54 +04:00
class OneToOneTargetSourceSerializer(serializers.ModelSerializer):
source = OneToOneSourceSerializer()
class Meta:
2013-03-15 23:17:54 +04:00
model = OneToOneTargetSource
exclude = ('target', )
2013-03-15 23:17:54 +04:00
class OneToOneTargetSerializer(serializers.ModelSerializer):
target_source = OneToOneTargetSourceSerializer()
class Meta:
model = OneToOneTarget
2013-03-15 23:17:54 +04:00
class NestedOneToOneTests(TestCase):
def setUp(self):
for idx in range(1, 4):
2013-03-15 23:17:54 +04:00
target = OneToOneTarget(name='target-%d' % idx)
target.save()
target_source = OneToOneTargetSource(name='target-source-%d' % idx, target=target)
target_source.save()
source = OneToOneSource(name='source-%d' % idx, target_source=target_source)
source.save()
2013-03-15 23:17:54 +04:00
def test_one_to_one_retrieve(self):
queryset = OneToOneTarget.objects.all()
serializer = OneToOneTargetSerializer(queryset)
2012-12-29 17:10:43 +04:00
expected = [
2013-03-15 23:17:54 +04:00
{'id': 1, 'name': 'target-1', 'target_source': {'id': 1, 'name': 'target-source-1', 'source': {'id': 1, 'name': 'source-1'}}},
{'id': 2, 'name': 'target-2', 'target_source': {'id': 2, 'name': 'target-source-2', 'source': {'id': 2, 'name': 'source-2'}}},
{'id': 3, 'name': 'target-3', 'target_source': {'id': 3, 'name': 'target-source-3', 'source': {'id': 3, 'name': 'source-3'}}}
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
2013-03-15 23:17:54 +04:00
def test_one_to_one_create(self):
data = {'id': 4, 'name': 'target-4', 'target_source': {'id': 4, 'name': 'target-source-4', 'source': {'id': 4, 'name': 'source-4'}}}
serializer = OneToOneTargetSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
self.assertEqual(serializer.data, data)
self.assertEqual(obj.name, 'target-4')
# Ensure (target 4, target_source 4, source 4) are added, and
# everything else is as expected.
queryset = OneToOneTarget.objects.all()
serializer = OneToOneTargetSerializer(queryset)
expected = [
2013-03-15 23:17:54 +04:00
{'id': 1, 'name': 'target-1', 'target_source': {'id': 1, 'name': 'target-source-1', 'source': {'id': 1, 'name': 'source-1'}}},
{'id': 2, 'name': 'target-2', 'target_source': {'id': 2, 'name': 'target-source-2', 'source': {'id': 2, 'name': 'source-2'}}},
{'id': 3, 'name': 'target-3', 'target_source': {'id': 3, 'name': 'target-source-3', 'source': {'id': 3, 'name': 'source-3'}}},
{'id': 4, 'name': 'target-4', 'target_source': {'id': 4, 'name': 'target-source-4', 'source': {'id': 4, 'name': 'source-4'}}}
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2013-03-15 23:17:54 +04:00
def test_one_to_one_create_with_invalid_data(self):
data = {'id': 4, 'name': 'target-4', 'target_source': {'id': 4, 'name': 'target-source-4', 'source': {'id': 4}}}
serializer = OneToOneTargetSerializer(data=data)
self.assertFalse(serializer.is_valid())
self.assertEqual(serializer.errors, {'target_source': [{'source': [{'name': ['This field is required.']}]}]})
def test_one_to_one_update(self):
data = {'id': 3, 'name': 'target-3-updated', 'target_source': {'id': 3, 'name': 'target-source-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}}}
instance = OneToOneTarget.objects.get(pk=3)
serializer = OneToOneTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
self.assertEqual(serializer.data, data)
self.assertEqual(obj.name, 'target-3-updated')
# Ensure (target 3, target_source 3, source 3) are updated,
# and everything else is as expected.
queryset = OneToOneTarget.objects.all()
serializer = OneToOneTargetSerializer(queryset)
expected = [
2013-03-15 23:17:54 +04:00
{'id': 1, 'name': 'target-1', 'target_source': {'id': 1, 'name': 'target-source-1', 'source': {'id': 1, 'name': 'source-1'}}},
{'id': 2, 'name': 'target-2', 'target_source': {'id': 2, 'name': 'target-source-2', 'source': {'id': 2, 'name': 'source-2'}}},
{'id': 3, 'name': 'target-3-updated', 'target_source': {'id': 3, 'name': 'target-source-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}}}
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2013-03-15 23:17:54 +04:00
def test_one_to_one_delete(self):
data = {'id': 3, 'name': 'target-3', 'target_source': None}
instance = OneToOneTarget.objects.get(pk=3)
serializer = OneToOneTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
serializer.save()
2013-03-15 23:17:54 +04:00
# Ensure (target_source 3, source 3) are deleted,
# and everything else is as expected.
queryset = OneToOneTarget.objects.all()
2013-03-15 23:17:54 +04:00
serializer = OneToOneTargetSerializer(queryset)
expected = [
2013-03-15 23:17:54 +04:00
{'id': 1, 'name': 'target-1', 'target_source': {'id': 1, 'name': 'target-source-1', 'source': {'id': 1, 'name': 'source-1'}}},
{'id': 2, 'name': 'target-2', 'target_source': {'id': 2, 'name': 'target-source-2', 'source': {'id': 2, 'name': 'source-2'}}},
{'id': 3, 'name': 'target-3', 'target_source': None}
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)