2013-01-02 22:06:02 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-09-25 13:30:04 +04:00
|
|
|
from django.conf.urls import patterns, url
|
2012-12-18 23:20:31 +04:00
|
|
|
from django.test import TestCase
|
|
|
|
from rest_framework import serializers
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2014-03-02 15:40:30 +04:00
|
|
|
from tests.models import (
|
2013-05-18 14:23:12 +04:00
|
|
|
BlogPost,
|
2013-02-13 00:07:35 +04:00
|
|
|
ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource,
|
|
|
|
NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource
|
|
|
|
)
|
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2013-02-13 00:07:35 +04:00
|
|
|
request = factory.get('/') # Just to ensure we have a request in the serializer context
|
2012-12-18 23:20:31 +04:00
|
|
|
|
2013-01-16 18:32:51 +04:00
|
|
|
|
2012-12-18 23:20:31 +04:00
|
|
|
def dummy_view(request, pk):
|
|
|
|
pass
|
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
urlpatterns = patterns(
|
|
|
|
'',
|
2013-05-18 14:23:12 +04:00
|
|
|
url(r'^dummyurl/(?P<pk>[0-9]+)/$', dummy_view, name='dummy-url'),
|
2012-12-18 23:20:31 +04:00
|
|
|
url(r'^manytomanysource/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanysource-detail'),
|
|
|
|
url(r'^manytomanytarget/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanytarget-detail'),
|
|
|
|
url(r'^foreignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeysource-detail'),
|
|
|
|
url(r'^foreignkeytarget/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeytarget-detail'),
|
|
|
|
url(r'^nullableforeignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableforeignkeysource-detail'),
|
2013-01-08 04:27:31 +04:00
|
|
|
url(r'^onetoonetarget/(?P<pk>[0-9]+)/$', dummy_view, name='onetoonetarget-detail'),
|
|
|
|
url(r'^nullableonetoonesource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableonetoonesource-detail'),
|
2012-12-18 23:20:31 +04:00
|
|
|
)
|
|
|
|
|
2013-01-16 18:32:51 +04:00
|
|
|
|
2013-04-23 14:31:38 +04:00
|
|
|
# ManyToMany
|
2012-12-18 23:20:31 +04:00
|
|
|
class ManyToManyTargetSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ManyToManyTarget
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'sources')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ManyToManySourceSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ManyToManySource
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'targets')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
2013-04-23 14:31:38 +04:00
|
|
|
# ForeignKey
|
2012-12-18 23:20:31 +04:00
|
|
|
class ForeignKeyTargetSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ForeignKeyTarget
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'sources')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class ForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = ForeignKeySource
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'target')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Nullable ForeignKey
|
2013-01-08 04:27:31 +04:00
|
|
|
class NullableForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = NullableForeignKeySource
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'target')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
2013-04-23 14:31:38 +04:00
|
|
|
# Nullable OneToOne
|
2013-01-08 04:27:31 +04:00
|
|
|
class NullableOneToOneTargetSerializer(serializers.HyperlinkedModelSerializer):
|
2012-12-18 23:20:31 +04:00
|
|
|
class Meta:
|
2013-01-08 04:27:31 +04:00
|
|
|
model = OneToOneTarget
|
2013-04-23 14:31:38 +04:00
|
|
|
fields = ('url', 'name', 'nullable_source')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
# TODO: Add test that .data cannot be accessed prior to .is_valid
|
|
|
|
|
|
|
|
class HyperlinkedManyToManyTests(TestCase):
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_relations_hyperlink'
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
for idx in range(1, 4):
|
|
|
|
target = ManyToManyTarget(name='target-%d' % idx)
|
|
|
|
target.save()
|
|
|
|
source = ManyToManySource(name='source-%d' % idx)
|
|
|
|
source.save()
|
|
|
|
for target in ManyToManyTarget.objects.all():
|
|
|
|
source.targets.add(target)
|
|
|
|
|
|
|
|
def test_many_to_many_retrieve(self):
|
|
|
|
queryset = ManyToManySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2014-08-19 16:28:07 +04:00
|
|
|
{'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_reverse_many_to_many_retrieve(self):
|
|
|
|
queryset = ManyToManyTarget.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_many_to_many_update(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = ManyToManySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManySourceSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
serializer.save()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, data)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
# Ensure source 1 is updated, and everything else is as expected
|
|
|
|
queryset = ManyToManySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2014-08-19 16:28:07 +04:00
|
|
|
{'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_reverse_many_to_many_update(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = ManyToManyTarget.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManyTargetSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
|
|
|
serializer.save()
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, data)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
# Ensure target 1 is updated, and everything else is as expected
|
|
|
|
queryset = ManyToManyTarget.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_many_to_many_create(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']}
|
|
|
|
serializer = ManyToManySourceSerializer(data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
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-18 23:20:31 +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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_reverse_many_to_many_create(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']}
|
|
|
|
serializer = ManyToManyTargetSerializer(data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
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')
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
# 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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']},
|
|
|
|
{'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class HyperlinkedForeignKeyTests(TestCase):
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_relations_hyperlink'
|
2012-12-18 23:20:31 +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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']},
|
|
|
|
{'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []},
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_foreign_key_update(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = ForeignKeySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, data)
|
2012-12-18 23:20:31 +04:00
|
|
|
serializer.save()
|
|
|
|
|
|
|
|
# Ensure source 1 is updated, and everything else is as expected
|
|
|
|
queryset = ForeignKeySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
2013-01-19 01:29:21 +04:00
|
|
|
def test_foreign_key_update_incorrect_type(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 2}
|
2013-01-19 01:29:21 +04:00
|
|
|
instance = ForeignKeySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request})
|
2013-01-19 01:29:21 +04:00
|
|
|
self.assertFalse(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected url string, received int.']})
|
2013-01-19 01:29:21 +04:00
|
|
|
|
2012-12-29 22:26:47 +04:00
|
|
|
def test_reverse_foreign_key_update(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}
|
2012-12-29 22:26:47 +04:00
|
|
|
instance = ForeignKeyTarget.objects.get(pk=2)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeyTargetSerializer(instance, data=data, context={'request': request})
|
2012-12-29 22:26:47 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
2013-01-01 20:51:39 +04:00
|
|
|
# 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, context={'request': request})
|
2013-01-01 20:51:39 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']},
|
|
|
|
{'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []},
|
2013-01-19 01:29:21 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(new_serializer.data, expected)
|
2013-01-01 20:51:39 +04:00
|
|
|
|
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, context={'request': request})
|
2012-12-29 22:26:47 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']},
|
|
|
|
{'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/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 21:12:07 +04:00
|
|
|
def test_foreign_key_create(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'}
|
|
|
|
serializer = ForeignKeySourceSerializer(data=data, context={'request': request})
|
2012-12-29 21:12:07 +04:00
|
|
|
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 21:12:07 +04:00
|
|
|
|
|
|
|
# Ensure source 1 is updated, and everything else is as expected
|
|
|
|
queryset = ForeignKeySource.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request})
|
2012-12-29 21:12:07 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'},
|
2012-12-29 21:12:07 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-29 21:12:07 +04:00
|
|
|
|
2012-12-29 22:17:48 +04:00
|
|
|
def test_reverse_foreign_key_create(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}
|
|
|
|
serializer = ForeignKeyTargetSerializer(data=data, context={'request': request})
|
2012-12-29 22:17:48 +04:00
|
|
|
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 22:17:48 +04:00
|
|
|
|
|
|
|
# Ensure target 4 is added, and everything else is as expected
|
|
|
|
queryset = ForeignKeyTarget.objects.all()
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request})
|
2012-12-29 22:17:48 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']},
|
|
|
|
{'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []},
|
|
|
|
{'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']},
|
2012-12-29 22:17:48 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-29 21:12:07 +04:00
|
|
|
|
2012-12-18 23:20:31 +04:00
|
|
|
def test_foreign_key_update_with_invalid_null(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': None}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = ForeignKeySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertFalse(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.errors, {'target': ['This field is required.']})
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class HyperlinkedNullableForeignKeyTests(TestCase):
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_relations_hyperlink'
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
target = ForeignKeyTarget(name='target-1')
|
|
|
|
target.save()
|
|
|
|
for idx in range(1, 4):
|
2012-12-29 17:32:49 +04:00
|
|
|
if idx == 3:
|
|
|
|
target = None
|
2012-12-18 23:20:31 +04:00
|
|
|
source = NullableForeignKeySource(name='source-%d' % idx, target=target)
|
|
|
|
source.save()
|
|
|
|
|
2012-12-29 17:32:49 +04:00
|
|
|
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, context={'request': request})
|
2012-12-29 17:32:49 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None},
|
2012-12-29 17:32:49 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-29 17:32:49 +04:00
|
|
|
|
2012-12-18 23:20:31 +04:00
|
|
|
def test_foreign_key_create_with_valid_null(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None}
|
|
|
|
serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
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-18 23:20:31 +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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_foreign_key_create_with_valid_emptystring(self):
|
|
|
|
"""
|
|
|
|
The emptystring should be interpreted as null in the context
|
|
|
|
of relationships.
|
|
|
|
"""
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': ''}
|
|
|
|
expected_data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None}
|
|
|
|
serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
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-18 23:20:31 +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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None}
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_foreign_key_update_with_valid_null(self):
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = NullableForeignKeySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, data)
|
2012-12-18 23:20:31 +04:00
|
|
|
serializer.save()
|
|
|
|
|
|
|
|
# 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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None},
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +04:00
|
|
|
|
|
|
|
def test_foreign_key_update_with_valid_emptystring(self):
|
|
|
|
"""
|
|
|
|
The emptystring should be interpreted as null in the context
|
|
|
|
of relationships.
|
|
|
|
"""
|
2013-02-13 00:07:35 +04:00
|
|
|
data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': ''}
|
|
|
|
expected_data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}
|
2012-12-18 23:20:31 +04:00
|
|
|
instance = NullableForeignKeySource.objects.get(pk=1)
|
2013-02-13 00:07:35 +04:00
|
|
|
serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
self.assertTrue(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected_data)
|
2012-12-18 23:20:31 +04:00
|
|
|
serializer.save()
|
|
|
|
|
|
|
|
# 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, context={'request': request})
|
2012-12-18 23:20:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'},
|
|
|
|
{'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None},
|
2012-12-18 23:20:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2012-12-18 23:20:31 +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-12-18 23:20:31 +04:00
|
|
|
# instance = ForeignKeyTarget.objects.get(pk=1)
|
|
|
|
# serializer = ForeignKeyTargetSerializer(instance, data=data)
|
|
|
|
# self.assertTrue(serializer.is_valid())
|
2013-02-28 01:15:00 +04:00
|
|
|
# self.assertEqual(serializer.data, data)
|
2012-12-18 23:20:31 +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-12-18 23:20:31 +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-12-18 23:20:31 +04:00
|
|
|
# ]
|
2013-02-28 01:15:00 +04:00
|
|
|
# self.assertEqual(serializer.data, expected)
|
2013-01-08 04:27:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
class HyperlinkedNullableOneToOneTests(TestCase):
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_relations_hyperlink'
|
2013-01-08 04:27:31 +04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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, context={'request': request})
|
2013-01-08 04:27:31 +04:00
|
|
|
expected = [
|
2013-02-13 00:07:35 +04:00
|
|
|
{'url': 'http://testserver/onetoonetarget/1/', 'name': 'target-1', 'nullable_source': 'http://testserver/nullableonetoonesource/1/'},
|
|
|
|
{'url': 'http://testserver/onetoonetarget/2/', 'name': 'target-2', 'nullable_source': None},
|
2013-01-08 04:27:31 +04:00
|
|
|
]
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(serializer.data, expected)
|
2013-05-18 14:23:12 +04:00
|
|
|
|
|
|
|
|
|
|
|
# Regression tests for #694 (`source` attribute on related fields)
|
|
|
|
|
|
|
|
class HyperlinkedRelatedFieldSourceTests(TestCase):
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_relations_hyperlink'
|
2013-05-18 14:23:12 +04:00
|
|
|
|
|
|
|
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.HyperlinkedRelatedField(
|
|
|
|
many=True,
|
|
|
|
source='get_blogposts_manager',
|
|
|
|
view_name='dummy-url',
|
|
|
|
)
|
|
|
|
field.context = {'request': request}
|
|
|
|
|
|
|
|
class ClassWithManagerMethod(object):
|
|
|
|
def get_blogposts_manager(self):
|
|
|
|
return BlogPost.objects
|
|
|
|
|
|
|
|
obj = ClassWithManagerMethod()
|
|
|
|
value = field.field_to_native(obj, 'field_name')
|
|
|
|
self.assertEqual(value, ['http://testserver/dummyurl/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.HyperlinkedRelatedField(
|
|
|
|
many=True,
|
|
|
|
source='get_blogposts_queryset',
|
|
|
|
view_name='dummy-url',
|
|
|
|
)
|
|
|
|
field.context = {'request': request}
|
|
|
|
|
|
|
|
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, ['http://testserver/dummyurl/1/'])
|
2013-05-18 14:32:48 +04:00
|
|
|
|
|
|
|
def test_dotted_source(self):
|
|
|
|
"""
|
|
|
|
Source argument should support dotted.source notation.
|
|
|
|
"""
|
|
|
|
BlogPost.objects.create(title='blah')
|
|
|
|
field = serializers.HyperlinkedRelatedField(
|
|
|
|
many=True,
|
|
|
|
source='a.b.c',
|
|
|
|
view_name='dummy-url',
|
|
|
|
)
|
|
|
|
field.context = {'request': request}
|
|
|
|
|
|
|
|
class ClassWithQuerysetMethod(object):
|
|
|
|
a = {
|
|
|
|
'b': {
|
|
|
|
'c': BlogPost.objects.all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = ClassWithQuerysetMethod()
|
|
|
|
value = field.field_to_native(obj, 'field_name')
|
|
|
|
self.assertEqual(value, ['http://testserver/dummyurl/1/'])
|