mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Moved ensure_defaults() and prepare_test_settings() logic to ConnectionHandler.configure_settings().
This commit is contained in:
parent
58ad9a99a7
commit
13378ad952
@ -154,57 +154,38 @@ class ConnectionHandler(BaseConnectionHandler):
|
|||||||
)
|
)
|
||||||
elif databases[DEFAULT_DB_ALIAS] == {}:
|
elif databases[DEFAULT_DB_ALIAS] == {}:
|
||||||
databases[DEFAULT_DB_ALIAS]["ENGINE"] = "django.db.backends.dummy"
|
databases[DEFAULT_DB_ALIAS]["ENGINE"] = "django.db.backends.dummy"
|
||||||
|
|
||||||
|
# Configure default settings.
|
||||||
|
for conn in databases.values():
|
||||||
|
conn.setdefault("ATOMIC_REQUESTS", False)
|
||||||
|
conn.setdefault("AUTOCOMMIT", True)
|
||||||
|
conn.setdefault("ENGINE", "django.db.backends.dummy")
|
||||||
|
if conn["ENGINE"] == "django.db.backends." or not conn["ENGINE"]:
|
||||||
|
conn["ENGINE"] = "django.db.backends.dummy"
|
||||||
|
conn.setdefault("CONN_MAX_AGE", 0)
|
||||||
|
conn.setdefault("CONN_HEALTH_CHECKS", False)
|
||||||
|
conn.setdefault("OPTIONS", {})
|
||||||
|
conn.setdefault("TIME_ZONE", None)
|
||||||
|
for setting in ["NAME", "USER", "PASSWORD", "HOST", "PORT"]:
|
||||||
|
conn.setdefault(setting, "")
|
||||||
|
|
||||||
|
test_settings = conn.setdefault("TEST", {})
|
||||||
|
default_test_settings = [
|
||||||
|
("CHARSET", None),
|
||||||
|
("COLLATION", None),
|
||||||
|
("MIGRATE", True),
|
||||||
|
("MIRROR", None),
|
||||||
|
("NAME", None),
|
||||||
|
]
|
||||||
|
for key, value in default_test_settings:
|
||||||
|
test_settings.setdefault(key, value)
|
||||||
return databases
|
return databases
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def databases(self):
|
def databases(self):
|
||||||
return self.settings
|
return self.settings
|
||||||
|
|
||||||
def ensure_defaults(self, alias):
|
|
||||||
"""
|
|
||||||
Put the defaults into the settings dictionary for a given connection
|
|
||||||
where no settings is provided.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
conn = self.settings[alias]
|
|
||||||
except KeyError:
|
|
||||||
raise self.exception_class(f"The connection '{alias}' doesn't exist.")
|
|
||||||
|
|
||||||
conn.setdefault("ATOMIC_REQUESTS", False)
|
|
||||||
conn.setdefault("AUTOCOMMIT", True)
|
|
||||||
conn.setdefault("ENGINE", "django.db.backends.dummy")
|
|
||||||
if conn["ENGINE"] == "django.db.backends." or not conn["ENGINE"]:
|
|
||||||
conn["ENGINE"] = "django.db.backends.dummy"
|
|
||||||
conn.setdefault("CONN_MAX_AGE", 0)
|
|
||||||
conn.setdefault("CONN_HEALTH_CHECKS", False)
|
|
||||||
conn.setdefault("OPTIONS", {})
|
|
||||||
conn.setdefault("TIME_ZONE", None)
|
|
||||||
for setting in ["NAME", "USER", "PASSWORD", "HOST", "PORT"]:
|
|
||||||
conn.setdefault(setting, "")
|
|
||||||
|
|
||||||
def prepare_test_settings(self, alias):
|
|
||||||
"""
|
|
||||||
Make sure the test settings are available in the 'TEST' sub-dictionary.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
conn = self.settings[alias]
|
|
||||||
except KeyError:
|
|
||||||
raise self.exception_class(f"The connection '{alias}' doesn't exist.")
|
|
||||||
|
|
||||||
test_settings = conn.setdefault("TEST", {})
|
|
||||||
default_test_settings = [
|
|
||||||
("CHARSET", None),
|
|
||||||
("COLLATION", None),
|
|
||||||
("MIGRATE", True),
|
|
||||||
("MIRROR", None),
|
|
||||||
("NAME", None),
|
|
||||||
]
|
|
||||||
for key, value in default_test_settings:
|
|
||||||
test_settings.setdefault(key, value)
|
|
||||||
|
|
||||||
def create_connection(self, alias):
|
def create_connection(self, alias):
|
||||||
self.ensure_defaults(alias)
|
|
||||||
self.prepare_test_settings(alias)
|
|
||||||
db = self.settings[alias]
|
db = self.settings[alias]
|
||||||
backend = load_backend(db["ENGINE"])
|
backend = load_backend(db["ENGINE"])
|
||||||
return backend.DatabaseWrapper(db, alias)
|
return backend.DatabaseWrapper(db, alias)
|
||||||
|
@ -50,26 +50,6 @@ class ConnectionHandlerTests(SimpleTestCase):
|
|||||||
with self.assertRaisesMessage(ConnectionDoesNotExist, msg):
|
with self.assertRaisesMessage(ConnectionDoesNotExist, msg):
|
||||||
conns["nonexistent"]
|
conns["nonexistent"]
|
||||||
|
|
||||||
def test_ensure_defaults_nonexistent_alias(self):
|
|
||||||
msg = "The connection 'nonexistent' doesn't exist."
|
|
||||||
conns = ConnectionHandler(
|
|
||||||
{
|
|
||||||
DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
with self.assertRaisesMessage(ConnectionDoesNotExist, msg):
|
|
||||||
conns.ensure_defaults("nonexistent")
|
|
||||||
|
|
||||||
def test_prepare_test_settings_nonexistent_alias(self):
|
|
||||||
msg = "The connection 'nonexistent' doesn't exist."
|
|
||||||
conns = ConnectionHandler(
|
|
||||||
{
|
|
||||||
DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
with self.assertRaisesMessage(ConnectionDoesNotExist, msg):
|
|
||||||
conns.prepare_test_settings("nonexistent")
|
|
||||||
|
|
||||||
|
|
||||||
class DatabaseErrorWrapperTests(TestCase):
|
class DatabaseErrorWrapperTests(TestCase):
|
||||||
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test")
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test")
|
||||||
|
Loading…
Reference in New Issue
Block a user