Skip to content

Commit 462e320

Browse files
committed
Add another test and wrap up linters
1 parent d41f60a commit 462e320

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

ansible_base/lib/utils/db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from zlib import crc32
55

66
import psycopg
7-
87
from django.conf import settings
98
from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction
109
from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper

test_app/tests/lib/utils/test_db.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22
import time
33

44
import psycopg
5-
65
import pytest
6+
from django.conf import settings
77
from django.db import connection
88
from django.db.utils import OperationalError
99
from django.test import override_settings
10-
from django.conf import settings
1110

1211
from ansible_base.lib.utils.db import (
1312
advisory_lock,
1413
get_pg_notify_params,
1514
migrations_are_complete,
16-
psycopg_kwargs_from_settings_dict,
1715
psycopg_conn_string_from_settings_dict,
16+
psycopg_kwargs_from_settings_dict,
1817
)
1918

2019

@@ -90,31 +89,45 @@ def test_psycopg_kwargs_from_settings_dict(self):
9089
psycopg_params = self._trim_python_objects(psycopg_kwargs_from_settings_dict(test_dict))
9190
assert psycopg_params == self.PSYCOPG_KWARGS
9291

92+
def test_psycopg_kwargs_use(self):
93+
"This assures that the data we get for the kwargs are usable, and demos how to use"
94+
if connection.vendor == 'sqlite':
95+
pytest.skip('Test needs to connect to postgres which is not running')
96+
97+
test_dict = settings.DATABASES['default'].copy()
98+
test_dict['OPTIONS'] = {'autocommit': True}
99+
with override_settings(USE_TZ=False):
100+
psycopg_params = self._trim_python_objects(psycopg_kwargs_from_settings_dict(test_dict))
101+
102+
psycopg.connect(**psycopg_params)
103+
93104
def test_listener_string_production(self):
94105
"This is a test to correspond to PG_NOTIFY_DSN_SERVER type settings in eda-server"
95-
args = psycopg_conn_string_from_settings_dict(
96-
{
97-
"ENGINE": "django.db.backends.postgresql",
98-
"HOST": "127.0.0.1",
99-
"PORT": 5432,
100-
"USER": "postgres",
101-
"PASSWORD": "DB_PASSWORD",
102-
"NAME": "eda",
103-
"TIME_ZONE": settings.DATABASES['default']['TIME_ZONE'],
104-
"OPTIONS": {
105-
"sslmode": "allow",
106-
"sslcert": "",
107-
"sslkey": "",
108-
"sslrootcert": "",
109-
},
110-
}
111-
)
106+
with override_settings(USE_TZ=False):
107+
args = psycopg_conn_string_from_settings_dict(
108+
{
109+
"ENGINE": "django.db.backends.postgresql",
110+
"HOST": "127.0.0.1",
111+
"PORT": 5432,
112+
"USER": "postgres",
113+
"PASSWORD": "DB_PASSWORD",
114+
"NAME": "eda",
115+
"OPTIONS": {
116+
"sslmode": "allow",
117+
"sslcert": "",
118+
"sslkey": "",
119+
"sslrootcert": "",
120+
},
121+
}
122+
)
112123
assert args == (
113124
"dbname=eda sslmode=allow sslcert='' sslkey='' sslrootcert='' client_encoding=UTF8 user=postgres password=DB_PASSWORD host=127.0.0.1 port=5432"
114125
)
115126

116127
def test_listener_string_production_use(self):
117128
"This assures that the data we get for the connection string is usable, and demos how to use"
129+
if connection.vendor == 'sqlite':
130+
pytest.skip('Test needs to connect to postgres which is not running')
118131
args = psycopg_conn_string_from_settings_dict(settings.DATABASES['default'])
119132
psycopg.connect(conninfo=args)
120133

0 commit comments

Comments
 (0)