mirror of
https://github.com/django/django.git
synced 2025-10-25 14:46:09 +00:00
Fixed #20579 -- Improved TransactionTestCase.available_apps.
Also moved its documentation to the 'advanced' section. It doesn't belong to the 'overview'. Same for TransactionTestCase.reset_sequences. When available_apps is set, after a TransactionTestCase, the database is now totally empty. post_syncdb is fired at the beginning of the next TransactionTestCase. Refs #20483.
This commit is contained in:
@@ -24,6 +24,7 @@ from django.core.exceptions import ValidationError, ImproperlyConfigured
|
||||
from django.core.handlers.wsgi import WSGIHandler
|
||||
from django.core.management import call_command
|
||||
from django.core.management.color import no_style
|
||||
from django.core.management.commands import flush
|
||||
from django.core.servers.basehttp import (WSGIRequestHandler, WSGIServer,
|
||||
WSGIServerException)
|
||||
from django.core.urlresolvers import clear_url_caches, set_urlconf
|
||||
@@ -196,9 +197,9 @@ class SimpleTestCase(ut2.TestCase):
|
||||
def _pre_setup(self):
|
||||
"""Performs any pre-test setup. This includes:
|
||||
|
||||
* If the Test Case class has a 'urls' member, replace the
|
||||
ROOT_URLCONF with it.
|
||||
* Clearing the mail test outbox.
|
||||
* Creating a test client.
|
||||
* If the class has a 'urls' attribute, replace ROOT_URLCONF with it.
|
||||
* Clearing the mail test outbox.
|
||||
"""
|
||||
self.client = self.client_class()
|
||||
self._urlconf_setup()
|
||||
@@ -212,6 +213,10 @@ class SimpleTestCase(ut2.TestCase):
|
||||
clear_url_caches()
|
||||
|
||||
def _post_teardown(self):
|
||||
"""Performs any post-test things. This includes:
|
||||
|
||||
* Putting back the original ROOT_URLCONF if it was changed.
|
||||
"""
|
||||
self._urlconf_teardown()
|
||||
|
||||
def _urlconf_teardown(self):
|
||||
@@ -732,13 +737,17 @@ class TransactionTestCase(SimpleTestCase):
|
||||
def _pre_setup(self):
|
||||
"""Performs any pre-test setup. This includes:
|
||||
|
||||
* Flushing the database.
|
||||
* If the Test Case class has a 'fixtures' member, installing the
|
||||
named fixtures.
|
||||
* If the class has an 'available_apps' attribute, restricting the app
|
||||
cache to these applications, then firing post_syncdb -- it must run
|
||||
with the correct set of applications for the test case.
|
||||
* If the class has a 'fixtures' attribute, installing these fixtures.
|
||||
"""
|
||||
super(TransactionTestCase, self)._pre_setup()
|
||||
if self.available_apps is not None:
|
||||
cache.set_available_apps(self.available_apps)
|
||||
for db_name in self._databases_names(include_mirrors=False):
|
||||
flush.Command.emit_post_syncdb(
|
||||
verbosity=0, interactive=False, database=db_name)
|
||||
try:
|
||||
self._fixture_setup()
|
||||
except Exception:
|
||||
@@ -782,9 +791,9 @@ class TransactionTestCase(SimpleTestCase):
|
||||
def _post_teardown(self):
|
||||
"""Performs any post-test things. This includes:
|
||||
|
||||
* Putting back the original ROOT_URLCONF if it was changed.
|
||||
* Force closing the connection, so that the next test gets
|
||||
a clean cursor.
|
||||
* Flushing the contents of the database, to leave a clean slate. If
|
||||
the class has an 'available_apps' attribute, post_syncdb isn't fired.
|
||||
* Force-closing the connection, so the next test gets a clean cursor.
|
||||
"""
|
||||
try:
|
||||
self._fixture_teardown()
|
||||
@@ -801,12 +810,14 @@ class TransactionTestCase(SimpleTestCase):
|
||||
cache.unset_available_apps()
|
||||
|
||||
def _fixture_teardown(self):
|
||||
# Allow TRUNCATE ... CASCADE when flushing only a subset of the apps
|
||||
allow_cascade = self.available_apps is not None
|
||||
# Allow TRUNCATE ... CASCADE and don't emit the post_syncdb signal
|
||||
# when flushing only a subset of the apps
|
||||
for db_name in self._databases_names(include_mirrors=False):
|
||||
call_command('flush', verbosity=0, interactive=False,
|
||||
database=db_name, skip_validation=True,
|
||||
reset_sequences=False, allow_cascade=allow_cascade)
|
||||
reset_sequences=False,
|
||||
allow_cascade=self.available_apps is not None,
|
||||
inhibit_post_syncdb=self.available_apps is not None)
|
||||
|
||||
def assertQuerysetEqual(self, qs, values, transform=repr, ordered=True):
|
||||
items = six.moves.map(transform, qs)
|
||||
|
||||
Reference in New Issue
Block a user