summaryrefslogtreecommitdiff
path: root/tests/backends/tests.py
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2015-05-14 19:27:31 +0200
committerClaude Paroz <claude@2xlibre.net>2015-05-15 18:46:05 +0200
commitcdf7f90f95022002464faede7f0a75fb11d10ecb (patch)
tree3424091fec98d58ac53be8dfa49d5e46d8199e77 /tests/backends/tests.py
parent3c659856eb9d5a45e02213f1e8f9fcbb03bd12cf (diff)
[1.8.x] Fixed #24791 -- Added fallback when 'postgres' database isn't available
Thanks Carl Meyer and Tim Graham for the reviews. Backport of 322605035 from master.
Diffstat (limited to 'tests/backends/tests.py')
-rw-r--r--tests/backends/tests.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index a624bf57b2..11136e3b09 100644
--- a/tests/backends/tests.py
+++ b/tests/backends/tests.py
@@ -162,6 +162,32 @@ class PostgreSQLTests(TestCase):
self.assert_parses("PostgreSQL 9.4beta1", 90400)
self.assert_parses("PostgreSQL 9.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)", 90301)
+ def test_nodb_connection(self):
+ """
+ Test that the _nodb_connection property fallbacks to the default connection
+ database when access to the 'postgres' database is not granted.
+ """
+ def mocked_connect(self):
+ if self.settings_dict['NAME'] is None:
+ raise DatabaseError()
+ return ''
+
+ nodb_conn = connection._nodb_connection
+ self.assertIsNone(nodb_conn.settings_dict['NAME'])
+
+ # Now assume the 'postgres' db isn't available
+ del connection._nodb_connection
+ with warnings.catch_warnings(record=True) as w:
+ with mock.patch('django.db.backends.base.base.BaseDatabaseWrapper.connect',
+ side_effect=mocked_connect, autospec=True):
+ nodb_conn = connection._nodb_connection
+ del connection._nodb_connection
+ self.assertIsNotNone(nodb_conn.settings_dict['NAME'])
+ self.assertEqual(nodb_conn.settings_dict['NAME'], settings.DATABASES[DEFAULT_DB_ALIAS]['NAME'])
+ # Check a RuntimeWarning nas been emitted
+ self.assertEqual(len(w), 1)
+ self.assertEqual(w[0].message.__class__, RuntimeWarning)
+
def test_version_detection(self):
"""Test PostgreSQL version detection"""