summaryrefslogtreecommitdiff
path: root/tests/serializers
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2015-09-26 11:53:20 +0200
committerClaude Paroz <claude@2xlibre.net>2015-09-26 20:03:54 +0200
commita74728b36d8e1a83b3dffb85ea2601b7ec3bcc2d (patch)
tree79f604185ccc01c90a480663ec178509fed05139 /tests/serializers
parentd31c38cddd986a9df750a2332737d7a14da1a80d (diff)
[1.9.x] Separated natural key serialization tests
Backport of d59d3caf3 from master.
Diffstat (limited to 'tests/serializers')
-rw-r--r--tests/serializers/models.py21
-rw-r--r--tests/serializers/test_natural.py70
-rw-r--r--tests/serializers/tests.py16
3 files changed, 107 insertions, 0 deletions
diff --git a/tests/serializers/models.py b/tests/serializers/models.py
index 6532ea953e..c3663b5339 100644
--- a/tests/serializers/models.py
+++ b/tests/serializers/models.py
@@ -157,3 +157,24 @@ class Player(models.Model):
def __str__(self):
return '%s (%d) playing for %s' % (self.name, self.rank, self.team.to_string())
+
+
+# ******** Models for test_natural.py ***********
+
+class NaturalKeyAnchorManager(models.Manager):
+ def get_by_natural_key(self, data):
+ return self.get(data=data)
+
+
+class NaturalKeyAnchor(models.Model):
+ objects = NaturalKeyAnchorManager()
+
+ data = models.CharField(max_length=100, unique=True)
+ title = models.CharField(max_length=100, null=True)
+
+ def natural_key(self):
+ return (self.data,)
+
+
+class FKDataNaturalKey(models.Model):
+ data = models.ForeignKey(NaturalKeyAnchor, models.SET_NULL, null=True)
diff --git a/tests/serializers/test_natural.py b/tests/serializers/test_natural.py
new file mode 100644
index 0000000000..6682e5cdd8
--- /dev/null
+++ b/tests/serializers/test_natural.py
@@ -0,0 +1,70 @@
+from __future__ import unicode_literals
+
+from django.core import serializers
+from django.db import connection
+from django.test import TestCase
+
+from .models import FKDataNaturalKey, NaturalKeyAnchor
+from .tests import register_tests
+
+
+class NaturalKeySerializerTests(TestCase):
+ pass
+
+
+def natural_key_serializer_test(format, self):
+ # 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
+ serialized_data = serializers.serialize(format, objects, indent=2,
+ use_natural_foreign_keys=True)
+
+ 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)
+ 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))
+ )
+
+
+def natural_key_test(format, self):
+ book1 = {'data': '978-1590597255', 'title': 'The Definitive Guide to '
+ 'Django: Web Development Done Right'}
+ book2 = {'data': '978-1590599969', 'title': 'Practical Django Projects'}
+
+ # Create the books.
+ adrian = NaturalKeyAnchor.objects.create(**book1)
+ james = NaturalKeyAnchor.objects.create(**book2)
+
+ # Serialize the books.
+ string_data = serializers.serialize(format, NaturalKeyAnchor.objects.all(),
+ indent=2, use_natural_foreign_keys=True,
+ use_natural_primary_keys=True)
+
+ # 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'])
+ self.assertEqual(books[1].object.pk, None)
+
+
+# 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)
diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py
index ffd573c0cc..2de9ce8796 100644
--- a/tests/serializers/tests.py
+++ b/tests/serializers/tests.py
@@ -10,6 +10,7 @@ from django.test import (
SimpleTestCase, mock, override_settings, skipUnlessDBFeature,
)
from django.test.utils import Approximate
+from django.utils.functional import curry
from django.utils.six import StringIO
from .models import (
@@ -317,3 +318,18 @@ class SerializersTransactionTestBase(object):
art_obj = Article.objects.all()[0]
self.assertEqual(art_obj.categories.all().count(), 1)
self.assertEqual(art_obj.author.name, "Agnes")
+
+
+def register_tests(test_class, method_name, test_func, exclude=None):
+ """
+ Dynamically create serializer tests to ensure that all registered
+ serializers are automatically tested.
+ """
+ formats = [
+ f for f in serializers.get_serializer_formats()
+ if (not isinstance(serializers.get_serializer(f), serializers.BadSerializer)
+ and not f == 'geojson'
+ and (exclude is None or f not in exclude))
+ ]
+ for format_ in formats:
+ setattr(test_class, method_name % format_, curry(test_func, format_))