import pytest
import asyncio
import aiomysql
from config import config

@pytest.mark.asyncio
@pytest.mark.skip(reason="Requires MySQL database connection - integration test")
async def test_async_database_connection():
    """Test that async database connection works"""
    pool = await aiomysql.create_pool(
        host=config.DB_HOST,
        port=config.DB_PORT,
        user=config.DB_USER,
        password=config.DB_PASSWORD,
        db=config.DB_NAME,
        minsize=1,
        maxsize=5,
    )

    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute("SELECT 1 as test")
            result = await cursor.fetchone()
            assert result[0] == 1

    pool.close()
    await pool.wait_closed()

@pytest.mark.asyncio
@pytest.mark.skip(reason="Requires MySQL database connection - integration test")
async def test_async_pool_multiple_connections():
    """Test that connection pool can handle multiple concurrent connections"""
    pool = await aiomysql.create_pool(
        host=config.DB_HOST,
        port=config.DB_PORT,
        user=config.DB_USER,
        password=config.DB_PASSWORD,
        db=config.DB_NAME,
        minsize=2,
        maxsize=10,
    )

    async def query_db(conn_id):
        async with pool.acquire() as conn:
            async with conn.cursor() as cursor:
                await cursor.execute("SELECT %s as id", (conn_id,))
                result = await cursor.fetchone()
                return result[0]

    # Run 20 concurrent queries with pool of 10
    results = await asyncio.gather(*[query_db(i) for i in range(20)])
    assert results == list(range(20))

    pool.close()
    await pool.wait_closed()
