tests.py 3.8 KB

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