Skip to content

Commit 88c8c43

Browse files
committed
Improved tests to require explicit db config and share connection fixtures.
1 parent f376b28 commit 88c8c43

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

.github/workflows/tests/test_notifications.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@
1010
from 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+
1340
def 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

209205
def test_k_sink_injection():

.github/workflows/tests/test_pgstac_notifications.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@
1212
@pytest.fixture(scope='session')
1313
def db_connection():
1414
"""Create database connection for testing."""
15-
# Use the same environment variables as the pgstac bootstrap
15+
# Require all database connection parameters to be explicitly set
16+
required_vars = ['PGHOST', 'PGPORT', 'PGDATABASE', 'PGUSER', 'PGPASSWORD']
17+
missing_vars = [var for var in required_vars if not os.getenv(var)]
18+
19+
if missing_vars:
20+
pytest.fail(f"Required environment variables not set: {', '.join(missing_vars)}")
21+
1622
connection_params = {
17-
'host': os.getenv('PGHOST', 'localhost'),
18-
'port': int(os.getenv('PGPORT', '5432')),
19-
'database': os.getenv('PGDATABASE', 'postgres'),
20-
'user': os.getenv('PGUSER', 'postgres'),
21-
'password': os.getenv('PGPASSWORD', 'password')
23+
'host': os.getenv('PGHOST'),
24+
'port': int(os.getenv('PGPORT')),
25+
'database': os.getenv('PGDATABASE'),
26+
'user': os.getenv('PGUSER'),
27+
'password': os.getenv('PGPASSWORD')
2228
}
2329

24-
conn = psycopg2.connect(**connection_params)
25-
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
26-
yield conn
27-
conn.close()
30+
try:
31+
conn = psycopg2.connect(**connection_params)
32+
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
33+
yield conn
34+
conn.close()
35+
except psycopg2.Error as e:
36+
pytest.fail(f"Cannot connect to database: {e}")
2837

2938

3039
@pytest.fixture(scope='session')

0 commit comments

Comments
 (0)