Kaydet (Commit) 9136ceb6 authored tarafından wrwrwr's avatar wrwrwr Kaydeden (comit) Tim Graham

Replaced router.routers usage with override_settings(DATABASE_ROUTERS); refs #23933.

üst e6f19ec3
...@@ -8,9 +8,9 @@ import unittest ...@@ -8,9 +8,9 @@ import unittest
from unittest import skipUnless from unittest import skipUnless
from django.contrib.gis.gdal import HAS_GDAL from django.contrib.gis.gdal import HAS_GDAL
from django.db import connection, router from django.db import connection
from django.conf import settings from django.conf import settings
from django.test import TestCase, skipUnlessDBFeature from django.test import TestCase, override_settings, skipUnlessDBFeature
from django.utils._os import upath from django.utils._os import upath
if HAS_GDAL: if HAS_GDAL:
...@@ -328,15 +328,9 @@ class OtherRouter(object): ...@@ -328,15 +328,9 @@ class OtherRouter(object):
@skipUnless(HAS_GDAL, "LayerMapRouterTest needs GDAL support") @skipUnless(HAS_GDAL, "LayerMapRouterTest needs GDAL support")
@skipUnlessDBFeature("gis_enabled") @skipUnlessDBFeature("gis_enabled")
@override_settings(DATABASE_ROUTERS=[OtherRouter()])
class LayerMapRouterTest(TestCase): class LayerMapRouterTest(TestCase):
def setUp(self):
self.old_routers = router.routers
router.routers = [OtherRouter()]
def tearDown(self):
router.routers = self.old_routers
@unittest.skipUnless(len(settings.DATABASES) > 1, 'multiple databases required') @unittest.skipUnless(len(settings.DATABASES) > 1, 'multiple databases required')
def test_layermapping_default_db(self): def test_layermapping_default_db(self):
lm = LayerMapping(City, city_shp, city_mapping) lm = LayerMapping(City, city_shp, city_mapping)
......
...@@ -19,7 +19,7 @@ from django.core import management ...@@ -19,7 +19,7 @@ from django.core import management
from django.core.cache import (cache, caches, CacheKeyWarning, from django.core.cache import (cache, caches, CacheKeyWarning,
InvalidCacheBackendError, DEFAULT_CACHE_ALIAS) InvalidCacheBackendError, DEFAULT_CACHE_ALIAS)
from django.core.context_processors import csrf from django.core.context_processors import csrf
from django.db import connection, connections, router, transaction from django.db import connection, connections, transaction
from django.core.cache.utils import make_template_fragment_key from django.core.cache.utils import make_template_fragment_key
from django.http import HttpResponse, StreamingHttpResponse from django.http import HttpResponse, StreamingHttpResponse
from django.middleware.cache import (FetchFromCacheMiddleware, from django.middleware.cache import (FetchFromCacheMiddleware,
...@@ -974,29 +974,25 @@ class DBCacheRouter(object): ...@@ -974,29 +974,25 @@ class DBCacheRouter(object):
class CreateCacheTableForDBCacheTests(TestCase): class CreateCacheTableForDBCacheTests(TestCase):
multi_db = True multi_db = True
@override_settings(DATABASE_ROUTERS=[DBCacheRouter()])
def test_createcachetable_observes_database_router(self): def test_createcachetable_observes_database_router(self):
old_routers = router.routers # cache table should not be created on 'default'
try: with self.assertNumQueries(0, using='default'):
router.routers = [DBCacheRouter()] management.call_command('createcachetable',
# cache table should not be created on 'default' database='default',
with self.assertNumQueries(0, using='default'): verbosity=0, interactive=False)
management.call_command('createcachetable', # cache table should be created on 'other'
database='default', # Queries:
verbosity=0, interactive=False) # 1: check table doesn't already exist
# cache table should be created on 'other' # 2: create savepoint (if transactional DDL is supported)
# Queries: # 3: create the table
# 1: check table doesn't already exist # 4: create the index
# 2: create savepoint (if transactional DDL is supported) # 5: release savepoint (if transactional DDL is supported)
# 3: create the table num = 5 if connections['other'].features.can_rollback_ddl else 3
# 4: create the index with self.assertNumQueries(num, using='other'):
# 5: release savepoint (if transactional DDL is supported) management.call_command('createcachetable',
num = 5 if connections['other'].features.can_rollback_ddl else 3 database='other',
with self.assertNumQueries(num, using='other'): verbosity=0, interactive=False)
management.call_command('createcachetable',
database='other',
verbosity=0, interactive=False)
finally:
router.routers = old_routers
class PicklingSideEffect(object): class PicklingSideEffect(object):
......
...@@ -7,8 +7,8 @@ from django.apps import apps ...@@ -7,8 +7,8 @@ from django.apps import apps
from django.core.management.color import no_style from django.core.management.color import no_style
from django.core.management.sql import (sql_create, sql_delete, sql_indexes, from django.core.management.sql import (sql_create, sql_delete, sql_indexes,
sql_destroy_indexes, sql_all) sql_destroy_indexes, sql_all)
from django.db import connections, DEFAULT_DB_ALIAS, router from django.db import connections, DEFAULT_DB_ALIAS
from django.test import TestCase from django.test import TestCase, override_settings
from django.utils import six from django.utils import six
# See also initial_sql_regress for 'custom_sql_for_model' tests # See also initial_sql_regress for 'custom_sql_for_model' tests
...@@ -91,13 +91,8 @@ class TestRouter(object): ...@@ -91,13 +91,8 @@ class TestRouter(object):
return False return False
@override_settings(DATABASE_ROUTERS=[TestRouter()])
class SQLCommandsRouterTestCase(TestCase): class SQLCommandsRouterTestCase(TestCase):
def setUp(self):
self._old_routers = router.routers
router.routers = [TestRouter()]
def tearDown(self):
router.routers = self._old_routers
def test_router_honored(self): def test_router_honored(self):
app_config = apps.get_app_config('commands_sql') app_config = apps.get_app_config('commands_sql')
......
...@@ -8,9 +8,9 @@ from django.contrib.contenttypes.fields import ( ...@@ -8,9 +8,9 @@ from django.contrib.contenttypes.fields import (
from django.contrib.contenttypes import management from django.contrib.contenttypes import management
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import checks from django.core import checks
from django.db import connections, models, router from django.db import connections, models
from django.test import TestCase from django.test import TestCase, override_settings
from django.test.utils import captured_stdout, override_settings from django.test.utils import captured_stdout
from django.utils.encoding import force_str from django.utils.encoding import force_str
from .models import Author, Article, SchemeIncludedURL from .models import Author, Article, SchemeIncludedURL
...@@ -397,12 +397,10 @@ class TestRouter(object): ...@@ -397,12 +397,10 @@ class TestRouter(object):
return 'default' return 'default'
@override_settings(DATABASE_ROUTERS=[TestRouter()])
class ContentTypesMultidbTestCase(TestCase): class ContentTypesMultidbTestCase(TestCase):
def setUp(self): def setUp(self):
self.old_routers = router.routers
router.routers = [TestRouter()]
# Whenever a test starts executing, only the "default" database is # Whenever a test starts executing, only the "default" database is
# connected. We explicitly connect to the "other" database here. If we # connected. We explicitly connect to the "other" database here. If we
# don't do it, then it will be implicitly connected later when we query # don't do it, then it will be implicitly connected later when we query
...@@ -411,9 +409,6 @@ class ContentTypesMultidbTestCase(TestCase): ...@@ -411,9 +409,6 @@ class ContentTypesMultidbTestCase(TestCase):
# "SET SQL_AUTO_IS_NULL = 0"), which will affect assertNumQueries(). # "SET SQL_AUTO_IS_NULL = 0"), which will affect assertNumQueries().
connections['other'].ensure_connection() connections['other'].ensure_connection()
def tearDown(self):
router.routers = self.old_routers
def test_multidb(self): def test_multidb(self):
""" """
Test that, when using multiple databases, we use the db_for_read (see Test that, when using multiple databases, we use the db_for_read (see
......
...@@ -8,13 +8,13 @@ except ImportError: ...@@ -8,13 +8,13 @@ except ImportError:
sqlparse = None sqlparse = None
from django import test from django import test
from django.test import override_settings from django.db import connection, migrations, models
from django.db import connection, migrations, models, router
from django.db.migrations.migration import Migration from django.db.migrations.migration import Migration
from django.db.migrations.state import ProjectState from django.db.migrations.state import ProjectState
from django.db.models.fields import NOT_PROVIDED from django.db.models.fields import NOT_PROVIDED
from django.db.transaction import atomic from django.db.transaction import atomic
from django.db.utils import IntegrityError, DatabaseError from django.db.utils import IntegrityError, DatabaseError
from django.test import override_settings
from django.utils import six from django.utils import six
from .test_base import MigrationTestBase from .test_base import MigrationTestBase
...@@ -1566,18 +1566,10 @@ class MigrateNothingRouter(object): ...@@ -1566,18 +1566,10 @@ class MigrateNothingRouter(object):
return False return False
@override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()])
class MultiDBOperationTests(MigrationTestBase): class MultiDBOperationTests(MigrationTestBase):
multi_db = True multi_db = True
def setUp(self):
# Make the 'other' database appear to be a replica of the 'default'
self.old_routers = router.routers
router.routers = [MigrateNothingRouter()]
def tearDown(self):
# Restore the 'other' database as an independent database
router.routers = self.old_routers
def test_create_model(self): def test_create_model(self):
""" """
Tests that CreateModel honours multi-db settings. Tests that CreateModel honours multi-db settings.
......
...@@ -6,9 +6,10 @@ import unittest ...@@ -6,9 +6,10 @@ import unittest
from django.conf import settings from django.conf import settings
from django.db import transaction, connection, router from django.db import transaction, connection, router
from django.db.utils import ConnectionHandler, DEFAULT_DB_ALIAS, DatabaseError from django.db.utils import ConnectionHandler, DEFAULT_DB_ALIAS, DatabaseError
from django.test import (TransactionTestCase, skipIfDBFeature, from django.test import (
skipUnlessDBFeature) TransactionTestCase, override_settings, skipIfDBFeature,
from django.test import override_settings skipUnlessDBFeature,
)
from multiple_database.routers import TestRouter from multiple_database.routers import TestRouter
...@@ -257,14 +258,10 @@ class SelectForUpdateTests(TransactionTestCase): ...@@ -257,14 +258,10 @@ class SelectForUpdateTests(TransactionTestCase):
self.assertIsInstance(status[-1], DatabaseError) self.assertIsInstance(status[-1], DatabaseError)
@skipUnlessDBFeature('has_select_for_update') @skipUnlessDBFeature('has_select_for_update')
@override_settings(DATABASE_ROUTERS=[TestRouter()])
def test_select_for_update_on_multidb(self): def test_select_for_update_on_multidb(self):
old_routers = router.routers query = Person.objects.select_for_update()
try: self.assertEqual(router.db_for_write(Person), query.db)
router.routers = [TestRouter()]
query = Person.objects.select_for_update()
self.assertEqual(router.db_for_write(Person), query.db)
finally:
router.routers = old_routers
@skipUnlessDBFeature('has_select_for_update') @skipUnlessDBFeature('has_select_for_update')
def test_select_for_update_with_get(self): def test_select_for_update_with_get(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment