CoolFace
Apppublic

HumeAI/expressive-tts-arena

sourceHugging Facemitupdated 11mo agoView on Hugging Face
68likes
test_db.py97 linesDownload Raw Back to scripts
1"""2test_db.py3 4This script verifies the database connection for the Expressive TTS Arena project.5It attempts to connect to the PostgreSQL database using async SQLAlchemy and executes6a simple query to confirm connectivity.7 8Functionality:9- Loads the database connection from `database.py`.10- Attempts to establish an async connection to the database.11- Executes a test query (`SELECT 1`) to confirm connectivity.12- Prints a success message if the connection is valid.13- Prints an error message if the connection fails.14 15Usage:16    python src/test_db.py17 18Expected Output:19    Database connection successful!  (if the database is reachable)20    Database connection failed: <error message> (if there are connection issues)21 22Troubleshooting:23- Ensure the `.env` file contains a valid `DATABASE_URL`.24- Check that the database server is running and accessible.25- Verify PostgreSQL credentials and network settings.26"""27 28# Standard Library Imports29import asyncio30import sys31 32# Third-Party Library Imports33from sqlalchemy import text34 35# Local Application Imports36from src.common import Config, logger37from src.database import engine, init_db38 39 40async def test_connection_async():41    """42    Asynchronously test the database connection.43 44    This function attempts to connect to the database using the configured45    async engine and execute a simple SELECT query. It logs success or failure46    messages accordingly.47 48    Returns:49        bool: True if the connection was successful, False otherwise.50    """51    if engine is None:52        logger.error("No valid database engine configured.")53        return False54 55    try:56        # Create a new async session57        async with engine.connect() as conn:58            # Execute a simple query to verify connectivity59            result = await conn.execute(text("SELECT 1"))60            # Fetch the result to make sure the query completes61            await result.fetchone()62 63        logger.info("Async database connection successful!")64        return True65 66    except Exception as e:67        logger.error(f"Async database connection failed: {e}")68        return False69 70 71def main():72    """73    Main entry point for the database connection test script.74 75    This function creates the configuration, initializes the database engine,76    and runs the async test function within an event loop. It exits with an77    appropriate system exit code based on the test result.78 79    Returns:80        None81    """82    # Make sure config is loaded first to initialize the engine83    config = Config.get()84 85    # Initialize the database engine86    init_db(config)87 88    # Run the async test function89    success = asyncio.run(test_connection_async())90 91    # Exit with an appropriate status code92    sys.exit(0 if success else 1)93 94 95if __name__ == "__main__":96    main()97