Buckets:
AnonymousSpottedToad/human_verified_bucket / codebase /python /Extralit_extralit /extralit-server /tests /conftest.py
| import asyncio | |
| from collections.abc import AsyncGenerator, Generator | |
| from typing import TYPE_CHECKING | |
| import httpx | |
| import pytest | |
| import pytest_asyncio | |
| from rq import Queue | |
| from sqlalchemy import NullPool, create_engine | |
| from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine | |
| from extralit_server.cli.database.migrate import migrate_db | |
| from extralit_server.database import database_url_sync | |
| from extralit_server.jobs.queues import REDIS_CONNECTION | |
| from extralit_server.settings import settings | |
| from tests.database import SyncTestSession, TestSession, set_task | |
| if TYPE_CHECKING: | |
| from sqlalchemy.engine import Connection | |
| from sqlalchemy.ext.asyncio import AsyncConnection | |
| def _protect_event_loop(): | |
| """Save and restore the event loop around each test. | |
| CLI commands use asyncio.run() which calls set_event_loop(None), | |
| breaking subsequent tests. This fixture captures the loop before | |
| the test and restores it after if it was replaced or unset.""" | |
| policy = asyncio.get_event_loop_policy() | |
| try: | |
| loop_before = policy.get_event_loop() | |
| if loop_before.is_closed(): | |
| loop_before = None | |
| except RuntimeError: | |
| loop_before = None | |
| yield | |
| if loop_before is not None and not loop_before.is_closed(): | |
| try: | |
| current = policy.get_event_loop() | |
| if current is not loop_before or current.is_closed(): | |
| policy.set_event_loop(loop_before) | |
| except RuntimeError: | |
| policy.set_event_loop(loop_before) | |
| def mock_httpx_client(mocker) -> Generator[httpx.Client, None, None]: | |
| return mocker.Mock(httpx.Client) | |
| async def connection() -> AsyncGenerator["AsyncConnection", None]: | |
| set_task(asyncio.current_task()) | |
| database_url = settings.database_url | |
| engine = create_async_engine(database_url, poolclass=NullPool) | |
| conn = await engine.connect() | |
| TestSession.configure(bind=conn) | |
| migrate_db("head") | |
| yield conn | |
| migrate_db("base") | |
| await conn.close() | |
| await engine.dispose() | |
| async def db(connection: "AsyncConnection") -> AsyncGenerator["AsyncSession", None]: | |
| await connection.begin_nested() | |
| session = TestSession() | |
| yield session | |
| await session.close() | |
| await TestSession.remove() | |
| await connection.rollback() | |
| def sync_connection() -> Generator["Connection", None, None]: | |
| engine = create_engine(database_url_sync()) | |
| conn = engine.connect() | |
| SyncTestSession.configure(bind=conn) | |
| migrate_db("head") | |
| yield conn | |
| migrate_db("base") | |
| conn.close() | |
| engine.dispose() | |
| def sync_db(sync_connection: "Connection") -> Generator["Session", None, None]: | |
| sync_connection.begin_nested() | |
| session = SyncTestSession() | |
| yield session | |
| session.close() | |
| SyncTestSession.remove() | |
| sync_connection.rollback() | |
| def empty_job_queues(): | |
| queues = Queue.all(connection=REDIS_CONNECTION) | |
| for queue in queues: | |
| queue.empty() | |
| yield | |
| def async_db_proxy(mocker: "MockerFixture", sync_db: "Session") -> "AsyncSession": | |
| """Create a mocked `AsyncSession` that proxies to the sync session. This will allow us to execute the async CLI commands | |
| and then in the unit test function use the sync session to assert the changes. | |
| Args: | |
| mocker: pytest-mock fixture. | |
| sync_db: Sync session. | |
| Returns: | |
| Mocked `AsyncSession` that proxies to the sync session. | |
| """ | |
| async_session = AsyncSession() | |
| async_session.sync_session = sync_db | |
| async_session._proxied = sync_db | |
| async_session.close = mocker.AsyncMock() | |
| return async_session | |
Xet Storage Details
- Size:
- 3.88 kB
- Xet hash:
- 2c97d71ff67f15b442a49c51d2637a3946bef61c7961efd79dd51d920cca86f1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.