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

543 lines
22 KiB
Python
Raw Normal View History

2013-01-02 22:06:02 +04:00
from __future__ import unicode_literals
from django.db import models
2012-11-02 23:11:40 +04:00
from django.test import TestCase
from rest_framework import serializers
from rest_framework.tests.models import (
BlogPost, ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource,
NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource,
)
from rest_framework.compat import six
2012-11-02 23:11:40 +04:00
# ManyToMany
2012-11-03 00:53:33 +04:00
class ManyToManyTargetSerializer(serializers.ModelSerializer):
2012-11-02 23:11:40 +04:00
class Meta:
2012-11-03 00:53:33 +04:00
model = ManyToManyTarget
fields = ('id', 'name', 'sources')
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
fields = ('id', 'name', 'targets')
2012-11-03 00:53:33 +04:00
# ForeignKey
2012-11-03 00:53:33 +04:00
class ForeignKeyTargetSerializer(serializers.ModelSerializer):
class Meta:
model = ForeignKeyTarget
fields = ('id', 'name', 'sources')
2012-11-03 00:53:33 +04:00
class ForeignKeySourceSerializer(serializers.ModelSerializer):
class Meta:
model = ForeignKeySource
fields = ('id', 'name', 'target')
2012-11-02 23:11:40 +04:00
# Nullable ForeignKey
2012-12-08 01:32:39 +04:00
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
class Meta:
model = NullableForeignKeySource
fields = ('id', 'name', 'target')
2012-12-08 01:32:39 +04:00
# Nullable OneToOne
class NullableOneToOneTargetSerializer(serializers.ModelSerializer):
class Meta:
model = OneToOneTarget
fields = ('id', 'name', 'nullable_source')
2012-11-02 23:11:40 +04:00
# TODO: Add test that .data cannot be accessed prior to .is_valid
2012-12-08 01:32:39 +04:00
class PKManyToManyTests(TestCase):
2012-11-02 23:11:40 +04:00
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()
2013-02-13 00:07:35 +04:00
serializer = ManyToManySourceSerializer(queryset, many=True)
2012-11-02 23:11:40 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-02 23:11:40 +04:00
def test_reverse_many_to_many_retrieve(self):
2012-11-03 00:53:33 +04:00
queryset = ManyToManyTarget.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ManyToManyTargetSerializer(queryset, many=True)
2012-11-02 23:11:40 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-02 23:11:40 +04:00
def test_many_to_many_update(self):
2013-01-02 22:06:02 +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())
2012-11-03 00:53:33 +04:00
serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
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()
2013-02-13 00:07:35 +04:00
serializer = ManyToManySourceSerializer(queryset, many=True)
2012-11-02 23:11:40 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-02 23:11:40 +04:00
def test_reverse_many_to_many_update(self):
2013-01-02 22:06:02 +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())
2012-11-03 00:53:33 +04:00
serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
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()
2013-02-13 00:07:35 +04:00
serializer = ManyToManyTargetSerializer(queryset, many=True)
2012-11-02 23:11:40 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-03 00:53:33 +04:00
2012-12-08 16:29:35 +04:00
def test_many_to_many_create(self):
2013-01-02 22:06:02 +04:00
data = {'id': 4, 'name': 'source-4', 'targets': [1, 3]}
2012-12-08 16:29:35 +04:00
serializer = ManyToManySourceSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'source-4')
2012-12-08 16:29:35 +04:00
# Ensure source 4 is added, and everything else is as expected
queryset = ManyToManySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ManyToManySourceSerializer(queryset, many=True)
self.assertFalse(serializer.fields['targets'].read_only)
2012-12-08 16:29:35 +04:00
expected = [
2013-01-02 22:06:02 +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]},
{'id': 4, 'name': 'source-4', 'targets': [1, 3]},
2012-12-08 16:29:35 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 16:29:35 +04:00
def test_reverse_many_to_many_create(self):
2013-01-02 22:06:02 +04:00
data = {'id': 4, 'name': 'target-4', 'sources': [1, 3]}
serializer = ManyToManyTargetSerializer(data=data)
self.assertFalse(serializer.fields['sources'].read_only)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'target-4')
# Ensure target 4 is added, and everything else is as expected
queryset = ManyToManyTarget.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ManyToManyTargetSerializer(queryset, many=True)
expected = [
2013-01-02 22:06:02 +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]}
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-03 00:53:33 +04:00
2012-11-13 15:47:32 +04:00
2012-12-08 01:32:39 +04:00
class PKForeignKeyTests(TestCase):
2012-11-03 00:53:33 +04:00
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()
2013-02-13 00:07:35 +04:00
serializer = ForeignKeySourceSerializer(queryset, many=True)
2012-11-03 00:53:33 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-03 00:53:33 +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-11-03 00:53:33 +04:00
expected = [
2013-01-02 22:06:02 +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
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-03 00:53:33 +04:00
def test_foreign_key_update(self):
2013-01-02 22:06:02 +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())
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2012-11-03 00:53:33 +04:00
serializer.save()
2012-12-08 01:32:39 +04:00
# Ensure source 1 is updated, and everything else is as expected
2012-11-03 00:53:33 +04:00
queryset = ForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ForeignKeySourceSerializer(queryset, many=True)
2012-11-03 00:53:33 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': 2},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': 1}
2012-12-08 01:32:39 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 01:32:39 +04:00
def test_foreign_key_update_incorrect_type(self):
2013-02-01 18:03:28 +04:00
data = {'id': 1, 'name': 'source-1', 'target': 'foo'}
instance = ForeignKeySource.objects.get(pk=1)
serializer = ForeignKeySourceSerializer(instance, data=data)
self.assertFalse(serializer.is_valid())
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected pk value, received %s.' % six.text_type.__name__]})
2012-12-29 22:26:47 +04:00
def test_reverse_foreign_key_update(self):
2013-01-02 22:06:02 +04:00
data = {'id': 2, 'name': 'target-2', 'sources': [1, 3]}
2012-12-29 22:26:47 +04:00
instance = ForeignKeyTarget.objects.get(pk=2)
serializer = ForeignKeyTargetSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
# We shouldn't have saved anything to the db yet since save
# hasn't been called.
queryset = ForeignKeyTarget.objects.all()
2013-02-13 00:07:35 +04:00
new_serializer = ForeignKeyTargetSerializer(queryset, many=True)
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]},
{'id': 2, 'name': 'target-2', 'sources': []},
]
2013-02-28 01:15:00 +04:00
self.assertEqual(new_serializer.data, expected)
2012-12-29 22:26:47 +04:00
serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2012-12-29 22:26:47 +04:00
# Ensure target 2 is update, and everything else is as expected
queryset = ForeignKeyTarget.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ForeignKeyTargetSerializer(queryset, many=True)
2012-12-29 22:26:47 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'target-1', 'sources': [2]},
{'id': 2, 'name': 'target-2', 'sources': [1, 3]},
2012-12-29 22:26:47 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-29 22:26:47 +04:00
2012-12-29 19:51:32 +04:00
def test_foreign_key_create(self):
2013-01-02 22:06:02 +04:00
data = {'id': 4, 'name': 'source-4', 'target': 2}
2012-12-29 19:51:32 +04:00
serializer = ForeignKeySourceSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'source-4')
2012-12-29 19:51:32 +04:00
# Ensure source 4 is added, and everything else is as expected
2012-12-29 19:51:32 +04:00
queryset = ForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ForeignKeySourceSerializer(queryset, many=True)
2012-12-29 19:51:32 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': 1},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': 1},
{'id': 4, 'name': 'source-4', 'target': 2},
2012-12-29 19:51:32 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-29 19:51:32 +04:00
2012-12-29 20:51:41 +04:00
def test_reverse_foreign_key_create(self):
2013-01-02 22:06:02 +04:00
data = {'id': 3, 'name': 'target-3', 'sources': [1, 3]}
2012-12-29 20:51:41 +04:00
serializer = ForeignKeyTargetSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'target-3')
2012-12-29 20:51:41 +04:00
# Ensure target 3 is added, and everything else is as expected
2012-12-29 20:51:41 +04:00
queryset = ForeignKeyTarget.objects.all()
2013-02-13 00:07:35 +04:00
serializer = ForeignKeyTargetSerializer(queryset, many=True)
2012-12-29 20:51:41 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'target-1', 'sources': [2]},
{'id': 2, 'name': 'target-2', 'sources': []},
{'id': 3, 'name': 'target-3', 'sources': [1, 3]},
2012-12-29 20:51:41 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-29 19:51:32 +04:00
2012-12-08 01:32:39 +04:00
def test_foreign_key_update_with_invalid_null(self):
2013-01-02 22:06:02 +04:00
data = {'id': 1, 'name': 'source-1', 'target': None}
2012-12-08 01:32:39 +04:00
instance = ForeignKeySource.objects.get(pk=1)
serializer = ForeignKeySourceSerializer(instance, data=data)
self.assertFalse(serializer.is_valid())
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.errors, {'target': ['This field is required.']})
2012-12-08 01:32:39 +04:00
class PKNullableForeignKeyTests(TestCase):
def setUp(self):
target = ForeignKeyTarget(name='target-1')
target.save()
for idx in range(1, 4):
if idx == 3:
target = None
2012-12-08 01:32:39 +04:00
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)
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': 1},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': None},
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 16:48:27 +04:00
def test_foreign_key_create_with_valid_null(self):
2013-01-02 22:06:02 +04:00
data = {'id': 4, 'name': 'source-4', 'target': None}
2012-12-08 16:48:27 +04:00
serializer = NullableForeignKeySourceSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'source-4')
2012-12-08 16:48:27 +04:00
# Ensure source 4 is created, and everything else is as expected
queryset = NullableForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = NullableForeignKeySourceSerializer(queryset, many=True)
2012-12-08 16:48:27 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': 1},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': None},
{'id': 4, 'name': 'source-4', 'target': None}
2012-12-08 16:48:27 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 16:48:27 +04:00
def test_foreign_key_create_with_valid_emptystring(self):
"""
The emptystring should be interpreted as null in the context
of relationships.
"""
2013-01-02 22:06:02 +04:00
data = {'id': 4, 'name': 'source-4', 'target': ''}
expected_data = {'id': 4, 'name': 'source-4', 'target': None}
2012-12-08 16:48:27 +04:00
serializer = NullableForeignKeySourceSerializer(data=data)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected_data)
2013-01-02 22:06:02 +04:00
self.assertEqual(obj.name, 'source-4')
2012-12-08 16:48:27 +04:00
# Ensure source 4 is created, and everything else is as expected
queryset = NullableForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = NullableForeignKeySourceSerializer(queryset, many=True)
2012-12-08 16:48:27 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': 1},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': None},
{'id': 4, 'name': 'source-4', 'target': None}
2012-12-08 16:48:27 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 16:48:27 +04:00
2012-12-08 01:32:39 +04:00
def test_foreign_key_update_with_valid_null(self):
2013-01-02 22:06:02 +04:00
data = {'id': 1, 'name': 'source-1', 'target': None}
2012-12-08 01:32:39 +04:00
instance = NullableForeignKeySource.objects.get(pk=1)
serializer = NullableForeignKeySourceSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, data)
2012-12-08 01:32:39 +04:00
serializer.save()
2012-12-08 16:48:27 +04:00
# Ensure source 1 is updated, and everything else is as expected
queryset = NullableForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = NullableForeignKeySourceSerializer(queryset, many=True)
2012-12-08 16:48:27 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': None},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': None}
2012-12-08 16:48:27 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-12-08 16:48:27 +04:00
def test_foreign_key_update_with_valid_emptystring(self):
"""
The emptystring should be interpreted as null in the context
of relationships.
"""
2013-01-02 22:06:02 +04:00
data = {'id': 1, 'name': 'source-1', 'target': ''}
expected_data = {'id': 1, 'name': 'source-1', 'target': None}
2012-12-08 16:48:27 +04:00
instance = NullableForeignKeySource.objects.get(pk=1)
serializer = NullableForeignKeySourceSerializer(instance, data=data)
self.assertTrue(serializer.is_valid())
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected_data)
2012-12-08 16:48:27 +04:00
serializer.save()
2012-12-08 01:32:39 +04:00
# Ensure source 1 is updated, and everything else is as expected
queryset = NullableForeignKeySource.objects.all()
2013-02-13 00:07:35 +04:00
serializer = NullableForeignKeySourceSerializer(queryset, many=True)
2012-12-08 01:32:39 +04:00
expected = [
2013-01-02 22:06:02 +04:00
{'id': 1, 'name': 'source-1', 'target': None},
{'id': 2, 'name': 'source-2', 'target': 1},
{'id': 3, 'name': 'source-3', 'target': None}
2012-11-03 00:53:33 +04:00
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
2012-11-03 00:53:33 +04:00
# 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):
2013-01-02 22:06:02 +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())
2013-02-28 01:15:00 +04:00
# self.assertEqual(serializer.data, data)
2012-11-03 00:53:33 +04:00
# serializer.save()
# # Ensure target 1 is updated, and everything else is as expected
# queryset = ForeignKeyTarget.objects.all()
2013-02-13 00:07:35 +04:00
# serializer = ForeignKeyTargetSerializer(queryset, many=True)
2012-11-03 00:53:33 +04:00
# expected = [
2013-01-02 22:06:02 +04:00
# {'id': 1, 'name': 'target-1', 'sources': [1]},
# {'id': 2, 'name': 'target-2', 'sources': []},
2012-11-03 00:53:33 +04:00
# ]
2013-02-28 01:15:00 +04:00
# self.assertEqual(serializer.data, expected)
class PKNullableOneToOneTests(TestCase):
def setUp(self):
target = OneToOneTarget(name='target-1')
target.save()
new_target = OneToOneTarget(name='target-2')
new_target.save()
2013-03-11 00:01:51 +04:00
source = NullableOneToOneSource(name='source-1', target=new_target)
source.save()
def test_reverse_foreign_key_retrieve_with_null(self):
queryset = OneToOneTarget.objects.all()
2013-02-13 00:07:35 +04:00
serializer = NullableOneToOneTargetSerializer(queryset, many=True)
expected = [
2013-03-11 00:01:51 +04:00
{'id': 1, 'name': 'target-1', 'nullable_source': None},
{'id': 2, 'name': 'target-2', 'nullable_source': 1},
]
2013-02-28 01:15:00 +04:00
self.assertEqual(serializer.data, expected)
# The below models and tests ensure that serializer fields corresponding
# to a ManyToManyField field with a user-specified ``through`` model are
# set to read only
class ManyToManyThroughTarget(models.Model):
name = models.CharField(max_length=100)
class ManyToManyThrough(models.Model):
source = models.ForeignKey('ManyToManyThroughSource')
target = models.ForeignKey(ManyToManyThroughTarget)
class ManyToManyThroughSource(models.Model):
name = models.CharField(max_length=100)
targets = models.ManyToManyField(ManyToManyThroughTarget,
related_name='sources',
through='ManyToManyThrough')
class ManyToManyThroughTargetSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyThroughTarget
fields = ('id', 'name', 'sources')
class ManyToManyThroughSourceSerializer(serializers.ModelSerializer):
class Meta:
model = ManyToManyThroughSource
fields = ('id', 'name', 'targets')
class PKManyToManyThroughTests(TestCase):
def setUp(self):
self.source = ManyToManyThroughSource.objects.create(
name='through-source-1')
self.target = ManyToManyThroughTarget.objects.create(
name='through-target-1')
def test_many_to_many_create(self):
data = {'id': 2, 'name': 'source-2', 'targets': [self.target.pk]}
serializer = ManyToManyThroughSourceSerializer(data=data)
self.assertTrue(serializer.fields['targets'].read_only)
self.assertTrue(serializer.is_valid())
obj = serializer.save()
self.assertEqual(obj.name, 'source-2')
self.assertEqual(obj.targets.count(), 0)
def test_many_to_many_reverse_create(self):
data = {'id': 2, 'name': 'target-2', 'sources': [self.source.pk]}
serializer = ManyToManyThroughTargetSerializer(data=data)
self.assertTrue(serializer.fields['sources'].read_only)
self.assertTrue(serializer.is_valid())
serializer.save()
obj = serializer.save()
self.assertEqual(obj.name, 'target-2')
self.assertEqual(obj.sources.count(), 0)
2013-05-18 17:04:27 +04:00
# Regression tests for #694 (`source` attribute on related fields)
2013-05-18 17:04:27 +04:00
class PrimaryKeyRelatedFieldSourceTests(TestCase):
def test_related_manager_source(self):
"""
Relational fields should be able to use manager-returning methods as their source.
"""
BlogPost.objects.create(title='blah')
field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_manager')
class ClassWithManagerMethod(object):
def get_blogposts_manager(self):
return BlogPost.objects
obj = ClassWithManagerMethod()
value = field.field_to_native(obj, 'field_name')
self.assertEqual(value, [1])
def test_related_queryset_source(self):
"""
Relational fields should be able to use queryset-returning methods as their source.
"""
BlogPost.objects.create(title='blah')
field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_queryset')
class ClassWithQuerysetMethod(object):
def get_blogposts_queryset(self):
return BlogPost.objects.all()
obj = ClassWithQuerysetMethod()
value = field.field_to_native(obj, 'field_name')
self.assertEqual(value, [1])
def test_dotted_source(self):
"""
Source argument should support dotted.source notation.
"""
BlogPost.objects.create(title='blah')
field = serializers.PrimaryKeyRelatedField(many=True, source='a.b.c')
class ClassWithQuerysetMethod(object):
a = {
'b': {
'c': BlogPost.objects.all()
}
}
obj = ClassWithQuerysetMethod()
value = field.field_to_native(obj, 'field_name')
self.assertEqual(value, [1])