#31398: multiple_database.tests.AuthTestCase doesn't flush the default database 
if
transactions aren't supported
----------------------------------------+------------------------
               Reporter:  Tim Graham    |          Owner:  nobody
                   Type:  Bug           |         Status:  new
              Component:  Core (Other)  |        Version:  master
               Severity:  Normal        |       Keywords:
           Triage Stage:  Unreviewed    |      Has patch:  0
    Needs documentation:  0             |    Needs tests:  0
Patch needs improvement:  0             |  Easy pickings:  0
                  UI/UX:  0             |
----------------------------------------+------------------------
 On databases that don't support transactions, the database is flushed
 between test methods.

 In Django's test suite,
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/tests.py#L1527-L1577
 multiple_database.tests.AuthTestCase.test_auth_manager()] is the first
 test to run in that class. It creates two Users: alice in the 'other'
 database and bob in the 'default' database.

 The test class uses `AuthRouter` which has an
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/routers.py#L48-L52
 allow_migrate()] method that makes sure the auth app only appears in the
 'other' db, however, the test database creation doesn't respect that
 router and so the table is created in both databases (thus no errors when
 creating the users as described above).

 The problem comes during `TransactionTestCase. _fixture_teardown()` after
 the first test. It invokes the flush command which doesn't include any
 tables from the auth app because of the `allow_migrate()` method on the
 router.  (The flush command calls
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/core/management/commands/flush.py#L47
 django.core.management.sql.sql_flush] which calls
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/core/management/sql.py#L13
 BaseDatabaseIntrospection.django_table_names()] which calls
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/db/backends/base/introspection.py#L85
 router.get_migratable_models()] which eventually invokes
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/django/db/utils.py#L309-L320
 allow_migrate()].) Since the auth_user table isn't flushed after the first
 test, an `IntegrityError` (violating the unique username constraint) is
 raised when
 
[https://github.com/django/django/blob/8fe2447a01232d9598bf4692d1face12fa074288/tests/multiple_database/tests.py#L1564
 the second test creates bob in the 'default' database].

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31398>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.5e242047d4ad36f0b3fb12906bef5244%40djangoproject.com.

Reply via email to