Mocked db.connections in test_runner tests
This commit is contained in:
parent
b99114f67f
commit
67c063e6d5
|
@ -11,7 +11,6 @@ from django import db
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.db.backends.dummy.base import DatabaseCreation
|
|
||||||
from django.test import (
|
from django.test import (
|
||||||
TestCase, TransactionTestCase, mock, skipUnlessDBFeature, testcases,
|
TestCase, TransactionTestCase, mock, skipUnlessDBFeature, testcases,
|
||||||
)
|
)
|
||||||
|
@ -211,29 +210,28 @@ class Sqlite3InMemoryTestDbs(TestCase):
|
||||||
"This is an sqlite-specific issue")
|
"This is an sqlite-specific issue")
|
||||||
def test_transaction_support(self):
|
def test_transaction_support(self):
|
||||||
"""Ticket #16329: sqlite3 in-memory test databases"""
|
"""Ticket #16329: sqlite3 in-memory test databases"""
|
||||||
old_db_connections = db.connections
|
|
||||||
for option_key, option_value in (
|
for option_key, option_value in (
|
||||||
('NAME', ':memory:'), ('TEST', {'NAME': ':memory:'})):
|
('NAME', ':memory:'), ('TEST', {'NAME': ':memory:'})):
|
||||||
try:
|
tested_connections = db.ConnectionHandler({
|
||||||
db.connections = db.ConnectionHandler({
|
'default': {
|
||||||
'default': {
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
option_key: option_value,
|
||||||
option_key: option_value,
|
},
|
||||||
},
|
'other': {
|
||||||
'other': {
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
option_key: option_value,
|
||||||
option_key: option_value,
|
},
|
||||||
},
|
})
|
||||||
})
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
other = db.connections['other']
|
with mock.patch('django.test.testcases.connections', new=tested_connections):
|
||||||
DiscoverRunner(verbosity=0).setup_databases()
|
other = tested_connections['other']
|
||||||
msg = "DATABASES setting '%s' option set to sqlite3's ':memory:' value shouldn't interfere with transaction support detection." % option_key
|
DiscoverRunner(verbosity=0).setup_databases()
|
||||||
# Transaction support should be properly initialized for the 'other' DB
|
msg = ("DATABASES setting '%s' option set to sqlite3's ':memory:' value "
|
||||||
self.assertTrue(other.features.supports_transactions, msg)
|
"shouldn't interfere with transaction support detection." % option_key)
|
||||||
# And all the DBs should report that they support transactions
|
# Transaction support should be properly initialized for the 'other' DB
|
||||||
self.assertTrue(connections_support_transactions(), msg)
|
self.assertTrue(other.features.supports_transactions, msg)
|
||||||
finally:
|
# And all the DBs should report that they support transactions
|
||||||
db.connections = old_db_connections
|
self.assertTrue(connections_support_transactions(), msg)
|
||||||
|
|
||||||
|
|
||||||
class DummyBackendTest(unittest.TestCase):
|
class DummyBackendTest(unittest.TestCase):
|
||||||
|
@ -241,17 +239,15 @@ class DummyBackendTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Test that setup_databases() doesn't fail with dummy database backend.
|
Test that setup_databases() doesn't fail with dummy database backend.
|
||||||
"""
|
"""
|
||||||
runner_instance = DiscoverRunner(verbosity=0)
|
tested_connections = db.ConnectionHandler({})
|
||||||
old_db_connections = db.connections
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
try:
|
runner_instance = DiscoverRunner(verbosity=0)
|
||||||
db.connections = db.ConnectionHandler({})
|
try:
|
||||||
old_config = runner_instance.setup_databases()
|
old_config = runner_instance.setup_databases()
|
||||||
runner_instance.teardown_databases(old_config)
|
runner_instance.teardown_databases(old_config)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.fail("setup_databases/teardown_databases unexpectedly raised "
|
self.fail("setup_databases/teardown_databases unexpectedly raised "
|
||||||
"an error: %s" % e)
|
"an error: %s" % e)
|
||||||
finally:
|
|
||||||
db.connections = old_db_connections
|
|
||||||
|
|
||||||
|
|
||||||
class AliasedDefaultTestSetupTest(unittest.TestCase):
|
class AliasedDefaultTestSetupTest(unittest.TestCase):
|
||||||
|
@ -259,51 +255,31 @@ class AliasedDefaultTestSetupTest(unittest.TestCase):
|
||||||
"""
|
"""
|
||||||
Test that setup_datebases() doesn't fail when 'default' is aliased
|
Test that setup_datebases() doesn't fail when 'default' is aliased
|
||||||
"""
|
"""
|
||||||
runner_instance = DiscoverRunner(verbosity=0)
|
tested_connections = db.ConnectionHandler({
|
||||||
old_db_connections = db.connections
|
'default': {
|
||||||
try:
|
'NAME': 'dummy'
|
||||||
db.connections = db.ConnectionHandler({
|
},
|
||||||
'default': {
|
'aliased': {
|
||||||
'NAME': 'dummy'
|
'NAME': 'dummy'
|
||||||
},
|
}
|
||||||
'aliased': {
|
})
|
||||||
'NAME': 'dummy'
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
}
|
runner_instance = DiscoverRunner(verbosity=0)
|
||||||
})
|
try:
|
||||||
old_config = runner_instance.setup_databases()
|
old_config = runner_instance.setup_databases()
|
||||||
runner_instance.teardown_databases(old_config)
|
runner_instance.teardown_databases(old_config)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.fail("setup_databases/teardown_databases unexpectedly raised "
|
self.fail("setup_databases/teardown_databases unexpectedly raised "
|
||||||
"an error: %s" % e)
|
"an error: %s" % e)
|
||||||
finally:
|
|
||||||
db.connections = old_db_connections
|
|
||||||
|
|
||||||
|
|
||||||
class SetupDatabasesTests(unittest.TestCase):
|
class SetupDatabasesTests(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self._old_db_connections = db.connections
|
|
||||||
self._old_destroy_test_db = DatabaseCreation.destroy_test_db
|
|
||||||
self._old_create_test_db = DatabaseCreation.create_test_db
|
|
||||||
self.runner_instance = DiscoverRunner(verbosity=0)
|
self.runner_instance = DiscoverRunner(verbosity=0)
|
||||||
|
|
||||||
def tearDown(self):
|
|
||||||
DatabaseCreation.create_test_db = self._old_create_test_db
|
|
||||||
DatabaseCreation.destroy_test_db = self._old_destroy_test_db
|
|
||||||
db.connections = self._old_db_connections
|
|
||||||
|
|
||||||
def test_setup_aliased_databases(self):
|
def test_setup_aliased_databases(self):
|
||||||
destroyed_names = []
|
tested_connections = db.ConnectionHandler({
|
||||||
DatabaseCreation.destroy_test_db = (
|
|
||||||
lambda self, old_database_name, verbosity=1, keepdb=False, serialize=True:
|
|
||||||
destroyed_names.append(old_database_name)
|
|
||||||
)
|
|
||||||
DatabaseCreation.create_test_db = (
|
|
||||||
lambda self, verbosity=1, autoclobber=False, keepdb=False, serialize=True:
|
|
||||||
self._get_test_db_name()
|
|
||||||
)
|
|
||||||
|
|
||||||
db.connections = db.ConnectionHandler({
|
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.dummy',
|
'ENGINE': 'django.db.backends.dummy',
|
||||||
'NAME': 'dbname',
|
'NAME': 'dbname',
|
||||||
|
@ -314,13 +290,14 @@ class SetupDatabasesTests(unittest.TestCase):
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
old_config = self.runner_instance.setup_databases()
|
with mock.patch('django.db.backends.dummy.base.DatabaseCreation') as mocked_db_creation:
|
||||||
self.runner_instance.teardown_databases(old_config)
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
|
old_config = self.runner_instance.setup_databases()
|
||||||
self.assertEqual(destroyed_names.count('dbname'), 1)
|
self.runner_instance.teardown_databases(old_config)
|
||||||
|
mocked_db_creation.return_value.destroy_test_db.assert_called_once_with('dbname', 0, False)
|
||||||
|
|
||||||
def test_destroy_test_db_restores_db_name(self):
|
def test_destroy_test_db_restores_db_name(self):
|
||||||
db.connections = db.ConnectionHandler({
|
tested_connections = db.ConnectionHandler({
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"],
|
'ENGINE': settings.DATABASES[db.DEFAULT_DB_ALIAS]["ENGINE"],
|
||||||
'NAME': 'xxx_test_database',
|
'NAME': 'xxx_test_database',
|
||||||
|
@ -328,35 +305,36 @@ class SetupDatabasesTests(unittest.TestCase):
|
||||||
})
|
})
|
||||||
# Using the real current name as old_name to not mess with the test suite.
|
# Using the real current name as old_name to not mess with the test suite.
|
||||||
old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"]
|
old_name = settings.DATABASES[db.DEFAULT_DB_ALIAS]["NAME"]
|
||||||
db.connections['default'].creation.destroy_test_db(old_name, verbosity=0, keepdb=True)
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
self.assertEqual(db.connections['default'].settings_dict["NAME"], old_name)
|
tested_connections['default'].creation.destroy_test_db(old_name, verbosity=0, keepdb=True)
|
||||||
|
self.assertEqual(tested_connections['default'].settings_dict["NAME"], old_name)
|
||||||
|
|
||||||
def test_serialization(self):
|
def test_serialization(self):
|
||||||
serialize = []
|
tested_connections = db.ConnectionHandler({
|
||||||
DatabaseCreation.create_test_db = (
|
|
||||||
lambda *args, **kwargs: serialize.append(kwargs.get('serialize'))
|
|
||||||
)
|
|
||||||
db.connections = db.ConnectionHandler({
|
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.dummy',
|
'ENGINE': 'django.db.backends.dummy',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
self.runner_instance.setup_databases()
|
with mock.patch('django.db.backends.dummy.base.DatabaseCreation') as mocked_db_creation:
|
||||||
self.assertEqual(serialize, [True])
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
|
self.runner_instance.setup_databases()
|
||||||
|
mocked_db_creation.return_value.create_test_db.assert_called_once_with(
|
||||||
|
0, autoclobber=False, serialize=True, keepdb=False
|
||||||
|
)
|
||||||
|
|
||||||
def test_serialized_off(self):
|
def test_serialized_off(self):
|
||||||
serialize = []
|
tested_connections = db.ConnectionHandler({
|
||||||
DatabaseCreation.create_test_db = (
|
|
||||||
lambda *args, **kwargs: serialize.append(kwargs.get('serialize'))
|
|
||||||
)
|
|
||||||
db.connections = db.ConnectionHandler({
|
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.dummy',
|
'ENGINE': 'django.db.backends.dummy',
|
||||||
'TEST': {'SERIALIZE': False},
|
'TEST': {'SERIALIZE': False},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
self.runner_instance.setup_databases()
|
with mock.patch('django.db.backends.dummy.base.DatabaseCreation') as mocked_db_creation:
|
||||||
self.assertEqual(serialize, [False])
|
with mock.patch('django.db.connections', new=tested_connections):
|
||||||
|
self.runner_instance.setup_databases()
|
||||||
|
mocked_db_creation.return_value.create_test_db.assert_called_once_with(
|
||||||
|
0, autoclobber=False, serialize=False, keepdb=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DeprecationDisplayTest(AdminScriptTestCase):
|
class DeprecationDisplayTest(AdminScriptTestCase):
|
||||||
|
|
Loading…
Reference in New Issue