Fixed #13638 -- Refactored the serializers_regress tests to avoid the use of flush, and make better use of the transactional capabilities of django.test.TestCase. Thanks to Alex Gaynor for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13308 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
6cffb9b880
commit
eedf16d12b
|
@ -10,14 +10,16 @@ forward, backwards and self references.
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import unittest
|
try:
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
except ImportError:
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
from django.utils.functional import curry
|
|
||||||
from django.core import serializers
|
|
||||||
from django.db import transaction, DEFAULT_DB_ALIAS
|
|
||||||
from django.core import management
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core import serializers, management
|
||||||
|
from django.db import transaction, DEFAULT_DB_ALIAS
|
||||||
|
from django.test import TestCase
|
||||||
|
from django.utils.functional import curry
|
||||||
|
|
||||||
from models import *
|
from models import *
|
||||||
|
|
||||||
|
@ -59,10 +61,10 @@ def im2m_create(pk, klass, data):
|
||||||
|
|
||||||
def im_create(pk, klass, data):
|
def im_create(pk, klass, data):
|
||||||
instance = klass(id=pk)
|
instance = klass(id=pk)
|
||||||
setattr(instance, 'right_id', data['right'])
|
instance.right_id = data['right']
|
||||||
setattr(instance, 'left_id', data['left'])
|
instance.left_id = data['left']
|
||||||
if 'extra' in data:
|
if 'extra' in data:
|
||||||
setattr(instance, 'extra', data['extra'])
|
instance.extra = data['extra']
|
||||||
models.Model.save_base(instance, raw=True)
|
models.Model.save_base(instance, raw=True)
|
||||||
return [instance]
|
return [instance]
|
||||||
|
|
||||||
|
@ -96,7 +98,9 @@ def inherited_create(pk, klass, data):
|
||||||
def data_compare(testcase, pk, klass, data):
|
def data_compare(testcase, pk, klass, data):
|
||||||
instance = klass.objects.get(id=pk)
|
instance = klass.objects.get(id=pk)
|
||||||
testcase.assertEqual(data, instance.data,
|
testcase.assertEqual(data, instance.data,
|
||||||
"Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (pk,data, type(data), instance.data, type(instance.data)))
|
"Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (
|
||||||
|
pk, data, type(data), instance.data, type(instance.data))
|
||||||
|
)
|
||||||
|
|
||||||
def generic_compare(testcase, pk, klass, data):
|
def generic_compare(testcase, pk, klass, data):
|
||||||
instance = klass.objects.get(id=pk)
|
instance = klass.objects.get(id=pk)
|
||||||
|
@ -348,28 +352,16 @@ if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
|
||||||
|
|
||||||
# Dynamically create serializer tests to ensure that all
|
# Dynamically create serializer tests to ensure that all
|
||||||
# registered serializers are automatically tested.
|
# registered serializers are automatically tested.
|
||||||
class SerializerTests(unittest.TestCase):
|
class SerializerTests(TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def serializerTest(format, self):
|
def serializerTest(format, self):
|
||||||
# Clear the database first
|
|
||||||
management.call_command('flush', verbosity=0, interactive=False)
|
|
||||||
|
|
||||||
# Create all the objects defined in the test data
|
# Create all the objects defined in the test data
|
||||||
objects = []
|
objects = []
|
||||||
instance_count = {}
|
instance_count = {}
|
||||||
transaction.enter_transaction_management()
|
for (func, pk, klass, datum) in test_data:
|
||||||
try:
|
objects.extend(func[0](pk, klass, datum))
|
||||||
transaction.managed(True)
|
|
||||||
for (func, pk, klass, datum) in test_data:
|
|
||||||
objects.extend(func[0](pk, klass, datum))
|
|
||||||
instance_count[klass] = 0
|
|
||||||
transaction.commit()
|
|
||||||
except:
|
|
||||||
transaction.rollback()
|
|
||||||
transaction.leave_transaction_management()
|
|
||||||
raise
|
|
||||||
transaction.leave_transaction_management()
|
|
||||||
|
|
||||||
# Get a count of the number of objects created for each class
|
# Get a count of the number of objects created for each class
|
||||||
for klass in instance_count:
|
for klass in instance_count:
|
||||||
|
@ -381,19 +373,8 @@ def serializerTest(format, self):
|
||||||
# Serialize the test database
|
# Serialize the test database
|
||||||
serialized_data = serializers.serialize(format, objects, indent=2)
|
serialized_data = serializers.serialize(format, objects, indent=2)
|
||||||
|
|
||||||
# Flush the database and recreate from the serialized data
|
for obj in serializers.deserialize(format, serialized_data):
|
||||||
management.call_command('flush', verbosity=0, interactive=False)
|
obj.save()
|
||||||
transaction.enter_transaction_management()
|
|
||||||
try:
|
|
||||||
transaction.managed(True)
|
|
||||||
for obj in serializers.deserialize(format, serialized_data):
|
|
||||||
obj.save()
|
|
||||||
transaction.commit()
|
|
||||||
except:
|
|
||||||
transaction.rollback()
|
|
||||||
transaction.leave_transaction_management()
|
|
||||||
raise
|
|
||||||
transaction.leave_transaction_management()
|
|
||||||
|
|
||||||
# Assert that the deserialized data is the same
|
# Assert that the deserialized data is the same
|
||||||
# as the original source
|
# as the original source
|
||||||
|
@ -406,10 +387,7 @@ def serializerTest(format, self):
|
||||||
self.assertEquals(count, klass.objects.count())
|
self.assertEquals(count, klass.objects.count())
|
||||||
|
|
||||||
def fieldsTest(format, self):
|
def fieldsTest(format, self):
|
||||||
# Clear the database first
|
obj = ComplexModel(field1='first', field2='second', field3='third')
|
||||||
management.call_command('flush', verbosity=0, interactive=False)
|
|
||||||
|
|
||||||
obj = ComplexModel(field1='first',field2='second',field3='third')
|
|
||||||
obj.save_base(raw=True)
|
obj.save_base(raw=True)
|
||||||
|
|
||||||
# Serialize then deserialize the test database
|
# Serialize then deserialize the test database
|
||||||
|
@ -422,9 +400,6 @@ def fieldsTest(format, self):
|
||||||
self.assertEqual(result.object.field3, 'third')
|
self.assertEqual(result.object.field3, 'third')
|
||||||
|
|
||||||
def streamTest(format, self):
|
def streamTest(format, self):
|
||||||
# Clear the database first
|
|
||||||
management.call_command('flush', verbosity=0, interactive=False)
|
|
||||||
|
|
||||||
obj = ComplexModel(field1='first',field2='second',field3='third')
|
obj = ComplexModel(field1='first',field2='second',field3='third')
|
||||||
obj.save_base(raw=True)
|
obj.save_base(raw=True)
|
||||||
|
|
||||||
|
@ -440,7 +415,7 @@ def streamTest(format, self):
|
||||||
stream.close()
|
stream.close()
|
||||||
|
|
||||||
for format in serializers.get_serializer_formats():
|
for format in serializers.get_serializer_formats():
|
||||||
setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format))
|
setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
|
||||||
setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format))
|
setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
|
||||||
if format != 'python':
|
if format != 'python':
|
||||||
setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(streamTest, format))
|
setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
|
||||||
|
|
Loading…
Reference in New Issue