11import threading
22import time
33
4+ import psycopg
5+
46import pytest
57from django .db import connection
68from django .db .utils import OperationalError
79from django .test import override_settings
10+ from django .conf import settings
811
9- from ansible_base .lib .utils .db import advisory_lock , get_pg_notify_params , migrations_are_complete , psycopg_kwargs_from_settings_dict
12+ from ansible_base .lib .utils .db import (
13+ advisory_lock ,
14+ get_pg_notify_params ,
15+ migrations_are_complete ,
16+ psycopg_kwargs_from_settings_dict ,
17+ psycopg_conn_string_from_settings_dict
18+ )
1019
1120
1221@pytest .mark .django_db
@@ -81,6 +90,30 @@ def test_psycopg_kwargs_from_settings_dict(self):
8190 psycopg_params = self ._trim_python_objects (psycopg_kwargs_from_settings_dict (test_dict ))
8291 assert psycopg_params == self .PSYCOPG_KWARGS
8392
93+ def test_listener_string_production (self ):
94+ "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+ "ENGINE" : "django.db.backends.postgresql" ,
97+ "HOST" : "127.0.0.1" ,
98+ "PORT" : 5432 ,
99+ "USER" : "postgres" ,
100+ "PASSWORD" : "DB_PASSWORD" ,
101+ "NAME" : "eda" ,
102+ "TIME_ZONE" : settings .DATABASES ['default' ]['TIME_ZONE' ],
103+ "OPTIONS" : {
104+ "sslmode" : "allow" ,
105+ "sslcert" : "" ,
106+ "sslkey" : "" ,
107+ "sslrootcert" : "" ,
108+ },
109+ })
110+ assert args == "dbname=eda sslmode=allow sslcert='' sslkey='' sslrootcert='' client_encoding=UTF8 user=postgres password=DB_PASSWORD host=127.0.0.1 port=5432"
111+
112+ def test_listener_string_production_use (self ):
113+ "This assures that the data we get for the connection string is usable, and demos how to use"
114+ args = psycopg_conn_string_from_settings_dict (settings .DATABASES ['default' ])
115+ psycopg .connect (conninfo = args )
116+
84117
85118class TestAdvisoryLock :
86119 @pytest .fixture (autouse = True )
@@ -104,7 +137,7 @@ def background_task(django_db_blocker):
104137 def test_determine_lock_is_held (self , django_db_blocker ):
105138 thread = threading .Thread (target = TestAdvisoryLock .background_task , args = (django_db_blocker ,))
106139 thread .start ()
107- for _ in range (5 ):
140+ for _ in range (20 ):
108141 with advisory_lock ('background_task_lock' , wait = False ) as held :
109142 if held is False :
110143 break
0 commit comments