summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorHasan Ramezani <hasan.r67@gmail.com>2020-12-23 23:39:43 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-01-20 17:30:37 +0100
commitdcb3ad3319cad5c270a1856fd5f355e37cf9d474 (patch)
tree919ded9b849cb0a4ae66052c030f5bfb2e980d1a /tests
parentf054468cac325e8d8fa4d5934b939b93242a3730 (diff)
downloaddjango-dcb3ad3319cad5c270a1856fd5f355e37cf9d474.tar.gz
Fixed #32292 -- Added support for connection by service name to PostgreSQL.
Diffstat (limited to 'tests')
-rw-r--r--tests/backends/postgresql/tests.py30
-rw-r--r--tests/dbshell/test_postgresql.py6
2 files changed, 36 insertions, 0 deletions
diff --git a/tests/backends/postgresql/tests.py b/tests/backends/postgresql/tests.py
index 8d0a801ea2..94ebfbdaf8 100644
--- a/tests/backends/postgresql/tests.py
+++ b/tests/backends/postgresql/tests.py
@@ -68,6 +68,36 @@ class Tests(TestCase):
with self.assertRaisesMessage(ImproperlyConfigured, msg):
DatabaseWrapper(settings).get_connection_params()
+ def test_database_name_empty(self):
+ from django.db.backends.postgresql.base import DatabaseWrapper
+ settings = connection.settings_dict.copy()
+ settings['NAME'] = ''
+ msg = (
+ "settings.DATABASES is improperly configured. Please supply the "
+ "NAME or OPTIONS['service'] value."
+ )
+ with self.assertRaisesMessage(ImproperlyConfigured, msg):
+ DatabaseWrapper(settings).get_connection_params()
+
+ def test_service_name(self):
+ from django.db.backends.postgresql.base import DatabaseWrapper
+ settings = connection.settings_dict.copy()
+ settings['OPTIONS'] = {'service': 'my_service'}
+ settings['NAME'] = ''
+ params = DatabaseWrapper(settings).get_connection_params()
+ self.assertEqual(params['service'], 'my_service')
+ self.assertNotIn('database', params)
+
+ def test_service_name_default_db(self):
+ # None is used to connect to the default 'postgres' db.
+ from django.db.backends.postgresql.base import DatabaseWrapper
+ settings = connection.settings_dict.copy()
+ settings['NAME'] = None
+ settings['OPTIONS'] = {'service': 'django_test'}
+ params = DatabaseWrapper(settings).get_connection_params()
+ self.assertEqual(params['database'], 'postgres')
+ self.assertNotIn('service', params)
+
def test_connect_and_rollback(self):
"""
PostgreSQL shouldn't roll back SET TIME ZONE, even if the first
diff --git a/tests/dbshell/test_postgresql.py b/tests/dbshell/test_postgresql.py
index ccf49d7e50..e9eb131db7 100644
--- a/tests/dbshell/test_postgresql.py
+++ b/tests/dbshell/test_postgresql.py
@@ -67,6 +67,12 @@ class PostgreSqlDbshellCommandTestCase(SimpleTestCase):
)
)
+ def test_service(self):
+ self.assertEqual(
+ self.settings_to_cmd_args_env({'OPTIONS': {'service': 'django_test'}}),
+ (['psql', 'postgres'], {'PGSERVICE': 'django_test'}),
+ )
+
def test_column(self):
self.assertEqual(
self.settings_to_cmd_args_env({