import tempfile import os import shutil from pathlib import Path class TestDatabaseManager: """Manager for temporary test databases""" @staticmethod def create_temp_db_path(): """Create a unique temporary path for test database""" temp_dir = tempfile.mkdtemp(prefix="test_mmt_") return os.path.join(temp_dir, "test_tools.db") @staticmethod def setup_test_environment(db_path): """Configure environment to use test database""" os.environ["DB_PATH"] = db_path os.environ["ADMIN_EMAIL"] = "test.admin@test.com" os.environ["ADMIN_PASSWORD"] = "TestAdmin123" os.environ["SECRET_KEY"] = "test-secret-key-for-e2e-tests" @staticmethod def inject_test_database(db_path): """Inject test database instance into the application""" # Import here to avoid circular imports from src.core.user_database import Database, set_user_db # Create test database instance test_db = Database(db_path) # Set it as the active instance set_user_db(test_db) return test_db @staticmethod def cleanup_test_db(db_path): """Clean up temporary database""" if os.path.exists(db_path): # Clean up the complete temporary directory temp_dir = os.path.dirname(db_path) if os.path.exists(temp_dir): shutil.rmtree(temp_dir, ignore_errors=True) @staticmethod def restore_original_environment(original_env): """Restore original environment""" for key, value in original_env.items(): if value is None: # Variable didn't exist before if key in os.environ: del os.environ[key] else: os.environ[key] = value