Ticket #7981: 7981_serializers_regress_tests_fix_transaction_management.diff

File 7981_serializers_regress_tests_fix_transaction_management.diff, 3.2 KB (added by Leo Soto M., 16 years ago)
  • tests/regressiontests/serializers_regress/tests.py

    diff -r 85e7a05ad3de tests/regressiontests/serializers_regress/tests.py
    a b  
    7272    #  1) we're testing inheritance, not field behaviour, so none
    7373    #     of the field values need to be protected.
    7474    #  2) saving the child class and having the parent created
    75     #     automatically is easier than manually creating both. 
     75    #     automatically is easier than manually creating both.
    7676    models.Model.save(instance)
    7777    created = [instance]
    7878    for klass,field in instance._meta.parents.items():
    7979        created.append(klass.objects.get(id=pk))
    8080    return created
    81    
     81
    8282# A set of functions that can be used to compare
    8383# test data objects of various kinds
    8484def data_compare(testcase, pk, klass, data):
     
    111111    instance = klass.objects.get(id=pk)
    112112    for key,value in data.items():
    113113        testcase.assertEqual(value, getattr(instance,key))
    114    
     114
    115115# Define some data types. Each data type is
    116116# actually a pair of functions; one to create
    117117# and one to compare objects of that type
     
    274274
    275275    (data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006,6,16,10,42,37)),
    276276    (data_obj, 810, ModifyingSaveData, 42),
    277    
     277
    278278    (inherited_obj, 900, InheritAbstractModel, {'child_data':37,'parent_data':42}),
    279279    (inherited_obj, 910, ExplicitInheritBaseModel, {'child_data':37,'parent_data':42}),
    280280    (inherited_obj, 920, InheritBaseModel, {'child_data':37,'parent_data':42}),
     
    302302    objects = []
    303303    instance_count = {}
    304304    transaction.enter_transaction_management()
    305     transaction.managed(True)
    306     for (func, pk, klass, datum) in test_data:
    307         objects.extend(func[0](pk, klass, datum))
    308         instance_count[klass] = 0
    309     transaction.commit()
    310     transaction.leave_transaction_management()
     305    try:
     306        transaction.managed(True)
     307        for (func, pk, klass, datum) in test_data:
     308            objects.extend(func[0](pk, klass, datum))
     309            instance_count[klass] = 0
     310        transaction.commit()
     311    finally:
     312        transaction.leave_transaction_management()
    311313
    312314    # Get a count of the number of objects created for each class
    313315    for klass in instance_count:
    314316        instance_count[klass] = klass.objects.count()
    315        
     317
    316318    # Add the generic tagged objects to the object list
    317319    objects.extend(Tag.objects.all())
    318320
     
    322324    # Flush the database and recreate from the serialized data
    323325    management.call_command('flush', verbosity=0, interactive=False)
    324326    transaction.enter_transaction_management()
    325     transaction.managed(True)
    326     for obj in serializers.deserialize(format, serialized_data):
    327         obj.save()
    328     transaction.commit()
    329     transaction.leave_transaction_management()
     327    try:
     328        transaction.managed(True)
     329        for obj in serializers.deserialize(format, serialized_data):
     330            obj.save()
     331        transaction.commit()
     332    finally:
     333        transaction.leave_transaction_management()
    330334
    331335    # Assert that the deserialized data is the same
    332336    # as the original source
Back to Top