| 1 |
"""Tests for django.db.utils.""" |
| 2 |
|
| 3 |
import unittest |
| 4 |
|
| 5 |
from django.core.exceptions import ImproperlyConfigured |
| 6 |
from django.db import DEFAULT_DB_ALIAS, ProgrammingError, connection |
| 7 |
from django.db.utils import ConnectionHandler, load_backend |
| 8 |
from django.test import SimpleTestCase, TestCase |
| 9 |
from django.utils.connection import ConnectionDoesNotExist |
| 10 |
|
| 11 |
|
| 12 |
class ConnectionHandlerTests(SimpleTestCase): |
| 13 |
def test_connection_handler_no_databases(self): |
| 14 |
""" |
| 15 |
Empty DATABASES and empty 'default' settings default to the dummy |
| 16 |
backend. |
| 17 |
""" |
| 18 |
for DATABASES in ( |
| 19 |
{}, # Empty DATABASES setting. |
| 20 |
{"default": {}}, # Empty 'default' database. |
| 21 |
): |
| 22 |
with self.subTest(DATABASES=DATABASES): |
| 23 |
self.assertImproperlyConfigured(DATABASES) |
| 24 |
|
| 25 |
def assertImproperlyConfigured(self, DATABASES): |
| 26 |
conns = ConnectionHandler(DATABASES) |
| 27 |
self.assertEqual( |
| 28 |
conns[DEFAULT_DB_ALIAS].settings_dict["ENGINE"], "django.db.backends.dummy" |
| 29 |
) |
| 30 |
msg = ( |
| 31 |
"settings.DATABASES is improperly configured. Please supply the " |
| 32 |
"ENGINE value. Check settings documentation for more details." |
| 33 |
) |
| 34 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 35 |
conns[DEFAULT_DB_ALIAS].ensure_connection() |
| 36 |
|
| 37 |
def test_no_default_database(self): |
| 38 |
DATABASES = {"other": {}} |
| 39 |
conns = ConnectionHandler(DATABASES) |
| 40 |
msg = "You must define a 'default' database." |
| 41 |
with self.assertRaisesMessage(ImproperlyConfigured, msg): |
| 42 |
conns["other"].ensure_connection() |
| 43 |
|
| 44 |
def test_databases_property(self): |
| 45 |
# The "databases" property is maintained for backwards compatibility |
| 46 |
# with 3rd party packages. It should be an alias of the "settings" |
| 47 |
# property. |
| 48 |
conn = ConnectionHandler({}) |
| 49 |
self.assertNotEqual(conn.settings, {}) |
| 50 |
self.assertEqual(conn.settings, conn.databases) |
| 51 |
|
| 52 |
def test_nonexistent_alias(self): |
| 53 |
msg = "The connection 'nonexistent' doesn't exist." |
| 54 |
conns = ConnectionHandler( |
| 55 |
{ |
| 56 |
DEFAULT_DB_ALIAS: {"ENGINE": "django.db.backends.dummy"}, |
| 57 |
} |
| 58 |
) |
| 59 |
with self.assertRaisesMessage(ConnectionDoesNotExist, msg): |
| 60 |
conns["nonexistent"] |
| 61 |
|
| 62 |
|
| 63 |
class DatabaseErrorWrapperTests(TestCase): |
| 64 |
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL test") |
| 65 |
def test_reraising_backend_specific_database_exception(self): |
| 66 |
from django.db.backends.postgresql.psycopg_any import is_psycopg3 |
| 67 |
|
| 68 |
with connection.cursor() as cursor: |
| 69 |
msg = 'table "X" does not exist' |
| 70 |
with self.assertRaisesMessage(ProgrammingError, msg) as cm: |
| 71 |
cursor.execute('DROP TABLE "X"') |
| 72 |
self.assertNotEqual(type(cm.exception), type(cm.exception.__cause__)) |
| 73 |
self.assertIsNotNone(cm.exception.__cause__) |
| 74 |
if is_psycopg3: |
| 75 |
self.assertIsNotNone(cm.exception.__cause__.diag.sqlstate) |
| 76 |
self.assertIsNotNone(cm.exception.__cause__.diag.message_primary) |
| 77 |
else: |
| 78 |
self.assertIsNotNone(cm.exception.__cause__.pgcode) |
| 79 |
self.assertIsNotNone(cm.exception.__cause__.pgerror) |
| 80 |
|
| 81 |
|
| 82 |
class LoadBackendTests(SimpleTestCase): |
| 83 |
def test_load_backend_invalid_name(self): |
| 84 |
msg = ( |
| 85 |
"'foo' isn't an available database backend or couldn't be " |
| 86 |
"imported. Check the above exception. To use one of the built-in " |
| 87 |
"backends, use 'django.db.backends.XXX', where XXX is one of:\n" |
| 88 |
" 'mysql', 'oracle', 'postgresql', 'sqlite3'" |
| 89 |
) |
| 90 |
with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm: |
| 91 |
load_backend("foo") |
| 92 |
self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'") |