44import  pytest 
55from  django .db  import  connection 
66from  django .db .utils  import  OperationalError 
7+ from  django .test  import  override_settings 
78
8- from  ansible_base .lib .utils .db  import  advisory_lock , migrations_are_complete 
9+ from  ansible_base .lib .utils .db  import  advisory_lock , get_pg_notify_params ,  migrations_are_complete ,  psycopg_kwargs_from_settings_dict 
910
1011
1112@pytest .mark .django_db  
@@ -14,6 +15,73 @@ def test_migrations_are_complete():
1415    assert  migrations_are_complete ()
1516
1617
18+ class  TestPGNotifyConnection :
19+     TEST_DATABASE_DICT  =  {
20+         "default" : {
21+             "ENGINE" : "django.db.backends.postgresql" ,
22+             "HOST" : "https://foo.invalid" ,
23+             "PORT" : 55434 ,
24+             "USER" : "dab_user" ,
25+             "PASSWORD" : "dabbing" ,
26+             "NAME" : "dab_db" ,
27+         }
28+     }
29+     PSYCOPG_KWARGS  =  {
30+         'dbname' : 'dab_db' ,
31+         'client_encoding' : 'UTF8' ,
32+         # kwargs containing objects can not be compared so they will be ignored 
33+         # 'cursor_factory': <class 'django.db.backends.postgresql.base.Cursor'>, 
34+         'user' : 'dab_user' ,
35+         'password' : 'dabbing' ,
36+         'host' : 'https://foo.invalid' ,
37+         'port' : 55434 ,
38+         # 'context': <psycopg.adapt.AdaptersMap object at 0x7f537f2d9f70>, 
39+         'prepare_threshold' : None ,
40+         'autocommit' : True ,
41+     }
42+ 
43+     @pytest .fixture  
44+     def  mock_settings (self ):
45+         with  override_settings (DATABASES = self .TEST_DATABASE_DICT , USE_TZ = False ):
46+             yield 
47+ 
48+     def  _trim_python_objects (self , psycopg_params ):
49+         # These remove those commented-out kwargs in PSYCOPG_KWARGS 
50+         psycopg_params .pop ('cursor_factory' )
51+         psycopg_params .pop ('context' )
52+         return  psycopg_params 
53+ 
54+     def  test_default_behavior (self , mock_settings ):
55+         params  =  self ._trim_python_objects (get_pg_notify_params ())
56+         assert  params  ==  self .PSYCOPG_KWARGS 
57+ 
58+     def  test_pg_notify_extra_options (self , mock_settings ):
59+         params  =  self ._trim_python_objects (get_pg_notify_params (application_name = 'joe_connection' ))
60+         expected  =  self .PSYCOPG_KWARGS .copy ()
61+         expected ['application_name' ] =  'joe_connection' 
62+         assert  params  ==  expected 
63+ 
64+     def  test_lister_databases (self , mock_settings ):
65+         LISTENER_DATABASES  =  {"default" : {"HOST" : "https://foo.anotherhost.invalid" }}
66+         with  override_settings (LISTENER_DATABASES = LISTENER_DATABASES ):
67+             params  =  self ._trim_python_objects (get_pg_notify_params ())
68+             assert  params ['host' ] ==  "https://foo.anotherhost.invalid" 
69+ 
70+     def  test_pg_notify_databases (self , mock_settings ):
71+         PG_NOTIFY_DATABASES  =  {"default" : {"HOST" : "https://foo.anotherhost2.invalid" }}
72+         with  override_settings (PG_NOTIFY_DATABASES = PG_NOTIFY_DATABASES ):
73+             params  =  self ._trim_python_objects (get_pg_notify_params ())
74+             assert  params ['host' ] ==  "https://foo.anotherhost2.invalid" 
75+ 
76+     def  test_psycopg_kwargs_from_settings_dict (self ):
77+         "More of a unit test, doing the same thing" 
78+         test_dict  =  self .TEST_DATABASE_DICT ["default" ].copy ()
79+         test_dict ['OPTIONS' ] =  {'autocommit' : True }
80+         with  override_settings (USE_TZ = False ):
81+             psycopg_params  =  self ._trim_python_objects (psycopg_kwargs_from_settings_dict (test_dict ))
82+             assert  psycopg_params  ==  self .PSYCOPG_KWARGS 
83+ 
84+ 
1785class  TestAdvisoryLock :
1886    @pytest .fixture (autouse = True ) 
1987    def  skip_if_sqlite (self ):
0 commit comments