1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Improved assert_serializer test assertions in test.serializers.

This commit is contained in:
Sarah Boyce 2024-11-28 12:07:21 +01:00
parent a444d2600b
commit d783a6f1c0

View File

@ -10,6 +10,7 @@ forward, backwards and self references.
import datetime import datetime
import decimal import decimal
import uuid import uuid
from collections import namedtuple
from django.core import serializers from django.core import serializers
from django.db import connection, models from django.db import connection, models
@ -239,22 +240,22 @@ def inherited_compare(testcase, pk, klass, data):
testcase.assertEqual(value, getattr(instance, key)) testcase.assertEqual(value, getattr(instance, key))
# Define some data types. Each data type is # Define some test helpers. Each has a pair of functions: one to create objects and one
# actually a pair of functions; one to create # to make assertions against objects of a particular type.
# and one to compare objects of that type TestHelper = namedtuple("TestHelper", ["create_object", "compare_object"])
data_obj = (data_create, data_compare) data_obj = TestHelper(data_create, data_compare)
generic_obj = (generic_create, generic_compare) generic_obj = TestHelper(generic_create, generic_compare)
fk_obj = (fk_create, fk_compare) fk_obj = TestHelper(fk_create, fk_compare)
m2m_obj = (m2m_create, m2m_compare) m2m_obj = TestHelper(m2m_create, m2m_compare)
im2m_obj = (im2m_create, im2m_compare) im2m_obj = TestHelper(im2m_create, im2m_compare)
im_obj = (im_create, im_compare) im_obj = TestHelper(im_create, im_compare)
o2o_obj = (o2o_create, o2o_compare) o2o_obj = TestHelper(o2o_create, o2o_compare)
pk_obj = (pk_create, pk_compare) pk_obj = TestHelper(pk_create, pk_compare)
inherited_obj = (inherited_create, inherited_compare) inherited_obj = TestHelper(inherited_create, inherited_compare)
uuid_obj = uuid.uuid4() uuid_obj = uuid.uuid4()
test_data = [ test_data = [
# Format: (data type, PK value, Model Class, data) # Format: (test helper, PK value, Model Class, data)
(data_obj, 1, BinaryData, memoryview(b"\x05\xFD\x00")), (data_obj, 1, BinaryData, memoryview(b"\x05\xFD\x00")),
(data_obj, 5, BooleanData, True), (data_obj, 5, BooleanData, True),
(data_obj, 6, BooleanData, False), (data_obj, 6, BooleanData, False),
@ -410,35 +411,36 @@ class SerializerDataTests(TestCase):
def assert_serializer(self, format, data): def assert_serializer(self, format, data):
# Create all the objects defined in the test data # Create all the objects defined in the test data.
objects = [] objects = []
instance_count = {} for test_helper, pk, model, data_value in data:
for func, pk, klass, datum in test_data:
with connection.constraint_checks_disabled(): with connection.constraint_checks_disabled():
objects.extend(func[0](pk, klass, datum)) objects.extend(test_helper.create_object(pk, model, data_value))
# Get a count of the number of objects created for each class # Get a count of the number of objects created for each model class.
for klass in instance_count: instance_counts = {}
instance_count[klass] = klass.objects.count() for _, _, model, _ in data:
if model not in instance_counts:
instance_counts[model] = model.objects.count()
# Add the generic tagged objects to the object list # Add the generic tagged objects to the object list.
objects.extend(Tag.objects.all()) objects.extend(Tag.objects.all())
# Serialize the test database # Serialize the test database.
serialized_data = serializers.serialize(format, objects, indent=2) serialized_data = serializers.serialize(format, objects, indent=2)
for obj in serializers.deserialize(format, serialized_data): for obj in serializers.deserialize(format, serialized_data):
obj.save() obj.save()
# Assert that the deserialized data is the same # Assert that the deserialized data is the same as the original source.
# as the original source for test_helper, pk, model, data_value in data:
for func, pk, klass, datum in test_data: with self.subTest(model=model, data_value=data_value):
func[1](self, pk, klass, datum) test_helper.compare_object(self, pk, model, data_value)
# Assert that the number of objects deserialized is the # Assert no new objects were created.
# same as the number that was serialized. for model, count in instance_counts.items():
for klass, count in instance_count.items(): with self.subTest(model=model, count=count):
self.assertEqual(count, klass.objects.count()) self.assertEqual(count, model.objects.count())
def serializerTest(self, format): def serializerTest(self, format):