Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#16885 closed Bug (fixed)

django.test.testcases.connections_support_transactions always returns False if TEST_MIRROR is used

Reported by: grimfandjango Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: m.vantellingen@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If a project is set up with multiple databases, and at least one of the databases uses the TEST_MIRROR option, django.test.testcases.connections_support_transactions will always return False, forcing any tests inheriting from TestCase into the (substantially slower) TransactionTestCase mode.

Example: take the test case:

    class TestTest(TestCase):
        def test_connections_support_transactions(self):
            import django.test.testcases
            self.assertTrue(django.test.testcases.connections_support_transactions())

and the database setup:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': '/tmp/foo.db',                      # Or path to database file if using sqlite3.
    }
    }


Test output:

    Creating test database for alias 'default'...
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.000s

    OK

Switch to:

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': '/tmp/foo.db',                      # Or path to database file if using sqlite3.
    },    
    'reporting': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': '/tmp/foo2.db',   
            'TEST_MIRROR': 'default',                 
    }
    }

Test output:

    FAIL: test_connections_support_transactions (foo.testit.tests.TestTest)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/../venvs/django_svn/foo/../foo/testit/tests.py", line 15, in test_connections_support_transactions
        self.assertTrue(django.test.testcases.connections_support_transactions())
    AssertionError: False is not True

The same problem occurs with the MySQL backend if configured analogously.

The underlying issue is that in conn.features.supports_transactions for conn in connections.all() in connections_support_transactions, conn.features.supports_transactions is None for the alias.

Attachments (1)

16885_fix.diff (2.1 KB ) - added by Michael 12 years ago.
Proposed fix + unittest

Download all attachments as: .zip

Change History (7)

comment:1 by m.vantellingen@…, 12 years ago

Cc: m.vantellingen@… added

comment:2 by Aymeric Augustin, 12 years ago

Triage Stage: UnreviewedAccepted

When TEST_MIRROR is set:

  • BaseDatabaseCreation.create_test_db is never called for the slave,
  • BaseDatabaseFeatures.confirm isn't called either,
  • connections['slave'].features.supports_transactions isn't computed, hence the problem.

I see two ways to fix this: call confirm for the slave, or copy the features from the master to the slave after the master database is created.

by Michael, 12 years ago

Attachment: 16885_fix.diff added

Proposed fix + unittest

comment:3 by Michael, 12 years ago

Not really sure about the unittest since there is no easy way to override the call_command() function call. Would be nice to get this in before 1.4.

comment:4 by Michael, 12 years ago

Has patch: set

comment:5 by Aymeric Augustin, 12 years ago

Resolution: fixed
Status: newclosed

In [17382]:

Fixed #16885 -- Confirmed features of mirror databases when setting up test databases.

comment:6 by Claude Paroz <claude@…>, 12 years ago

In [ad47364dd324508e8332ea853da59772431398aa]:

Reverted 905e33f, now that DatabaseFeatures does not need confirm

Connection.features does not need to be confirmed any more, after
commit aa42357, rendering obsolete the workaround when using
TEST_MIRROR (Refs #16885, #17760).

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