#!/usr/bin/env python3 """ Test script to verify the behavior of the load_env_from_file function and the osm_cal.py script when the .env file exists or doesn't exist. """ import os import sys import subprocess import shutil from oedb.utils.db import load_env_from_file def test_load_env_from_file(): """ Test the load_env_from_file function directly. """ print("\n=== Testing load_env_from_file function ===") # Backup the .env file if it exists env_exists = os.path.exists('.env') if env_exists: print("Backing up existing .env file...") shutil.copy('.env', '.env.backup') # Test when .env file doesn't exist if env_exists: os.remove('.env') print("\nTesting when .env file doesn't exist:") result = load_env_from_file() print(f"load_env_from_file() returned: {result}") # Create a test .env file print("\nCreating test .env file...") with open('.env', 'w') as f: f.write("TEST_VAR=test_value\n") # Test when .env file exists print("\nTesting when .env file exists:") result = load_env_from_file() print(f"load_env_from_file() returned: {result}") print(f"TEST_VAR environment variable: {os.getenv('TEST_VAR')}") # Clean up os.remove('.env') # Restore the original .env file if it existed if env_exists: print("\nRestoring original .env file...") shutil.move('.env.backup', '.env') def test_osm_cal_script(): """ Test the osm_cal.py script behavior with and without .env file. """ print("\n=== Testing osm_cal.py script ===") # Backup the .env file if it exists env_exists = os.path.exists('.env') if env_exists: print("Backing up existing .env file...") shutil.copy('.env', '.env.backup') # Test when .env file doesn't exist if env_exists: os.remove('.env') print("\nTesting osm_cal.py when .env file doesn't exist:") try: result = subprocess.run( [sys.executable, 'extractors/osm_cal.py'], capture_output=True, text=True, timeout=5 # Timeout after 5 seconds ) print(f"Exit code: {result.returncode}") print(f"Output: {result.stdout}") print(f"Error: {result.stderr}") except subprocess.TimeoutExpired: print("Process timed out - this might indicate it's trying to fetch RSS feed despite missing .env") # Create a test .env file with minimal required variables print("\nCreating test .env file...") with open('.env', 'w') as f: f.write("DB_NAME=test_db\n") f.write("DB_HOST=localhost\n") f.write("DB_USER=test_user\n") f.write("POSTGRES_PASSWORD=test_password\n") # Test when .env file exists print("\nTesting osm_cal.py when .env file exists:") try: # We'll use a very short timeout since we expect it to try to connect to the database # and fail, but we just want to verify it gets past the .env check result = subprocess.run( [sys.executable, 'extractors/osm_cal.py'], capture_output=True, text=True, timeout=2 # Short timeout ) print(f"Exit code: {result.returncode}") print(f"Output: {result.stdout}") print(f"Error: {result.stderr}") except subprocess.TimeoutExpired: print("Process timed out - this is expected as it's trying to connect to the database") # Clean up os.remove('.env') # Restore the original .env file if it existed if env_exists: print("\nRestoring original .env file...") shutil.move('.env.backup', '.env') if __name__ == "__main__": test_load_env_from_file() test_osm_cal_script()