2015-09-26 11:53:20 +02:00
|
|
|
from django.core import serializers
|
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2017-02-11 12:31:20 +01:00
|
|
|
from .models import FKDataNaturalKey, NaturalKeyAnchor
|
2015-09-26 11:53:20 +02:00
|
|
|
from .tests import register_tests
|
|
|
|
|
|
|
|
|
|
|
|
class NaturalKeySerializerTests(TestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-09-06 19:11:18 +02:00
|
|
|
def natural_key_serializer_test(self, format):
|
2015-09-26 11:53:20 +02: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
|
2016-04-08 04:04:45 +02:00
|
|
|
serialized_data = serializers.serialize(format, objects, indent=2, use_natural_foreign_keys=True)
|
2015-09-26 11:53:20 +02: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 04:04:45 +02:00
|
|
|
self.assertEqual(
|
|
|
|
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 11:53:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-09-06 19:11:18 +02:00
|
|
|
def natural_key_test(self, format):
|
2016-04-08 04:04:45 +02:00
|
|
|
book1 = {
|
|
|
|
'data': '978-1590597255',
|
|
|
|
'title': 'The Definitive Guide to Django: Web Development Done Right',
|
|
|
|
}
|
2015-09-26 11:53:20 +02:00
|
|
|
book2 = {'data': '978-1590599969', 'title': 'Practical Django Projects'}
|
|
|
|
|
|
|
|
# Create the books.
|
|
|
|
adrian = NaturalKeyAnchor.objects.create(**book1)
|
|
|
|
james = NaturalKeyAnchor.objects.create(**book2)
|
|
|
|
|
|
|
|
# Serialize the books.
|
2016-04-08 04:04:45 +02:00
|
|
|
string_data = serializers.serialize(
|
|
|
|
format, NaturalKeyAnchor.objects.all(), indent=2,
|
|
|
|
use_natural_foreign_keys=True, use_natural_primary_keys=True,
|
|
|
|
)
|
2015-09-26 11:53:20 +02: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))
|
|
|
|
self.assertEqual(len(books), 2)
|
|
|
|
self.assertEqual(books[0].object.title, book1['title'])
|
|
|
|
self.assertEqual(books[0].object.pk, adrian.pk)
|
|
|
|
self.assertEqual(books[1].object.title, book2['title'])
|
2016-06-16 20:19:18 +02:00
|
|
|
self.assertIsNone(books[1].object.pk)
|
2015-09-26 11:53:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
# 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)
|