1010from datetime import datetime
1111
1212
13+ @pytest .fixture (scope = 'session' )
14+ def db_connection ():
15+ """Create database connection for testing."""
16+ # Require all database connection parameters to be explicitly set
17+ required_vars = ['PGHOST' , 'PGPORT' , 'PGDATABASE' , 'PGUSER' , 'PGPASSWORD' ]
18+ missing_vars = [var for var in required_vars if not os .getenv (var )]
19+
20+ if missing_vars :
21+ pytest .fail (f"Required environment variables not set: { ', ' .join (missing_vars )} " )
22+
23+ connection_params = {
24+ 'host' : os .getenv ('PGHOST' ),
25+ 'port' : int (os .getenv ('PGPORT' )),
26+ 'database' : os .getenv ('PGDATABASE' ),
27+ 'user' : os .getenv ('PGUSER' ),
28+ 'password' : os .getenv ('PGPASSWORD' )
29+ }
30+
31+ try :
32+ conn = psycopg2 .connect (** connection_params )
33+ conn .set_isolation_level (psycopg2 .extensions .ISOLATION_LEVEL_AUTOCOMMIT )
34+ yield conn
35+ conn .close ()
36+ except psycopg2 .Error as e :
37+ pytest .fail (f"Cannot connect to database: { e } " )
38+
39+
1340def test_eoapi_notifier_deployment ():
1441 """Test that eoapi-notifier deployment is running."""
1542 # Check if eoapi-notifier deployment exists and is ready
@@ -98,23 +125,9 @@ def test_eoapi_notifier_logs_show_connection():
98125 assert "Authentication failed" not in logs , "Should not have auth errors"
99126
100127
101- def test_database_notification_triggers_exist ():
128+ def test_database_notification_triggers_exist (db_connection ):
102129 """Test that pgstac notification triggers are installed."""
103- # Port forward to database if not already done
104- import psycopg2
105- import os
106-
107- try :
108- # Try to connect using environment variables set by the test runner
109- conn = psycopg2 .connect (
110- host = os .getenv ('PGHOST' , 'localhost' ),
111- port = int (os .getenv ('PGPORT' , '5432' )),
112- database = os .getenv ('PGDATABASE' , 'eoapi' ),
113- user = os .getenv ('PGUSER' , 'eoapi' ),
114- password = os .getenv ('PGPASSWORD' , '' )
115- )
116-
117- with conn .cursor () as cur :
130+ with db_connection .cursor () as cur :
118131 # Check if the notification function exists
119132 cur .execute ("""
120133 SELECT EXISTS(
@@ -139,13 +152,10 @@ def test_database_notification_triggers_exist():
139152 trigger_count = result [0 ] if result else 0
140153 assert trigger_count >= 3 , f"Should have at least 3 triggers (INSERT, UPDATE, DELETE), found { trigger_count } "
141154
142- conn .close ()
143155
144- except (psycopg2 .Error , ConnectionError , OSError ):
145- pytest .skip ("Cannot connect to database for trigger verification" )
146156
147157
148- def test_end_to_end_notification_flow ():
158+ def test_end_to_end_notification_flow (db_connection ):
149159 """Test complete flow: database → eoapi-notifier → Knative CloudEvents sink."""
150160
151161 # Skip if notifications not enabled
@@ -160,23 +170,10 @@ def test_end_to_end_notification_flow():
160170
161171 sink_pod = result .stdout .strip ()
162172
163- # Connect to database using test environment
164- try :
165- conn = psycopg2 .connect (
166- host = os .getenv ('PGHOST' , 'localhost' ),
167- port = int (os .getenv ('PGPORT' , '5432' )),
168- database = os .getenv ('PGDATABASE' , 'eoapi' ),
169- user = os .getenv ('PGUSER' , 'eoapi' ),
170- password = os .getenv ('PGPASSWORD' , '' )
171- )
172- conn .set_isolation_level (psycopg2 .extensions .ISOLATION_LEVEL_AUTOCOMMIT )
173- except psycopg2 .Error as e :
174- pytest .skip (f"Cannot connect to database: { e } " )
175-
176173 # Insert test item and check for CloudEvent
177174 test_item_id = f"e2e-test-{ int (time .time ())} "
178175 try :
179- with conn .cursor () as cursor :
176+ with db_connection .cursor () as cursor :
180177 cursor .execute ("SELECT pgstac.create_item(%s);" , (json .dumps ({
181178 "id" : test_item_id ,
182179 "type" : "Feature" ,
@@ -201,9 +198,8 @@ def test_end_to_end_notification_flow():
201198
202199 finally :
203200 # Cleanup
204- with conn .cursor () as cursor :
201+ with db_connection .cursor () as cursor :
205202 cursor .execute ("SELECT pgstac.delete_item(%s);" , (test_item_id ,))
206- conn .close ()
207203
208204
209205def test_k_sink_injection ():
0 commit comments