From 78735594cf24830aa0b9b31661920e59f6de8ab6 Mon Sep 17 00:00:00 2001 From: agatha Date: Sat, 14 Mar 2026 14:03:10 -0400 Subject: [PATCH] test: add integration test for proxy model round-trip --- tests/conftest.py | 43 ++++++++++++++++++++++++++ tests/integration/test_proxy_models.py | 24 ++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 tests/integration/test_proxy_models.py diff --git a/tests/conftest.py b/tests/conftest.py index e69de29..74097ac 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -0,0 +1,43 @@ +import asyncio +from collections.abc import AsyncGenerator + +import pytest +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine + +from proxy_pool.config import Settings +from proxy_pool.db.base import Base + + +@pytest.fixture(scope="session") +def event_loop(): + loop = asyncio.new_event_loop() + yield loop + loop.close() + + +@pytest.fixture(scope="session") +def test_settings() -> Settings: + return Settings( + database_url="postgresql+asyncpg://proxypool:proxypool@localhost:5432/proxypool", + redis_url="redis://localhost:6379/1", + secret_key="test-secret", + log_level="DEBUG", + ) + + +@pytest.fixture(scope="session") +async def engine(test_settings: Settings): + engine = create_async_engine(test_settings.database_url) + yield engine + await engine.dispose() + + +@pytest.fixture +async def db_session(engine) -> AsyncGenerator[AsyncSession, None]: + async with engine.begin() as conn: + await conn.run_sync(Base.metadata.create_all) + + session_factory = async_sessionmaker(engine, expire_on_commit=False) + async with session_factory() as session: + yield session + await session.rollback() diff --git a/tests/integration/test_proxy_models.py b/tests/integration/test_proxy_models.py new file mode 100644 index 0000000..0577337 --- /dev/null +++ b/tests/integration/test_proxy_models.py @@ -0,0 +1,24 @@ +from proxy_pool.proxy.models import Proxy, ProxyProtocol, ProxySource, ProxyStatus + + +async def test_create_source_and_proxy(db_session): + source = ProxySource( + url="https://example.com/proxies.txt", + parser_name="plaintext", + ) + db_session.add(source) + await db_session.flush() + + proxy = Proxy( + ip="203.0.113.42", + port=8080, + protocol=ProxyProtocol.HTTP, + source_id=source.id, + ) + db_session.add(proxy) + await db_session.flush() + + assert proxy.id is not None + assert proxy.status == ProxyStatus.UNCHECKED + assert proxy.score == 0.0 + assert proxy.source_id == source.id