Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit eedf16d12b675c50d0b0bab60ba8ad2e30a9921e 1 parent 6cffb9b
@freakboy3742 freakboy3742 authored
Showing with 23 additions and 48 deletions.
  1. +23 −48 tests/regressiontests/serializers_regress/tests.py
View
71 tests/regressiontests/serializers_regress/tests.py
@@ -10,14 +10,16 @@
import datetime
import decimal
-import unittest
-from cStringIO import StringIO
+try:
+ 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.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 *
@@ -59,10 +61,10 @@ def im2m_create(pk, klass, data):
def im_create(pk, klass, data):
instance = klass(id=pk)
- setattr(instance, 'right_id', data['right'])
- setattr(instance, 'left_id', data['left'])
+ instance.right_id = data['right']
+ instance.left_id = data['left']
if 'extra' in data:
- setattr(instance, 'extra', data['extra'])
+ instance.extra = data['extra']
models.Model.save_base(instance, raw=True)
return [instance]
@@ -96,7 +98,9 @@ def inherited_create(pk, klass, data):
def data_compare(testcase, pk, klass, data):
instance = klass.objects.get(id=pk)
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):
instance = klass.objects.get(id=pk)
@@ -348,28 +352,16 @@ def inherited_compare(testcase, pk, klass, data):
# Dynamically create serializer tests to ensure that all
# registered serializers are automatically tested.
-class SerializerTests(unittest.TestCase):
+class SerializerTests(TestCase):
pass
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
objects = []
instance_count = {}
- transaction.enter_transaction_management()
- try:
- 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()
+ for (func, pk, klass, datum) in test_data:
+ objects.extend(func[0](pk, klass, datum))
# Get a count of the number of objects created for each class
for klass in instance_count:
@@ -381,19 +373,8 @@ def serializerTest(format, self):
# Serialize the test database
serialized_data = serializers.serialize(format, objects, indent=2)
- # Flush the database and recreate from the serialized data
- management.call_command('flush', verbosity=0, interactive=False)
- 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()
+ for obj in serializers.deserialize(format, serialized_data):
+ obj.save()
# Assert that the deserialized data is the same
# as the original source
@@ -406,10 +387,7 @@ def serializerTest(format, self):
self.assertEquals(count, klass.objects.count())
def fieldsTest(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)
# Serialize then deserialize the test database
@@ -422,9 +400,6 @@ def fieldsTest(format, self):
self.assertEqual(result.object.field3, 'third')
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.save_base(raw=True)
@@ -440,7 +415,7 @@ def streamTest(format, self):
stream.close()
for format in serializers.get_serializer_formats():
- setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format))
- setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
if format != 'python':
- setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(streamTest, format))
+ setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
Please sign in to comment.
Something went wrong with that request. Please try again.