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

209 lines
7.7 KiB
Python
Raw Normal View History

2012-11-22 03:20:49 +04:00
from __future__ import unicode_literals
2012-11-02 23:11:40 +04:00
from django.db import models
from django.test import TestCase
from rest_framework import serializers
2012-11-03 00:53:33 +04:00
# ManyToMany
class ManyToManyTarget(models.Model):
2012-11-02 23:11:40 +04:00
name = models.CharField(max_length=100)
2012-11-03 00:53:33 +04:00
class ManyToManySource(models.Model):
2012-11-02 23:11:40 +04:00
name = models.CharField(max_length=100)
2012-11-03 00:53:33 +04:00
targets = models.ManyToManyField(ManyToManyTarget, related_name='sources')
2012-11-02 23:11:40 +04:00
2012-11-03 00:53:33 +04:00
class ManyToManyTargetSerializer(serializers.ModelSerializer):
sources = serializers.ManyPrimaryKeyRelatedField()
2012-11-02 23:11:40 +04:00
class Meta:
2012-11-03 00:53:33 +04:00
model = ManyToManyTarget
2012-11-02 23:11:40 +04:00
2012-11-03 00:53:33 +04:00
class ManyToManySourceSerializer(serializers.ModelSerializer):
2012-11-02 23:11:40 +04:00
class Meta:
2012-11-03 00:53:33 +04:00
model = ManyToManySource
# ForeignKey
class ForeignKeyTarget(models.Model):
name = models.CharField(max_length=100)
class ForeignKeySource(models.Model):
name = models.CharField(max_length=100)
target = models.ForeignKey(ForeignKeyTarget, related_name='sources')
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
2012-11-03 00:56:51 +04:00
sources = serializers.ManyPrimaryKeyRelatedField(read_only=True)
2012-11-03 00:53:33 +04:00
class Meta:
model = ForeignKeyTarget
class ForeignKeySourceSerializer(serializers.ModelSerializer):
class Meta:
model = ForeignKeySource
2012-11-02 23:11:40 +04:00
# TODO: Add test that .data cannot be accessed prior to .is_valid
class PrimaryKeyManyToManyTests(TestCase):
def setUp(self):
for idx in range(1, 4):
2012-11-03 00:53:33 +04:00
target = ManyToManyTarget(name='target-%d' % idx)
2012-11-02 23:11:40 +04:00
target.save()
2012-11-03 00:53:33 +04:00
source = ManyToManySource(name='source-%d' % idx)
2012-11-02 23:11:40 +04:00
source.save()
2012-11-03 00:53:33 +04:00
for target in ManyToManyTarget.objects.all():
2012-11-02 23:11:40 +04:00
source.targets.add(target)
def test_many_to_many_retrieve(self):
2012-11-03 00:53:33 +04:00
queryset = ManyToManySource.objects.all()
serializer = ManyToManySourceSerializer(queryset)
2012-11-02 23:11:40 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'source-1', 'targets': [1]},
{'id': 2, 'name': 'source-2', 'targets': [1, 2]},
{'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]}
2012-11-02 23:11:40 +04:00
]
self.assertEquals(serializer.data, expected)
def test_reverse_many_to_many_retrieve(self):
2012-11-03 00:53:33 +04:00
queryset = ManyToManyTarget.objects.all()
serializer = ManyToManyTargetSerializer(queryset)
2012-11-02 23:11:40 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]},
{'id': 2, 'name': 'target-2', 'sources': [2, 3]},
{'id': 3, 'name': 'target-3', 'sources': [3]}
2012-11-02 23:11:40 +04:00
]
self.assertEquals(serializer.data, expected)
def test_many_to_many_update(self):
2012-11-22 03:20:49 +04:00
data = {'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]}
2012-11-03 00:53:33 +04:00
instance = ManyToManySource.objects.get(pk=1)
serializer = ManyToManySourceSerializer(instance, data=data)
2012-11-02 23:11:40 +04:00
self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
2012-11-03 00:53:33 +04:00
serializer.save()
2012-11-02 23:11:40 +04:00
# Ensure source 1 is updated, and everything else is as expected
2012-11-03 00:53:33 +04:00
queryset = ManyToManySource.objects.all()
serializer = ManyToManySourceSerializer(queryset)
2012-11-02 23:11:40 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]},
{'id': 2, 'name': 'source-2', 'targets': [1, 2]},
{'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]}
2012-11-02 23:11:40 +04:00
]
self.assertEquals(serializer.data, expected)
def test_reverse_many_to_many_update(self):
2012-11-22 03:20:49 +04:00
data = {'id': 1, 'name': 'target-1', 'sources': [1]}
2012-11-03 00:53:33 +04:00
instance = ManyToManyTarget.objects.get(pk=1)
serializer = ManyToManyTargetSerializer(instance, data=data)
2012-11-02 23:11:40 +04:00
self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
2012-11-03 00:53:33 +04:00
serializer.save()
2012-11-02 23:11:40 +04:00
# Ensure target 1 is updated, and everything else is as expected
2012-11-03 00:53:33 +04:00
queryset = ManyToManyTarget.objects.all()
serializer = ManyToManyTargetSerializer(queryset)
2012-11-02 23:11:40 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'target-1', 'sources': [1]},
{'id': 2, 'name': 'target-2', 'sources': [2, 3]},
{'id': 3, 'name': 'target-3', 'sources': [3]}
2012-11-02 23:11:40 +04:00
]
self.assertEquals(serializer.data, expected)
2012-11-03 00:53:33 +04:00
def test_reverse_many_to_many_create(self):
2012-11-22 03:20:49 +04:00
data = {'id': 4, 'name': 'target-4', 'sources': [1, 3]}
serializer = ManyToManyTargetSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
self.assertEquals(serializer.data, data)
2012-11-22 03:20:49 +04:00
self.assertEqual(obj.name, 'target-4')
# Ensure target 4 is added, and everything else is as expected
queryset = ManyToManyTarget.objects.all()
serializer = ManyToManyTargetSerializer(queryset)
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]},
{'id': 2, 'name': 'target-2', 'sources': [2, 3]},
{'id': 3, 'name': 'target-3', 'sources': [3]},
{'id': 4, 'name': 'target-4', 'sources': [1, 3]}
]
self.assertEquals(serializer.data, expected)
2012-11-03 00:53:33 +04:00
2012-11-13 15:47:32 +04:00
2012-11-03 00:53:33 +04:00
class PrimaryKeyForeignKeyTests(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()
def test_foreign_key_retrieve(self):
queryset = ForeignKeySource.objects.all()
serializer = ForeignKeySourceSerializer(queryset)
2012-11-03 00:53:33 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'source-1', 'target': 1},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': 1}
2012-11-03 00:53:33 +04:00
]
self.assertEquals(serializer.data, expected)
def test_reverse_foreign_key_retrieve(self):
queryset = ForeignKeyTarget.objects.all()
serializer = ForeignKeyTargetSerializer(queryset)
2012-11-03 00:53:33 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]},
{'id': 2, 'name': 'target-2', 'sources': []},
2012-11-03 00:53:33 +04:00
]
self.assertEquals(serializer.data, expected)
def test_foreign_key_update(self):
2012-11-22 03:20:49 +04:00
data = {'id': 1, 'name': 'source-1', 'target': 2}
2012-11-03 00:53:33 +04:00
instance = ForeignKeySource.objects.get(pk=1)
serializer = ForeignKeySourceSerializer(instance, data=data)
2012-11-03 00:53:33 +04:00
self.assertTrue(serializer.is_valid())
self.assertEquals(serializer.data, data)
serializer.save()
# # Ensure source 1 is updated, and everything else is as expected
queryset = ForeignKeySource.objects.all()
serializer = ForeignKeySourceSerializer(queryset)
2012-11-03 00:53:33 +04:00
expected = [
2012-11-22 03:20:49 +04:00
{'id': 1, 'name': 'source-1', 'target': 2},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': 1}
2012-11-03 00:53:33 +04:00
]
self.assertEquals(serializer.data, expected)
# reverse foreign keys MUST be read_only
# In the general case they do not provide .remove() or .clear()
# and cannot be arbitrarily set.
# def test_reverse_foreign_key_update(self):
2012-11-22 03:20:49 +04:00
# data = {'id': 1, 'name': 'target-1', 'sources': [1]}
2012-11-03 00:53:33 +04:00
# instance = ForeignKeyTarget.objects.get(pk=1)
# serializer = ForeignKeyTargetSerializer(instance, data=data)
2012-11-03 00:53:33 +04:00
# self.assertTrue(serializer.is_valid())
# self.assertEquals(serializer.data, data)
# serializer.save()
# # Ensure target 1 is updated, and everything else is as expected
# queryset = ForeignKeyTarget.objects.all()
# serializer = ForeignKeyTargetSerializer(queryset)
2012-11-03 00:53:33 +04:00
# expected = [
2012-11-22 03:20:49 +04:00
# {'id': 1, 'name': 'target-1', 'sources': [1]},
# {'id': 2, 'name': 'target-2', 'sources': []},
2012-11-03 00:53:33 +04:00
# ]
# self.assertEquals(serializer.data, expected)