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
|
|
|
|
from django.db.utils import ConnectionHandler, load_backend
|
2017-02-11 20:37:49 +00:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
{'default': {}}, # Empty 'default' database.
|
|
|
|
):
|
|
|
|
with self.subTest(DATABASES=DATABASES):
|
|
|
|
self.assertImproperlyConfigured(DATABASES)
|
|
|
|
|
|
|
|
def assertImproperlyConfigured(self, DATABASES):
|
2017-02-11 20:37:49 +00:00
|
|
|
conns = ConnectionHandler(DATABASES)
|
|
|
|
self.assertEqual(conns[DEFAULT_DB_ALIAS].settings_dict['ENGINE'], 'django.db.backends.dummy')
|
|
|
|
msg = (
|
|
|
|
'settings.DATABASES is improperly configured. Please supply the '
|
|
|
|
'ENGINE value. Check settings documentation for more details.'
|
|
|
|
)
|
|
|
|
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):
|
|
|
|
DATABASES = {'other': {}}
|
|
|
|
conns = ConnectionHandler(DATABASES)
|
|
|
|
msg = "You must define a 'default' database."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
conns['other'].ensure_connection()
|
|
|
|
|
2017-02-11 20:37:49 +00:00
|
|
|
|
|
|
|
class DatabaseErrorWrapperTests(TestCase):
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test')
|
|
|
|
def test_reraising_backend_specific_database_exception(self):
|
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__)
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.pgcode)
|
|
|
|
self.assertIsNotNone(cm.exception.__cause__.pgerror)
|
|
|
|
|
|
|
|
|
|
|
|
class LoadBackendTests(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_load_backend_invalid_name(self):
|
|
|
|
msg = (
|
|
|
|
"'foo' isn't an available database backend.\n"
|
|
|
|
"Try using 'django.db.backends.XXX', where XXX is one of:\n"
|
|
|
|
" 'mysql', 'oracle', 'postgresql', 'sqlite3'"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm:
|
|
|
|
load_backend('foo')
|
|
|
|
self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'")
|