Code

Opened 3 years ago

Closed 2 years ago

Last modified 2 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: master
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 mvantellingen 3 years ago.
Proposed fix + unittest

Download all attachments as: .zip

Change History (7)

comment:1 Changed 3 years ago by m.vantellingen@…

  • Cc m.vantellingen@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by aaugustin

  • Triage Stage changed from Unreviewed to Accepted

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.

Changed 3 years ago by mvantellingen

Proposed fix + unittest

comment:3 Changed 3 years ago by mvantellingen

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 Changed 3 years ago by mvantellingen

  • Has patch set

comment:5 Changed 2 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

In [17382]:

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

comment:6 Changed 2 years ago by Claude Paroz <claude@…>

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).

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.