2017-04-25 14:03:48 +08:00
|
|
|
import unittest
|
|
|
|
|
2015-07-15 22:19:38 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-04-25 14:03:48 +08:00
|
|
|
from django.db import connection
|
2017-02-17 06:14:02 +08:00
|
|
|
from django.db.backends.utils import truncate_name
|
2017-04-25 14:03:48 +08:00
|
|
|
from django.db.utils import ProgrammingError, load_backend
|
|
|
|
from django.test import SimpleTestCase, TestCase
|
2015-07-15 22:19:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestLoadBackend(SimpleTestCase):
|
|
|
|
def test_load_backend_invalid_name(self):
|
2015-07-17 20:19:40 +08:00
|
|
|
msg = (
|
2015-07-15 22:19:38 +08:00
|
|
|
"'foo' isn't an available database backend.\n"
|
|
|
|
"Try using 'django.db.backends.XXX', where XXX is one of:\n"
|
2017-01-24 21:33:26 +08:00
|
|
|
" 'mysql', 'oracle', 'postgresql', 'sqlite3'"
|
2016-12-01 18:38:01 +08:00
|
|
|
)
|
2017-01-24 21:33:26 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg) as cm:
|
2015-07-15 22:19:38 +08:00
|
|
|
load_backend('foo')
|
2017-01-24 21:33:26 +08:00
|
|
|
self.assertEqual(str(cm.exception.__cause__), "No module named 'foo'")
|
2017-02-17 06:14:02 +08:00
|
|
|
|
|
|
|
def test_truncate_name(self):
|
|
|
|
self.assertEqual(truncate_name('some_table', 10), 'some_table')
|
|
|
|
self.assertEqual(truncate_name('some_long_table', 10), 'some_la38a')
|
|
|
|
self.assertEqual(truncate_name('some_long_table', 10, 3), 'some_loa38')
|
|
|
|
self.assertEqual(truncate_name('some_long_table'), 'some_long_table')
|
2017-02-15 19:22:34 +08:00
|
|
|
# "user"."table" syntax
|
|
|
|
self.assertEqual(truncate_name('username"."some_table', 10), 'username"."some_table')
|
|
|
|
self.assertEqual(truncate_name('username"."some_long_table', 10), 'username"."some_la38a')
|
|
|
|
self.assertEqual(truncate_name('username"."some_long_table', 10, 3), 'username"."some_loa38')
|
2017-04-25 14:03:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific tests')
|
|
|
|
class TestDatabaseErrorWrapper(TestCase):
|
|
|
|
def test_reraising_backend_specific_database_exception(self):
|
|
|
|
cursor = connection.cursor()
|
|
|
|
msg = 'table "X" does not exist'
|
|
|
|
with self.assertRaisesMessage(ProgrammingError, msg) as cm:
|
|
|
|
cursor.execute('DROP TABLE "X"')
|
|
|
|
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)
|