From 562bca67b9a3509f7c9806c7051398ad775e8e09 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 25 Apr 2017 08:03:48 +0200 Subject: [PATCH] Added test for reraising backend-specific database exceptions. --- tests/backends/test_utils.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/backends/test_utils.py b/tests/backends/test_utils.py index bf62c8c6df..60429c5543 100644 --- a/tests/backends/test_utils.py +++ b/tests/backends/test_utils.py @@ -1,7 +1,10 @@ +import unittest + from django.core.exceptions import ImproperlyConfigured +from django.db import connection from django.db.backends.utils import truncate_name -from django.db.utils import load_backend -from django.test import SimpleTestCase +from django.db.utils import ProgrammingError, load_backend +from django.test import SimpleTestCase, TestCase class TestLoadBackend(SimpleTestCase): @@ -24,3 +27,16 @@ class TestLoadBackend(SimpleTestCase): 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') + + +@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)