2015-09-26 09:53:20 +00:00
|
|
|
from django.core import serializers
|
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2018-11-22 21:11:51 +00:00
|
|
|
from .models import (
|
2022-02-03 19:24:19 +00:00
|
|
|
Child,
|
|
|
|
FKAsPKNoNaturalKey,
|
|
|
|
FKDataNaturalKey,
|
|
|
|
NaturalKeyAnchor,
|
|
|
|
NaturalKeyThing,
|
|
|
|
NaturalPKWithDefault,
|
2018-11-22 21:11:51 +00:00
|
|
|
)
|
2015-09-26 09:53:20 +00:00
|
|
|
from .tests import register_tests
|
|
|
|
|
|
|
|
|
|
|
|
class NaturalKeySerializerTests(TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-09-06 17:11:18 +00:00
|
|
|
def natural_key_serializer_test(self, format):
|
2015-09-26 09:53:20 +00:00
|
|
|
# Create all the objects defined in the test data
|
|
|
|
with connection.constraint_checks_disabled():
|
|
|
|
objects = [
|
|
|
|
NaturalKeyAnchor.objects.create(id=1100, data="Natural Key Anghor"),
|
|
|
|
FKDataNaturalKey.objects.create(id=1101, data_id=1100),
|
|
|
|
FKDataNaturalKey.objects.create(id=1102, data_id=None),
|
|
|
|
]
|
|
|
|
# Serialize the test database
|
2022-02-03 19:24:19 +00:00
|
|
|
serialized_data = serializers.serialize(
|
|
|
|
format, objects, indent=2, use_natural_foreign_keys=True
|
|
|
|
)
|
2015-09-26 09:53:20 +00:00
|
|
|
|
|
|
|
for obj in serializers.deserialize(format, serialized_data):
|
|
|
|
obj.save()
|
|
|
|
|
|
|
|
# Assert that the deserialized data is the same
|
|
|
|
# as the original source
|
|
|
|
for obj in objects:
|
|
|
|
instance = obj.__class__.objects.get(id=obj.pk)
|
2016-04-08 02:04:45 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
obj.data,
|
|
|
|
instance.data,
|
|
|
|
"Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)"
|
|
|
|
% (
|
|
|
|
obj.pk,
|
|
|
|
obj.data,
|
|
|
|
type(obj.data),
|
|
|
|
instance,
|
|
|
|
type(instance.data),
|
|
|
|
),
|
2015-09-26 09:53:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-09-06 17:11:18 +00:00
|
|
|
def natural_key_test(self, format):
|
2016-04-08 02:04:45 +00:00
|
|
|
book1 = {
|
2022-02-03 19:24:19 +00:00
|
|
|
"data": "978-1590597255",
|
|
|
|
"title": "The Definitive Guide to Django: Web Development Done Right",
|
2016-04-08 02:04:45 +00:00
|
|
|
}
|
2022-02-03 19:24:19 +00:00
|
|
|
book2 = {"data": "978-1590599969", "title": "Practical Django Projects"}
|
2015-09-26 09:53:20 +00:00
|
|
|
|
|
|
|
# Create the books.
|
|
|
|
adrian = NaturalKeyAnchor.objects.create(**book1)
|
|
|
|
james = NaturalKeyAnchor.objects.create(**book2)
|
|
|
|
|
|
|
|
# Serialize the books.
|
2016-04-08 02:04:45 +00:00
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
NaturalKeyAnchor.objects.all(),
|
|
|
|
indent=2,
|
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
use_natural_primary_keys=True,
|
2016-04-08 02:04:45 +00:00
|
|
|
)
|
2015-09-26 09:53:20 +00:00
|
|
|
|
|
|
|
# Delete one book (to prove that the natural key generation will only
|
|
|
|
# restore the primary keys of books found in the database via the
|
|
|
|
# get_natural_key manager method).
|
|
|
|
james.delete()
|
|
|
|
|
|
|
|
# Deserialize and test.
|
|
|
|
books = list(serializers.deserialize(format, string_data))
|
2022-04-14 10:54:27 +00:00
|
|
|
self.assertCountEqual(
|
|
|
|
[(book.object.title, book.object.pk) for book in books],
|
|
|
|
[
|
|
|
|
(book1["title"], adrian.pk),
|
|
|
|
(book2["title"], None),
|
|
|
|
],
|
|
|
|
)
|
2015-09-26 09:53:20 +00:00
|
|
|
|
|
|
|
|
2017-02-11 11:40:15 +00:00
|
|
|
def natural_pk_mti_test(self, format):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
child_1 = Child.objects.create(parent_data="1", child_data="1")
|
|
|
|
child_2 = Child.objects.create(parent_data="2", child_data="2")
|
2017-02-11 11:40:15 +00:00
|
|
|
string_data = serializers.serialize(
|
|
|
|
format,
|
|
|
|
[child_1.parent_ptr, child_2.parent_ptr, child_2, child_1],
|
2022-02-03 19:24:19 +00:00
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
use_natural_primary_keys=True,
|
2017-02-11 11:40:15 +00:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-07-13 21:54:47 +00:00
|
|
|
def forward_ref_fk_test(self, format):
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.create(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.create(key="t2", other_thing=t1)
|
2018-07-13 21:54:47 +00:00
|
|
|
t1.other_thing = t2
|
|
|
|
t1.save()
|
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
[t1, t2],
|
|
|
|
use_natural_primary_keys=True,
|
2018-07-13 21:54:47 +00:00
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
)
|
|
|
|
NaturalKeyThing.objects.all().delete()
|
|
|
|
objs_with_deferred_fields = []
|
2022-02-03 19:24:19 +00:00
|
|
|
for obj in serializers.deserialize(
|
|
|
|
format, string_data, handle_forward_references=True
|
|
|
|
):
|
2018-07-13 21:54:47 +00:00
|
|
|
obj.save()
|
|
|
|
if obj.deferred_fields:
|
|
|
|
objs_with_deferred_fields.append(obj)
|
|
|
|
for obj in objs_with_deferred_fields:
|
|
|
|
obj.save_deferred_fields()
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.get(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.get(key="t2")
|
2018-07-13 21:54:47 +00:00
|
|
|
self.assertEqual(t1.other_thing, t2)
|
|
|
|
self.assertEqual(t2.other_thing, t1)
|
|
|
|
|
|
|
|
|
|
|
|
def forward_ref_fk_with_error_test(self, format):
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.create(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.create(key="t2", other_thing=t1)
|
2018-07-13 21:54:47 +00:00
|
|
|
t1.other_thing = t2
|
|
|
|
t1.save()
|
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
[t1],
|
|
|
|
use_natural_primary_keys=True,
|
2018-07-13 21:54:47 +00:00
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
)
|
|
|
|
NaturalKeyThing.objects.all().delete()
|
|
|
|
objs_with_deferred_fields = []
|
2022-02-03 19:24:19 +00:00
|
|
|
for obj in serializers.deserialize(
|
|
|
|
format, string_data, handle_forward_references=True
|
|
|
|
):
|
2018-07-13 21:54:47 +00:00
|
|
|
obj.save()
|
|
|
|
if obj.deferred_fields:
|
|
|
|
objs_with_deferred_fields.append(obj)
|
|
|
|
obj = objs_with_deferred_fields[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "NaturalKeyThing matching query does not exist"
|
2018-07-13 21:54:47 +00:00
|
|
|
with self.assertRaisesMessage(serializers.base.DeserializationError, msg):
|
|
|
|
obj.save_deferred_fields()
|
|
|
|
|
|
|
|
|
|
|
|
def forward_ref_m2m_test(self, format):
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.create(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.create(key="t2")
|
|
|
|
t3 = NaturalKeyThing.objects.create(key="t3")
|
2018-07-13 21:54:47 +00:00
|
|
|
t1.other_things.set([t2, t3])
|
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
[t1, t2, t3],
|
|
|
|
use_natural_primary_keys=True,
|
2018-07-13 21:54:47 +00:00
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
)
|
|
|
|
NaturalKeyThing.objects.all().delete()
|
|
|
|
objs_with_deferred_fields = []
|
2022-02-03 19:24:19 +00:00
|
|
|
for obj in serializers.deserialize(
|
|
|
|
format, string_data, handle_forward_references=True
|
|
|
|
):
|
2018-07-13 21:54:47 +00:00
|
|
|
obj.save()
|
|
|
|
if obj.deferred_fields:
|
|
|
|
objs_with_deferred_fields.append(obj)
|
|
|
|
for obj in objs_with_deferred_fields:
|
|
|
|
obj.save_deferred_fields()
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.get(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.get(key="t2")
|
|
|
|
t3 = NaturalKeyThing.objects.get(key="t3")
|
2018-07-13 21:54:47 +00:00
|
|
|
self.assertCountEqual(t1.other_things.all(), [t2, t3])
|
|
|
|
|
|
|
|
|
|
|
|
def forward_ref_m2m_with_error_test(self, format):
|
2022-02-03 19:24:19 +00:00
|
|
|
t1 = NaturalKeyThing.objects.create(key="t1")
|
|
|
|
t2 = NaturalKeyThing.objects.create(key="t2")
|
|
|
|
t3 = NaturalKeyThing.objects.create(key="t3")
|
2018-07-13 21:54:47 +00:00
|
|
|
t1.other_things.set([t2, t3])
|
|
|
|
t1.save()
|
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
[t1, t2],
|
|
|
|
use_natural_primary_keys=True,
|
2018-07-13 21:54:47 +00:00
|
|
|
use_natural_foreign_keys=True,
|
|
|
|
)
|
|
|
|
NaturalKeyThing.objects.all().delete()
|
|
|
|
objs_with_deferred_fields = []
|
2022-02-03 19:24:19 +00:00
|
|
|
for obj in serializers.deserialize(
|
|
|
|
format, string_data, handle_forward_references=True
|
|
|
|
):
|
2018-07-13 21:54:47 +00:00
|
|
|
obj.save()
|
|
|
|
if obj.deferred_fields:
|
|
|
|
objs_with_deferred_fields.append(obj)
|
|
|
|
obj = objs_with_deferred_fields[0]
|
2022-02-03 19:24:19 +00:00
|
|
|
msg = "NaturalKeyThing matching query does not exist"
|
2018-07-13 21:54:47 +00:00
|
|
|
with self.assertRaisesMessage(serializers.base.DeserializationError, msg):
|
|
|
|
obj.save_deferred_fields()
|
|
|
|
|
|
|
|
|
2018-11-22 21:11:51 +00:00
|
|
|
def pk_with_default(self, format):
|
|
|
|
"""
|
|
|
|
The deserializer works with natural keys when the primary key has a default
|
|
|
|
value.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
obj = NaturalPKWithDefault.objects.create(name="name")
|
2018-11-22 21:11:51 +00:00
|
|
|
string_data = serializers.serialize(
|
2022-02-03 19:24:19 +00:00
|
|
|
format,
|
|
|
|
NaturalPKWithDefault.objects.all(),
|
|
|
|
use_natural_foreign_keys=True,
|
2018-11-22 21:11:51 +00:00
|
|
|
use_natural_primary_keys=True,
|
|
|
|
)
|
|
|
|
objs = list(serializers.deserialize(format, string_data))
|
|
|
|
self.assertEqual(len(objs), 1)
|
|
|
|
self.assertEqual(objs[0].object.pk, obj.pk)
|
|
|
|
|
|
|
|
|
2021-02-04 17:19:14 +00:00
|
|
|
def fk_as_pk_natural_key_not_called(self, format):
|
|
|
|
"""
|
|
|
|
The deserializer doesn't rely on natural keys when a model has a custom
|
|
|
|
primary key that is a ForeignKey.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
o1 = NaturalKeyAnchor.objects.create(data="978-1590599969")
|
2021-02-04 17:19:14 +00:00
|
|
|
o2 = FKAsPKNoNaturalKey.objects.create(pk_fk=o1)
|
|
|
|
serialized_data = serializers.serialize(format, [o1, o2])
|
|
|
|
deserialized_objects = list(serializers.deserialize(format, serialized_data))
|
|
|
|
self.assertEqual(len(deserialized_objects), 2)
|
|
|
|
for obj in deserialized_objects:
|
|
|
|
self.assertEqual(obj.object.pk, o1.pk)
|
|
|
|
|
|
|
|
|
2015-09-26 09:53:20 +00:00
|
|
|
# Dynamically register tests for each serializer
|
2021-02-04 17:19:14 +00:00
|
|
|
register_tests(
|
|
|
|
NaturalKeySerializerTests,
|
2022-02-03 19:24:19 +00:00
|
|
|
"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,
|
|
|
|
)
|
|
|
|
register_tests(NaturalKeySerializerTests, "test_%s_pk_with_default", pk_with_default)
|
|
|
|
register_tests(
|
|
|
|
NaturalKeySerializerTests,
|
|
|
|
"test_%s_fk_as_pk_natural_key_not_called",
|
2021-02-04 17:19:14 +00:00
|
|
|
fk_as_pk_natural_key_not_called,
|
|
|
|
)
|