42 lines
No EOL
1.3 KiB
Python
Executable file
42 lines
No EOL
1.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
# Test script to verify database connectivity check
|
|
|
|
import os
|
|
import sys
|
|
import psycopg2
|
|
|
|
# Import the db_connect and check_db_connection functions from oedb.utils.db
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
from oedb.utils.db import db_connect, check_db_connection
|
|
|
|
def test_db_connection():
|
|
"""Test the database connection check functionality"""
|
|
print("Testing database connection...")
|
|
|
|
# Test with valid connection
|
|
result = check_db_connection()
|
|
if result:
|
|
print("✅ Database connection successful")
|
|
else:
|
|
print("❌ Database connection failed with valid credentials")
|
|
|
|
# Test with invalid connection (by temporarily modifying environment variables)
|
|
original_host = os.environ.get("DB_HOST", "")
|
|
os.environ["DB_HOST"] = "nonexistent-host"
|
|
|
|
result = check_db_connection()
|
|
if not result:
|
|
print("✅ Correctly detected invalid database connection")
|
|
else:
|
|
print("❌ Failed to detect invalid database connection")
|
|
|
|
# Restore original environment
|
|
if original_host:
|
|
os.environ["DB_HOST"] = original_host
|
|
else:
|
|
os.environ.pop("DB_HOST", None)
|
|
|
|
print("Database connection test completed")
|
|
|
|
if __name__ == "__main__":
|
|
test_db_connection() |