diff options
| author | João Sampaio <jpmelos@gmail.com> | 2016-10-08 15:09:49 -0300 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-10-12 20:04:57 -0400 |
| commit | 74a575eb7296fb04e1fc2bd4e3f68dee3c66ee0a (patch) | |
| tree | dddf32453cd3d4f10c8d4c6bf64ab9fefcdeb4e7 /tests/serializers/test_natural.py | |
| parent | 794f866cecb7598c1537067cc1d932d95a86f439 (diff) | |
Fixed #24607 -- Serialized natural keys in multi-table inheritance models.
Diffstat (limited to 'tests/serializers/test_natural.py')
| -rw-r--r-- | tests/serializers/test_natural.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/tests/serializers/test_natural.py b/tests/serializers/test_natural.py index d8c4e9a7db..1a4f4d5ea0 100644 --- a/tests/serializers/test_natural.py +++ b/tests/serializers/test_natural.py @@ -4,7 +4,7 @@ from django.core import serializers from django.db import connection from django.test import TestCase -from .models import FKDataNaturalKey, NaturalKeyAnchor +from .models import Child, FKDataNaturalKey, NaturalKeyAnchor from .tests import register_tests @@ -69,6 +69,37 @@ def natural_key_test(format, self): self.assertIsNone(books[1].object.pk) +def natural_pk_mti_test(format, self): + """ + If serializing objects in a multi-table inheritance relationship using + natural primary keys, the natural foreign key for the parent is output in + the fields of the child so it's possible to relate the child to the parent + when deserializing. + """ + child_1 = Child.objects.create(parent_data='1', child_data='1') + child_2 = Child.objects.create(parent_data='2', child_data='2') + + string_data = serializers.serialize( + format, + [child_1.parent_ptr, child_2.parent_ptr, child_2, child_1], + use_natural_foreign_keys=True, use_natural_primary_keys=True, + ) + + child_1.delete() + child_2.delete() + + for obj in serializers.deserialize(format, string_data): + obj.save() + + children = Child.objects.all() + self.assertEqual(len(children), 2) + for child in children: + # If it's possible to find the superclass from the subclass and it's + # the correct superclass, it's working. + self.assertEqual(child.child_data, child.parent_data) + + # 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) |
