From c6e6d4eeb776c473567362405cdbc6a0328eb194 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 4 Jun 2013 08:09:29 +0200 Subject: [PATCH] Defined available_apps in relevant tests. Fixed #20483. --- django/contrib/admin/tests.py | 9 +++++++++ django/contrib/auth/tests/test_handlers.py | 8 ++++++++ tests/admin_scripts/tests.py | 7 +++++++ tests/backends/tests.py | 7 +++++++ tests/basic/tests.py | 3 +++ tests/cache/tests.py | 2 ++ tests/delete_regress/tests.py | 6 ++++++ tests/file_storage/tests.py | 2 ++ tests/fixtures/tests.py | 7 +++++++ tests/fixtures_model_package/tests.py | 3 +++ tests/fixtures_regress/tests.py | 6 ++++++ tests/get_or_create/tests.py | 2 ++ tests/handlers/tests.py | 2 ++ tests/m2m_through_regress/tests.py | 1 + tests/middleware/tests.py | 3 +++ tests/proxy_model_inheritance/tests.py | 8 +++++++- tests/requests/tests.py | 2 ++ tests/runtests.py | 9 +++++++++ tests/select_for_update/tests.py | 2 ++ tests/serializers/tests.py | 3 +++ tests/servers/tests.py | 9 ++++++++- tests/settings_tests/tests.py | 2 ++ tests/test_runner/tests.py | 2 ++ tests/transactions/tests.py | 16 ++++++++++++++++ tests/transactions_regress/tests.py | 21 +++++++++++++++++++++ tests/view_tests/tests/test_i18n.py | 2 ++ 26 files changed, 142 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/tests.py b/django/contrib/admin/tests.py index badf45b580..25ea230b28 100644 --- a/django/contrib/admin/tests.py +++ b/django/contrib/admin/tests.py @@ -5,7 +5,16 @@ from django.utils.module_loading import import_by_path from django.utils.unittest import SkipTest from django.utils.translation import ugettext as _ + class AdminSeleniumWebDriverTestCase(LiveServerTestCase): + + available_apps = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.sites', + ] webdriver_class = 'selenium.webdriver.firefox.webdriver.WebDriver' @classmethod diff --git a/django/contrib/auth/tests/test_handlers.py b/django/contrib/auth/tests/test_handlers.py index e0d2fa2200..b3737172a3 100644 --- a/django/contrib/auth/tests/test_handlers.py +++ b/django/contrib/auth/tests/test_handlers.py @@ -8,10 +8,18 @@ from django.test import TransactionTestCase from django.test.utils import override_settings +# This must be a TransactionTestCase because the WSGI auth handler performs +# its own transaction management. class ModWsgiHandlerTestCase(TransactionTestCase): """ Tests for the mod_wsgi authentication handler """ + + available_apps = [ + 'django.contrib.auth', + 'django.contrib.contenttypes', + ] + @skipIfCustomUser def test_check_password(self): """ diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index df8a8becf6..2f399acb23 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -1474,6 +1474,13 @@ class ArgumentOrder(AdminScriptTestCase): class StartProject(LiveServerTestCase, AdminScriptTestCase): + available_apps = [ + 'admin_scripts', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + ] + def test_wrong_args(self): "Make sure passing the wrong kinds of arguments raises a CommandError" out, err = self.run_django_admin(['startproject']) diff --git a/tests/backends/tests.py b/tests/backends/tests.py index 08cae0f7c3..b6e3e9e36f 100644 --- a/tests/backends/tests.py +++ b/tests/backends/tests.py @@ -345,6 +345,8 @@ class PostgresNewConnectionTest(TestCase): # connection would implicitly rollback and cause problems during teardown. class ConnectionCreatedSignalTest(TransactionTestCase): + available_apps = [] + # Unfortunately with sqlite3 the in-memory test database cannot be closed, # and so it cannot be re-opened during testing. @skipUnlessDBFeature('test_db_allows_multiple_connections') @@ -514,6 +516,8 @@ class BackendTestCase(TestCase): # verify if its type is django.database.db.IntegrityError. class FkConstraintsTests(TransactionTestCase): + available_apps = ['backends'] + def setUp(self): # Create a Reporter. self.r = models.Reporter.objects.create(first_name='John', last_name='Smith') @@ -777,6 +781,9 @@ class MySQLPKZeroTests(TestCase): class DBConstraintTestCase(TransactionTestCase): + + available_apps = ['backends'] + def test_can_reference_existant(self): obj = models.Object.objects.create() ref = models.ObjectReference.objects.create(obj=obj) diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 6bf46cce9b..4b79c47be5 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -686,6 +686,9 @@ class ModelTest(TestCase): class ConcurrentSaveTests(TransactionTestCase): + + available_apps = ['basic'] + @skipUnlessDBFeature('test_db_allows_multiple_connections') def test_concurrent_delete_with_save(self): """ diff --git a/tests/cache/tests.py b/tests/cache/tests.py index da80c48058..75b7851400 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -827,6 +827,8 @@ def custom_key_func(key, key_prefix, version): class DBCacheTests(BaseCacheTests, TransactionTestCase): + + available_apps = [] backend_name = 'django.core.cache.backends.db.DatabaseCache' def setUp(self): diff --git a/tests/delete_regress/tests.py b/tests/delete_regress/tests.py index e88c95e229..a4908b2121 100644 --- a/tests/delete_regress/tests.py +++ b/tests/delete_regress/tests.py @@ -16,6 +16,9 @@ from .models import (Book, Award, AwardNote, Person, Child, Toy, PlayedWith, # Can't run this test under SQLite, because you can't # get two connections to an in-memory database. class DeleteLockingTest(TransactionTestCase): + + available_apps = ['delete_regress'] + def setUp(self): # Create a second connection to the default database new_connections = ConnectionHandler(settings.DATABASES) @@ -107,6 +110,9 @@ class DeleteCascadeTests(TestCase): class DeleteCascadeTransactionTests(TransactionTestCase): + + available_apps = ['delete_regress'] + def test_inheritance(self): """ Auto-created many-to-many through tables referencing a parent model are diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index e3f390ff61..6c3c559660 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -621,6 +621,8 @@ class FileLikeObjectTestCase(LiveServerTestCase): """ Test file-like objects (#15644). """ + + available_apps = [] urls = 'file_storage.urls' def setUp(self): diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index 1b4d6eeeef..d0942afdb7 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -354,6 +354,13 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): class FixtureTransactionTests(DumpDataAssertMixin, TransactionTestCase): + available_apps = [ + 'fixtures', + 'django.contrib.contenttypes', + 'django.contrib.auth', + 'django.contrib.sites', + ] + @skipUnlessDBFeature('supports_forward_references') def test_format_discovery(self): # Load fixture 1 again, using format discovery diff --git a/tests/fixtures_model_package/tests.py b/tests/fixtures_model_package/tests.py index e0a35e300d..dbcc721d8f 100644 --- a/tests/fixtures_model_package/tests.py +++ b/tests/fixtures_model_package/tests.py @@ -26,6 +26,9 @@ class SampleTestCase(TestCase): class TestNoInitialDataLoading(TransactionTestCase): + + available_apps = ['fixtures_model_package'] + def test_syncdb(self): transaction.set_autocommit(False) try: diff --git a/tests/fixtures_regress/tests.py b/tests/fixtures_regress/tests.py index 52526ec338..0f6ac65976 100644 --- a/tests/fixtures_regress/tests.py +++ b/tests/fixtures_regress/tests.py @@ -685,6 +685,12 @@ class NaturalKeyFixtureTests(TestCase): class TestTicket11101(TransactionTestCase): + available_apps = [ + 'fixtures_regress', + 'django.contrib.auth', + 'django.contrib.contenttypes', + ] + def ticket_11101(self): management.call_command( 'loaddata', diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py index 36c248b169..847a6dec01 100644 --- a/tests/get_or_create/tests.py +++ b/tests/get_or_create/tests.py @@ -94,6 +94,8 @@ class GetOrCreateTests(TestCase): class GetOrCreateTransactionTests(TransactionTestCase): + available_apps = ['get_or_create'] + def test_get_or_create_integrityerror(self): # Regression test for #15117. Requires a TransactionTestCase on # databases that delay integrity checks until the end of transactions, diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index 397b63647a..2f9f304b81 100644 --- a/tests/handlers/tests.py +++ b/tests/handlers/tests.py @@ -37,6 +37,8 @@ class HandlerTests(TestCase): class TransactionsPerRequestTests(TransactionTestCase): + + available_apps = [] urls = 'handlers.urls' def test_no_transaction(self): diff --git a/tests/m2m_through_regress/tests.py b/tests/m2m_through_regress/tests.py index 5ac10462fa..de4d52a2db 100644 --- a/tests/m2m_through_regress/tests.py +++ b/tests/m2m_through_regress/tests.py @@ -10,6 +10,7 @@ from .models import (Person, Group, Membership, UserMembership, Car, Driver, class M2MThroughTestCase(TestCase): + def test_everything(self): bob = Person.objects.create(name="Bob") jim = Person.objects.create(name="Jim") diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index 265eb97c36..20645cf91f 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -707,6 +707,9 @@ class TransactionMiddlewareTest(IgnorePendingDeprecationWarningsMixin, Transacti """ Test the transaction middleware. """ + + available_apps = ['middleware'] + def setUp(self): super(TransactionMiddlewareTest, self).setUp() self.request = HttpRequest() diff --git a/tests/proxy_model_inheritance/tests.py b/tests/proxy_model_inheritance/tests.py index 239bc67809..85bf74a4e9 100644 --- a/tests/proxy_model_inheritance/tests.py +++ b/tests/proxy_model_inheritance/tests.py @@ -22,6 +22,8 @@ class ProxyModelInheritanceTests(TransactionTestCase): apps and calls syncdb, then verifies that the table has been created. """ + available_apps = [] + def setUp(self): self.old_sys_path = sys.path[:] sys.path.append(os.path.dirname(os.path.abspath(upath(__file__)))) @@ -38,7 +40,11 @@ class ProxyModelInheritanceTests(TransactionTestCase): del cache.app_models['app2'] def test_table_exists(self): - call_command('syncdb', verbosity=0) + try: + cache.set_available_apps(settings.INSTALLED_APPS) + call_command('syncdb', verbosity=0) + finally: + cache.unset_available_apps() from .app1.models import ProxyModel from .app2.models import NiceModel self.assertEqual(NiceModel.objects.all().count(), 0) diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 4d730bb561..d9120db4e7 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -683,6 +683,8 @@ class RequestsTests(SimpleTestCase): "Cannot establish two connections to an in-memory SQLite database.") class DatabaseConnectionHandlingTests(TransactionTestCase): + available_apps = [] + def setUp(self): # Use a temporary connection to avoid messing with the main one. self._old_default_connection = connections['default'] diff --git a/tests/runtests.py b/tests/runtests.py index a18324f676..e8b58ad1d5 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -72,6 +72,15 @@ def get_installed(): def setup(verbosity, test_labels): from django.conf import settings from django.db.models.loading import get_apps, load_app + from django.test.testcases import TransactionTestCase, TestCase + + # Force declaring available_apps in TransactionTestCase for faster tests. + def no_available_apps(self): + raise Exception("Please define available_apps in TransactionTestCase " + "and its subclasses.") + TransactionTestCase.available_apps = property(no_available_apps) + TestCase.available_apps = None + state = { 'INSTALLED_APPS': settings.INSTALLED_APPS, 'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""), diff --git a/tests/select_for_update/tests.py b/tests/select_for_update/tests.py index c2fa22705a..f087ca123a 100644 --- a/tests/select_for_update/tests.py +++ b/tests/select_for_update/tests.py @@ -23,6 +23,8 @@ requires_threading = unittest.skipUnless(threading, 'requires threading') class SelectForUpdateTests(TransactionTestCase): + available_apps = ['select_for_update'] + def setUp(self): transaction.enter_transaction_management() self.person = Person.objects.create(name='Reinhardt') diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py index a96a1af748..038276ca21 100644 --- a/tests/serializers/tests.py +++ b/tests/serializers/tests.py @@ -259,6 +259,9 @@ class SerializersTestBase(object): class SerializersTransactionTestBase(object): + + available_apps = ['serializers'] + def test_forward_refs(self): """ Tests that objects ids can be referenced before they are diff --git a/tests/servers/tests.py b/tests/servers/tests.py index 920149477c..3377793d68 100644 --- a/tests/servers/tests.py +++ b/tests/servers/tests.py @@ -30,8 +30,15 @@ TEST_SETTINGS = { class LiveServerBase(LiveServerTestCase): - urls = 'servers.urls' + + available_apps = [ + 'servers', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + ] fixtures = ['testdata.json'] + urls = 'servers.urls' @classmethod def setUpClass(cls): diff --git a/tests/settings_tests/tests.py b/tests/settings_tests/tests.py index 97e43ff0eb..f8d66bb2b7 100644 --- a/tests/settings_tests/tests.py +++ b/tests/settings_tests/tests.py @@ -12,6 +12,8 @@ from django.utils import unittest, six @override_settings(TEST='override') class FullyDecoratedTranTestCase(TransactionTestCase): + available_apps = [] + def test_override(self): self.assertEqual(settings.TEST, 'override') diff --git a/tests/test_runner/tests.py b/tests/test_runner/tests.py index 95ccf0242a..74a557af26 100644 --- a/tests/test_runner/tests.py +++ b/tests/test_runner/tests.py @@ -318,6 +318,8 @@ class AutoIncrementResetTest(TransactionTestCase): that AutoField values start from 1 for each transactional test case. """ + available_apps = ['test_runner'] + reset_sequences = True @skipUnlessDBFeature('supports_sequence_reset') diff --git a/tests/transactions/tests.py b/tests/transactions/tests.py index 0f16a9c805..24b7615d6f 100644 --- a/tests/transactions/tests.py +++ b/tests/transactions/tests.py @@ -26,6 +26,8 @@ class AtomicTests(TransactionTestCase): syntax and the bulk of the tests use the context manager syntax. """ + available_apps = ['transactions'] + def test_decorator_syntax_commit(self): @transaction.atomic def make_reporter(): @@ -232,6 +234,8 @@ class AtomicInsideLegacyTransactionManagementTests(AtomicTests): class AtomicMergeTests(TransactionTestCase): """Test merging transactions with savepoint=False.""" + available_apps = ['transactions'] + def test_merged_outer_rollback(self): with transaction.atomic(): Reporter.objects.create(first_name="Tintin") @@ -286,6 +290,8 @@ class AtomicMergeTests(TransactionTestCase): "'atomic' requires transactions and savepoints.") class AtomicErrorsTests(TransactionTestCase): + available_apps = [] + def test_atomic_prevents_setting_autocommit(self): autocommit = transaction.get_autocommit() with transaction.atomic(): @@ -311,6 +317,8 @@ class AtomicErrorsTests(TransactionTestCase): class AtomicMiscTests(TransactionTestCase): + available_apps = [] + def test_wrap_callable_instance(self): # Regression test for #20028 class Callable(object): @@ -322,6 +330,8 @@ class AtomicMiscTests(TransactionTestCase): class TransactionTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase): + available_apps = ['transactions'] + def create_a_reporter_then_fail(self, first, last): a = Reporter(first_name=first, last_name=last) a.save() @@ -477,6 +487,9 @@ class TransactionTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCas class TransactionRollbackTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase): + + available_apps = ['transactions'] + def execute_bad_sql(self): cursor = connection.cursor() cursor.execute("INSERT INTO transactions_reporter (first_name, last_name) VALUES ('Douglas', 'Adams');") @@ -494,6 +507,9 @@ class TransactionRollbackTests(IgnorePendingDeprecationWarningsMixin, Transactio transaction.rollback() class TransactionContextManagerTests(IgnorePendingDeprecationWarningsMixin, TransactionTestCase): + + available_apps = ['transactions'] + def create_reporter_and_fail(self): Reporter.objects.create(first_name="Bob", last_name="Holtzman") raise Exception diff --git a/tests/transactions_regress/tests.py b/tests/transactions_regress/tests.py index 5339b4a8ea..8078f1d128 100644 --- a/tests/transactions_regress/tests.py +++ b/tests/transactions_regress/tests.py @@ -10,6 +10,9 @@ from django.utils.unittest import skipIf, skipUnless from .models import Mod, M2mA, M2mB, SubMod class ModelInheritanceTests(TransactionTestCase): + + available_apps = ['transactions_regress'] + def test_save(self): # First, create a SubMod, then try to save another with conflicting # cnt field. The problem was that transactions were committed after @@ -33,6 +36,13 @@ class TestTransactionClosing(IgnorePendingDeprecationWarningsMixin, TransactionT when they should be, and aren't left pending after operations have been performed in them. Refs #9964. """ + + available_apps = [ + 'transactions_regress', + 'django.contrib.auth', + 'django.contrib.contenttypes', + ] + def test_raw_committed_on_success(self): """ Make sure a transaction consisting of raw SQL execution gets @@ -188,6 +198,9 @@ class TestNewConnection(IgnorePendingDeprecationWarningsMixin, TransactionTestCa """ Check that new connections don't have special behaviour. """ + + available_apps = ['transactions_regress'] + def setUp(self): self._old_backend = connections[DEFAULT_DB_ALIAS] settings = self._old_backend.settings_dict.copy() @@ -235,6 +248,9 @@ class TestPostgresAutocommitAndIsolation(IgnorePendingDeprecationWarningsMixin, is restored after entering and leaving transaction management. Refs #16047, #18130. """ + + available_apps = ['transactions_regress'] + def setUp(self): from psycopg2.extensions import (ISOLATION_LEVEL_AUTOCOMMIT, ISOLATION_LEVEL_SERIALIZABLE, @@ -311,6 +327,9 @@ class TestPostgresAutocommitAndIsolation(IgnorePendingDeprecationWarningsMixin, class TestManyToManyAddTransaction(IgnorePendingDeprecationWarningsMixin, TransactionTestCase): + + available_apps = ['transactions_regress'] + def test_manyrelated_add_commit(self): "Test for https://code.djangoproject.com/ticket/16818" a = M2mA.objects.create() @@ -327,6 +346,8 @@ class TestManyToManyAddTransaction(IgnorePendingDeprecationWarningsMixin, Transa class SavepointTest(IgnorePendingDeprecationWarningsMixin, TransactionTestCase): + available_apps = ['transactions_regress'] + @skipIf(connection.vendor == 'sqlite', "SQLite doesn't support savepoints in managed mode") @skipUnlessDBFeature('uses_savepoints') diff --git a/tests/view_tests/tests/test_i18n.py b/tests/view_tests/tests/test_i18n.py index 2da17dd0f5..b35b192fd0 100644 --- a/tests/view_tests/tests/test_i18n.py +++ b/tests/view_tests/tests/test_i18n.py @@ -183,6 +183,8 @@ skip_selenium = not os.environ.get('DJANGO_SELENIUM_TESTS', False) @unittest.skipIf(skip_selenium, 'Selenium tests not requested') @unittest.skipUnless(firefox, 'Selenium not installed') class JavascriptI18nTests(LiveServerTestCase): + + available_apps = [] urls = 'view_tests.urls' @classmethod