summaryrefslogtreecommitdiff
path: root/tests/serializers
diff options
context:
space:
mode:
authorPeter Inglesby <peter.inglesby@gmail.com>2018-07-13 22:54:47 +0100
committerTim Graham <timograham@gmail.com>2018-07-13 17:54:47 -0400
commit312eb5cb11d09c0c41b2740e2e9aef838d60c8b5 (patch)
tree00dec6ddb620204930bc2c870e1bb73c5bb87973 /tests/serializers
parent8f75d21a2e6d255848ae441d858c6ea819e3d100 (diff)
downloaddjango-312eb5cb11d09c0c41b2740e2e9aef838d60c8b5.tar.gz
Fixed #26291 -- Allowed loaddata to handle forward references in natural_key fixtures.
Diffstat (limited to 'tests/serializers')
-rw-r--r--tests/serializers/models/natural.py18
-rw-r--r--tests/serializers/test_natural.py95
2 files changed, 112 insertions, 1 deletions
diff --git a/tests/serializers/models/natural.py b/tests/serializers/models/natural.py
index b50956692e..cd24b67f0a 100644
--- a/tests/serializers/models/natural.py
+++ b/tests/serializers/models/natural.py
@@ -19,3 +19,21 @@ class NaturalKeyAnchor(models.Model):
class FKDataNaturalKey(models.Model):
data = models.ForeignKey(NaturalKeyAnchor, models.SET_NULL, null=True)
+
+
+class NaturalKeyThing(models.Model):
+ key = models.CharField(max_length=100)
+ other_thing = models.ForeignKey('NaturalKeyThing', on_delete=models.CASCADE, null=True)
+ other_things = models.ManyToManyField('NaturalKeyThing', related_name='thing_m2m_set')
+
+ class Manager(models.Manager):
+ def get_by_natural_key(self, key):
+ return self.get(key=key)
+
+ objects = Manager()
+
+ def natural_key(self):
+ return (self.key,)
+
+ def __str__(self):
+ return self.key
diff --git a/tests/serializers/test_natural.py b/tests/serializers/test_natural.py
index 776f554a1c..7e9e9382da 100644
--- a/tests/serializers/test_natural.py
+++ b/tests/serializers/test_natural.py
@@ -2,7 +2,7 @@ from django.core import serializers
from django.db import connection
from django.test import TestCase
-from .models import Child, FKDataNaturalKey, NaturalKeyAnchor
+from .models import Child, FKDataNaturalKey, NaturalKeyAnchor, NaturalKeyThing
from .tests import register_tests
@@ -93,7 +93,100 @@ def natural_pk_mti_test(self, format):
self.assertEqual(child.child_data, child.parent_data)
+def forward_ref_fk_test(self, format):
+ t1 = NaturalKeyThing.objects.create(key='t1')
+ t2 = NaturalKeyThing.objects.create(key='t2', other_thing=t1)
+ t1.other_thing = t2
+ t1.save()
+ string_data = serializers.serialize(
+ format, [t1, t2], use_natural_primary_keys=True,
+ use_natural_foreign_keys=True,
+ )
+ NaturalKeyThing.objects.all().delete()
+ objs_with_deferred_fields = []
+ for obj in serializers.deserialize(format, string_data, handle_forward_references=True):
+ obj.save()
+ if obj.deferred_fields:
+ objs_with_deferred_fields.append(obj)
+ for obj in objs_with_deferred_fields:
+ obj.save_deferred_fields()
+ t1 = NaturalKeyThing.objects.get(key='t1')
+ t2 = NaturalKeyThing.objects.get(key='t2')
+ self.assertEqual(t1.other_thing, t2)
+ self.assertEqual(t2.other_thing, t1)
+
+
+def forward_ref_fk_with_error_test(self, format):
+ t1 = NaturalKeyThing.objects.create(key='t1')
+ t2 = NaturalKeyThing.objects.create(key='t2', other_thing=t1)
+ t1.other_thing = t2
+ t1.save()
+ string_data = serializers.serialize(
+ format, [t1], use_natural_primary_keys=True,
+ use_natural_foreign_keys=True,
+ )
+ NaturalKeyThing.objects.all().delete()
+ objs_with_deferred_fields = []
+ for obj in serializers.deserialize(format, string_data, handle_forward_references=True):
+ obj.save()
+ if obj.deferred_fields:
+ objs_with_deferred_fields.append(obj)
+ obj = objs_with_deferred_fields[0]
+ msg = 'NaturalKeyThing matching query does not exist'
+ with self.assertRaisesMessage(serializers.base.DeserializationError, msg):
+ obj.save_deferred_fields()
+
+
+def forward_ref_m2m_test(self, format):
+ t1 = NaturalKeyThing.objects.create(key='t1')
+ t2 = NaturalKeyThing.objects.create(key='t2')
+ t3 = NaturalKeyThing.objects.create(key='t3')
+ t1.other_things.set([t2, t3])
+ string_data = serializers.serialize(
+ format, [t1, t2, t3], use_natural_primary_keys=True,
+ use_natural_foreign_keys=True,
+ )
+ NaturalKeyThing.objects.all().delete()
+ objs_with_deferred_fields = []
+ for obj in serializers.deserialize(format, string_data, handle_forward_references=True):
+ obj.save()
+ if obj.deferred_fields:
+ objs_with_deferred_fields.append(obj)
+ for obj in objs_with_deferred_fields:
+ obj.save_deferred_fields()
+ t1 = NaturalKeyThing.objects.get(key='t1')
+ t2 = NaturalKeyThing.objects.get(key='t2')
+ t3 = NaturalKeyThing.objects.get(key='t3')
+ self.assertCountEqual(t1.other_things.all(), [t2, t3])
+
+
+def forward_ref_m2m_with_error_test(self, format):
+ t1 = NaturalKeyThing.objects.create(key='t1')
+ t2 = NaturalKeyThing.objects.create(key='t2')
+ t3 = NaturalKeyThing.objects.create(key='t3')
+ t1.other_things.set([t2, t3])
+ t1.save()
+ string_data = serializers.serialize(
+ format, [t1, t2], use_natural_primary_keys=True,
+ use_natural_foreign_keys=True,
+ )
+ NaturalKeyThing.objects.all().delete()
+ objs_with_deferred_fields = []
+ for obj in serializers.deserialize(format, string_data, handle_forward_references=True):
+ obj.save()
+ if obj.deferred_fields:
+ objs_with_deferred_fields.append(obj)
+ obj = objs_with_deferred_fields[0]
+ msg = 'NaturalKeyThing matching query does not exist'
+ with self.assertRaisesMessage(serializers.base.DeserializationError, msg):
+ obj.save_deferred_fields()
+
+
# Dynamically register tests for each serializer
register_tests(NaturalKeySerializerTests, 'test_%s_natural_key_serializer', natural_key_serializer_test)
register_tests(NaturalKeySerializerTests, 'test_%s_serializer_natural_keys', natural_key_test)
register_tests(NaturalKeySerializerTests, 'test_%s_serializer_natural_pks_mti', natural_pk_mti_test)
+register_tests(NaturalKeySerializerTests, 'test_%s_forward_references_fks', forward_ref_fk_test)
+register_tests(NaturalKeySerializerTests, 'test_%s_forward_references_fk_errors', forward_ref_fk_with_error_test)
+register_tests(NaturalKeySerializerTests, 'test_%s_forward_references_m2ms', forward_ref_m2m_test)
+register_tests(NaturalKeySerializerTests, 'test_%s_forward_references_m2m_errors', forward_ref_m2m_with_error_test)