Skip to content

Commit 5afa60d

Browse files
jucorclaude
andcommitted
Fix test DB connection: use DATABASE_URL with dotenv
connect_to_db() in test_postgres_real_data.py used POSTGRES_* env vars with wrong defaults (password='postgres', database='polismath'), causing both test_conversation_from_postgres and test_pakistan_conversation_batch to fail with auth errors even when Postgres is running. Switch to DATABASE_URL (the standard in all other delphi Python code) and load it from the repo root .env via python-dotenv. Refs #2442 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 655bcfa commit 5afa60d

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

delphi/tests/test_postgres_real_data.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,20 @@ def write_to_dynamodb(dynamodb_client, conversation_id, conv):
102102

103103

104104
def connect_to_db():
105-
"""Connect to PostgreSQL database."""
105+
"""Connect to PostgreSQL database using DATABASE_URL from env / .env file."""
106+
from pathlib import Path
107+
from dotenv import load_dotenv
108+
109+
# Load .env from the repo root (two levels up from tests/)
110+
load_dotenv(Path(__file__).parent.parent.parent / '.env')
111+
112+
database_url = os.environ.get('DATABASE_URL')
113+
if not database_url:
114+
print("DATABASE_URL environment variable is not set")
115+
return None
116+
106117
try:
107-
conn = psycopg2.connect(
108-
database=os.environ.get('POSTGRES_DB', 'polismath'),
109-
user=os.environ.get('POSTGRES_USER', 'postgres'),
110-
password=os.environ.get('POSTGRES_PASSWORD', 'postgres'),
111-
host=os.environ.get('POSTGRES_HOST', 'localhost'),
112-
port=os.environ.get('POSTGRES_PORT', '5432'),
113-
connect_timeout=5,
114-
)
118+
conn = psycopg2.connect(database_url, connect_timeout=5)
115119
print("Connected to database successfully")
116120
return conn
117121
except Exception as e:

0 commit comments

Comments
 (0)