Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20142 closed Bug (fixed)

Transaction methods are not re-enabled when fixture loading fails

Reported by: Martin Larente Owned by: Filipa Andrade
Component: Testing framework Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

When a test case fails to properly load a fixture (integrity error or some other reason), the transaction methods (which are disabled before the test is run) are not re-enabled before the next test is run. This can cause other tests (especially transaction test cases) to fail.

I've created a simple django application to reproduce this. You just have to add the application to a Django project, and then run:

manage.py test myapp.Before myapp.Broken myapp.After

This command is used to enforce the test order that shows the bug. The Before and After tests are actually the same test that checks if transaction methods are enabled (as they should be for a transaction test case). The first one passes and the other one fails:

import django.db.transaction
from django.test.testcases import TestCase
from django.test.testcases import TransactionTestCase

ORIGINAL = django.db.transaction.enter_transaction_management


class TransactionCheckMixin(object):
    def test_transaction_method(self):
        self.assertEqual(ORIGINAL, django.db.transaction.enter_transaction_management)


class Before(TransactionCheckMixin, TransactionTestCase):
    pass


class Broken(TestCase):
    fixtures = ['broken_fixture']

    def test_something(self):
        self.assertTrue(True)  # will not happen


class After(TransactionCheckMixin, TransactionTestCase):
    pass

The problem lies in the _fixture_setup method of the TestCase class:

class TestCase(TransactionTestCase):
    """
    Does basically the same as TransactionTestCase, but surrounds every test
    with a transaction, monkey-patches the real transaction management routines
    to do nothing, and rollsback the test transaction at the end of the test.
    You have to use TransactionTestCase, if you need transaction management
    inside a test.
    """

    def _fixture_setup(self):
        if not connections_support_transactions():
            return super(TestCase, self)._fixture_setup()

        assert not self.reset_sequences, 'reset_sequences cannot be used on TestCase instances'

        for db_name in self._databases_names():
            transaction.enter_transaction_management(using=db_name)
            transaction.managed(True, using=db_name)
        disable_transaction_methods()

        from django.contrib.sites.models import Site
        Site.objects.clear_cache()

        for db in self._databases_names(include_mirrors=False):
            if hasattr(self, 'fixtures'):
                call_command('loaddata', *self.fixtures,
                             **{
                                'verbosity': 0,
                                'commit': False,
                                'database': db,
                                'skip_validation': True,
                             })

It should handle errors and call the _fixture_teardown method if there's an error.

Attachments (1)

transaction-method-test.zip (1.7 KB ) - added by Martin Larente 11 years ago.
Simple app that show the bug

Download all attachments as: .zip

Change History (5)

by Martin Larente, 11 years ago

Attachment: transaction-method-test.zip added

Simple app that show the bug

comment:1 by Filipa Andrade, 11 years ago

Owner: changed from nobody to Filipa Andrade
Status: newassigned
Triage Stage: UnreviewedAccepted

Confirmed the bug in django 1.5 and development version.

Last edited 11 years ago by Filipa Andrade (previous) (diff)

comment:2 by Filipa Andrade, 11 years ago

Easy pickings: set
Has patch: set

https://github.com/django/django/pull/1127

This fix does not contain tests as it is impossible to test it without mocking and a decision about using mocks in the project as yet not been made. This was discussed and agreed with Russel.

comment:3 by Filipa Andrade <filipa.andrade@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 5883ae56b3e993052c46f7d9e756465b1387dd34:

Fixed #20142 -- Added error handling for fixture setup

TestCase._fixture_setup disables transactions so,
in case of an error, cleanup is needed to re-enable
transactions, otherwise following TransactionTestCase
would fail.

comment:4 by Aymeric Augustin <aymeric.augustin@…>, 11 years ago

In 5915800debef7cec01672dc0b467b7c2bd3aba03:

Merge pull request #1127 from filias/20142

Fixed #20142 -- Added error handling for fixture setup

Note: See TracTickets for help on using tickets.
Back to Top