2017-02-11 20:37:49 +00:00
|
|
|
"""Tests for django.db.utils."""
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2019-08-20 07:54:41 +00:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, ProgrammingError, connection
|
2017-10-22 15:30:42 +00:00
|
|
|
from django.db.utils import ConnectionHandler, load_backend
|
2017-02-11 20:37:49 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2017-10-22 15:30:42 +00:00
|
|
|
from django.utils.connection import ConnectionDoesNotExist
|
2017-02-11 20:37:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectionHandlerTests(SimpleTestCase):
|
|
|
|
def test_connection_handler_no_databases(self):
|
2019-01-09 15:58:25 +00:00
|
|
|
"""
|
|
|
|
Empty DATABASES and empty 'default' settings default to the dummy
|
|
|
|
backend.
|
|
|
|
"""
|
|
|
|
for DATABASES in (
|
|
|
|
{}, # Empty DATABASES setting.
|
2022-02-03 19:24:19 +00:00
|
|
|
{"default": {}}, # Empty 'default' database.
|
2019-01-09 15:58:25 +00:00
|
|
|
):
|
|
|
|
with self.subTest(DATABASES=DATABASES):
|
|
|
|
self.assertImproperlyConfigured(DATABASES)
|
|
|
|
|
|
|
|
def assertImproperlyConfigured(self, DATABASES):
|
2017-02-11 20:37:49 +00:00
|
|
|
conns = ConnectionHandler(DATABASES)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
conns[DEFAULT_DB_ALIAS].settings_dict["ENGINE"], "django.db.backends.dummy"
|
|
|
|
)
|
2017-02-11 20:37:49 +00:00
|
|
|
msg = (
|
2022-02-03 19:24:19 +00:00
|
|
|
"settings.DATABASES is improperly configured. Please supply the "
|
|
|
|
"ENGINE value. Check settings documentation for more details."
|
2017-02-11 20:37:49 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
conns[DEFAULT_DB_ALIAS].ensure_connection()
|
|
|
|
|
2018-12-16 18:09:08 +00:00
|
|
|
def test_no_default_database(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
DATABASES = {"other": {}}
|
2018-12-16 18:09:08 +00:00
|
|
|
conns = ConnectionHandler(DATABASES)
|
|
|
|
msg = "You must define a 'default' database."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
conns["other"].ensure_connection()
|
2018-12-16 18:09:08 +00:00
|
|
|
|
2022-05-12 10:54:29 +00:00
|
|
|
def test_databases_property(self):
|
|
|
|
# The "databases" property is maintained for backwards compatibility
|
|
|
|
# with 3rd party packages. It should be an alias of the "settings"
|
|
|
|
# property.
|
|
|
|
conn = ConnectionHandler({})
|
|
|
|
self.assertNotEqual(conn.settings, {})
|
|
|
|
self.assertEqual(conn.settings, conn.databases)
|
|
|
|
|
2020-12-08 06:05:49 +00:00
|
|
|
def test_nonexistent_alias(self):
|
2017-10-22 15:30:42 +00:00
|
|
|
msg = "The connection 'nonexistent' doesn't exist."
|
2022-02-03 19:24:19 +00:00
|
|
|
conns = ConnectionHandler(
|
|
|
|
{
|
|
|
|
DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"},
|
|
|
|
}
|
|
|
|
)
|
2020-12-08 06:05:49 +00:00
|
|
|
with self.assertRaisesMessage(ConnectionDoesNotExist, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
conns["nonexistent"]
|
2020-12-08 06:05:49 +00:00
|
|
|
|
2017-02-11 20:37:49 +00:00
|
|
|
|
|
|
|
class DatabaseErrorWrapperTests(TestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test")
|
2017-02-11 20:37:49 +00:00
|
|
|
def test_reraising_backend_specific_database_exception(self):
|
2022-12-01 19:23:43 +00:00
|
|
|
from django.db.backends.postgresql.psycopg_any import is_psycopg3
|
|
|
|
|
2017-11-28 13:12:28 +00:00
|
|
|
with connection.cursor() as cursor:
|
|
|
|
msg = 'table "X" does not exist'
|
|
|
|
with self.assertRaisesMessage(ProgrammingError, msg) as cm:
|
|
|
|
cursor.execute('DROP TABLE "X"')
|
2017-02-11 20:37:49 +00:00
|
|
|
self.assertNotEqual(type(cm.exception), type(cm.exception.__cause__))
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__)
|
2022-12-01 19:23:43 +00:00
|
|
|
if is_psycopg3:
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.diag.sqlstate)
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.diag.message_primary)
|
|
|
|
else:
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.pgcode)
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.pgerror)
|
2017-02-11 20:37:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoadBackendTests(SimpleTestCase):
|
|
|
|
def test_load_backend_invalid_name(self):
|
|
|
|
msg = (
|
2020-11-15 20:34:35 +00:00
|
|
|
"'foo' isn't an available database backend or couldn't be "
|
|
|
|
"imported. Check the above exception. To use one of the built-in "
|
|
|
|
"backends, use 'django.db.backends.XXX', where XXX is one of:\n"
|
2017-02-11 20:37:49 +00:00
|
|
|
" 'mysql', 'oracle', 'postgresql', 'sqlite3'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm:
|
2022-02-03 19:24:19 +00:00
|
|
|
load_backend("foo")
|
2017-02-11 20:37:49 +00:00
|
|
|
self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'")
|