Ticket #13638: django-serializer-regress-tests.diff

File django-serializer-regress-tests.diff, 5.4 KB (added by Alex Gaynor, 14 years ago)
  • tests/regressiontests/serializers_regress/tests.py

    diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py
    index 84e90ff..be920c6 100644
    a b forward, backwards and self references.  
    1010
    1111import datetime
    1212import decimal
    13 import unittest
    14 from cStringIO import StringIO
     13try:
     14    from cStringIO import StringIO
     15except ImportError:
     16    from StringIO import StringIO
    1517
    16 from django.utils.functional import curry
    17 from django.core import serializers
    18 from django.db import transaction, DEFAULT_DB_ALIAS
    19 from django.core import management
    2018from django.conf import settings
     19from django.core import serializers, management
     20from django.db import transaction, DEFAULT_DB_ALIAS
     21from django.test import TestCase
     22from django.utils.functional import curry
    2123
    2224from models import *
    2325
    def im2m_create(pk, klass, data):  
    5961
    6062def im_create(pk, klass, data):
    6163    instance = klass(id=pk)
    62     setattr(instance, 'right_id', data['right'])
    63     setattr(instance, 'left_id', data['left'])
     64    instance.right_id = data['right']
     65    instance.left_id = data['left']
    6466    if 'extra' in data:
    65         setattr(instance, 'extra', data['extra'])
     67        instance.extra = data['extra']
    6668    models.Model.save_base(instance, raw=True)
    6769    return [instance]
    6870
    def inherited_create(pk, klass, data):  
    9698def data_compare(testcase, pk, klass, data):
    9799    instance = klass.objects.get(id=pk)
    98100    testcase.assertEqual(data, instance.data,
    99                          "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (pk,data, type(data), instance.data, type(instance.data)))
     101         "Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)" % (
     102            pk, data, type(data), instance.data, type(instance.data))
     103    )
    100104
    101105def generic_compare(testcase, pk, klass, data):
    102106    instance = klass.objects.get(id=pk)
    if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':  
    348352
    349353# Dynamically create serializer tests to ensure that all
    350354# registered serializers are automatically tested.
    351 class SerializerTests(unittest.TestCase):
     355class SerializerTests(TestCase):
    352356    pass
    353357
    354358def serializerTest(format, self):
    355     # Clear the database first
    356     management.call_command('flush', verbosity=0, interactive=False)
    357359
    358360    # Create all the objects defined in the test data
    359361    objects = []
    360362    instance_count = {}
    361     transaction.enter_transaction_management()
    362     try:
    363         transaction.managed(True)
    364         for (func, pk, klass, datum) in test_data:
    365             objects.extend(func[0](pk, klass, datum))
    366             instance_count[klass] = 0
    367         transaction.commit()
    368     except:
    369         transaction.rollback()
    370         transaction.leave_transaction_management()
    371         raise
    372     transaction.leave_transaction_management()
     363    for (func, pk, klass, datum) in test_data:
     364        objects.extend(func[0](pk, klass, datum))
    373365
    374366    # Get a count of the number of objects created for each class
    375367    for klass in instance_count:
    def serializerTest(format, self):  
    381373    # Serialize the test database
    382374    serialized_data = serializers.serialize(format, objects, indent=2)
    383375
    384     # Flush the database and recreate from the serialized data
    385     management.call_command('flush', verbosity=0, interactive=False)
    386     transaction.enter_transaction_management()
    387     try:
    388         transaction.managed(True)
    389         for obj in serializers.deserialize(format, serialized_data):
    390             obj.save()
    391         transaction.commit()
    392     except:
    393         transaction.rollback()
    394         transaction.leave_transaction_management()
    395         raise
    396     transaction.leave_transaction_management()
     376    for obj in serializers.deserialize(format, serialized_data):
     377        obj.save()
    397378
    398379    # Assert that the deserialized data is the same
    399380    # as the original source
    def serializerTest(format, self):  
    406387        self.assertEquals(count, klass.objects.count())
    407388
    408389def fieldsTest(format, self):
    409     # Clear the database first
    410     management.call_command('flush', verbosity=0, interactive=False)
    411 
    412     obj = ComplexModel(field1='first',field2='second',field3='third')
     390    obj = ComplexModel(field1='first', field2='second', field3='third')
    413391    obj.save_base(raw=True)
    414392
    415393    # Serialize then deserialize the test database
    def fieldsTest(format, self):  
    422400    self.assertEqual(result.object.field3, 'third')
    423401
    424402def streamTest(format, self):
    425     # Clear the database first
    426     management.call_command('flush', verbosity=0, interactive=False)
    427 
    428403    obj = ComplexModel(field1='first',field2='second',field3='third')
    429404    obj.save_base(raw=True)
    430405
    def streamTest(format, self):  
    440415    stream.close()
    441416
    442417for format in serializers.get_serializer_formats():
    443     setattr(SerializerTests, 'test_'+format+'_serializer', curry(serializerTest, format))
    444     setattr(SerializerTests, 'test_'+format+'_serializer_fields', curry(fieldsTest, format))
     418    setattr(SerializerTests, 'test_' + format + '_serializer', curry(serializerTest, format))
     419    setattr(SerializerTests, 'test_' + format + '_serializer_fields', curry(fieldsTest, format))
    445420    if format != 'python':
    446         setattr(SerializerTests, 'test_'+format+'_serializer_stream', curry(streamTest, format))
     421        setattr(SerializerTests, 'test_' + format + '_serializer_stream', curry(streamTest, format))
Back to Top