Download apps/api/tests/conftest.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 6.47 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/tests/conftest.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/tests/conftest.py
6.47 kB
| """Test fixtures. | |
| Runs against SQLite so the engine can be exercised without Postgres, Redis or | |
| any model provider. The models carry dialect variants for exactly this reason. | |
| """ | |
| from __future__ import annotations | |
| import os | |
| # Before anything imports the application. | |
| # | |
| # The suite said it needed no model provider and did not enforce it. What made | |
| # that true locally was `.env`, which pydantic-settings reads and which holds | |
| # real keys - so the tests passed here and 94 of them failed on the first | |
| # machine without one: | |
| # | |
| # Value error, API key required for openrouter. | |
| # Set OPENROUTER_API_KEY environment variable | |
| # | |
| # CrewAI validates the key when the `LLM` object is built, not when it is | |
| # called, so constructing an agent needs one even though every test mocks the | |
| # call. | |
| # | |
| # Placeholders, and unconditionally rather than only when absent. A suite that | |
| # inherits a working key is a suite that can reach a provider and spend money | |
| # the moment a mock is wrong somewhere - and that failure would look like a | |
| # slow test rather than like a charge. | |
| for _provider in ( | |
| "OPENROUTER_API_KEY", | |
| "OPENAI_API_KEY", | |
| "MISTRAL_API_KEY", | |
| "SERPER_API_KEYS", | |
| "QDRANT_API_KEY", | |
| "SUPABASE_SERVICE_ROLE_KEY", | |
| ): | |
| os.environ[_provider] = "test-not-a-real-key" | |
| # ruff: noqa: E402 - everything below has to be imported after the block above. | |
| # pydantic-settings reads the environment once, when the settings class is | |
| # first constructed, and that happens on the first `from app...` here. | |
| import uuid | |
| from collections.abc import AsyncGenerator | |
| import pytest | |
| import pytest_asyncio | |
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | |
| from app.db.models import Base, Project, User, UserRole, UserStatus | |
| def _crew_locks_off_redis(): | |
| """Do at session start what the app does at startup. | |
| CrewAI takes a lock inside `Crew(...)`, and picks Redis for it whenever | |
| `REDIS_URL` is in the environment - which this project's .env sets, for the | |
| queue. `main.py` calls `crew_locks.install()` before the first crew is | |
| built; the tests never did, so they only passed while nothing had loaded | |
| dotenv before CrewAI. Import order decided it: adding one test that | |
| imported the content services flipped the order and eleven unrelated dry | |
| tests started failing on a Redis that was never meant to be running. | |
| Session-scoped and autouse, because the backend is process-wide and the | |
| failure appears in whichever test happens to build a crew first. | |
| """ | |
| from app.core import crew_locks | |
| crew_locks.install() | |
| async def engine(): | |
| eng = create_async_engine("sqlite+aiosqlite:///:memory:", future=True) | |
| async with eng.begin() as conn: | |
| await conn.run_sync(Base.metadata.create_all) | |
| yield eng | |
| await eng.dispose() | |
| async def db(engine) -> AsyncGenerator[AsyncSession, None]: | |
| factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) | |
| async with factory() as session: | |
| yield session | |
| async def user(db: AsyncSession) -> User: | |
| user_account = User( | |
| id=uuid.uuid4(), | |
| email="tester@contiai.local", | |
| full_name="Tester", | |
| role=UserRole.user, | |
| status=UserStatus.active, | |
| ) | |
| db.add(user_account) | |
| await db.commit() | |
| return user_account | |
| async def project(db: AsyncSession, user: User) -> Project: | |
| p = Project(owner_id=user.id, kind="demo", title="A test project") | |
| db.add(p) | |
| await db.commit() | |
| return p | |
| def _fresh_queue_pool(): | |
| """Drop the queue's connection pool between tests. | |
| `app.core.queue` caches the pool in a module global, which is right for a | |
| server - one process, one event loop, one pool for its lifetime - and wrong | |
| across tests, because each gets its own loop. The second test to use the | |
| queue inherits a pool whose sockets belong to a loop that has been closed, | |
| and redis tries to shut them down on it: | |
| RuntimeError: Event loop is closed | |
| Invisible until `REDIS_URL` points at a reachable server. With none, the | |
| pool is never built and there is nothing to leak - which is why this only | |
| appeared when the deployment's queue was configured locally. | |
| """ | |
| from app.core import queue | |
| queue._pool = None | |
| queue._pool_failed = False | |
| yield | |
| queue._pool = None | |
| queue._pool_failed = False | |
| def _clean_registry(): | |
| """Remove pipelines a test registered, keep the real ones. | |
| Registration happens as an import side effect, and Python only imports a | |
| module once - so the registry cannot simply be emptied and reloaded. The | |
| real pipelines are loaded up front and the snapshot is taken after that; | |
| teardown then drops only what the test added. | |
| """ | |
| from app.pipelines import registry | |
| registry.load_all() | |
| saved = dict(registry._REGISTRY) | |
| yield | |
| registry._REGISTRY.clear() | |
| registry._REGISTRY.update(saved) | |
| def _no_length_fitter_calls(request, monkeypatch): | |
| """The length fitter calls a model directly, not through a Crew, so the | |
| dry suites' stubbed `kickoff_async` does not reach it. Here it answers | |
| «no rewrite» - the draft goes to the validator as written, which is what | |
| every test written before the fitter expects. A test of the fitter itself | |
| marks itself `real_fitter` and stubs the model it is given.""" | |
| if request.node.get_closest_marker("real_fitter"): | |
| yield | |
| return | |
| from app.agents.arabic_bag import length_fit | |
| async def _no_rewrite(*args, **kwargs): | |
| return None | |
| monkeypatch.setattr(length_fit, "_rewrite", _no_rewrite) | |
| yield | |
| def _uploads_not_read_in_the_background(monkeypatch): | |
| """An upload is read (OCR) as it arrives, in a task of the API process. | |
| In a test that task would outlive its database and, with the placeholder | |
| keys above, try to reach a provider. Tests that read uploads call | |
| `services.sources.library.read_upload` themselves.""" | |
| from app.services.sources import library | |
| scheduled: list = [] | |
| monkeypatch.setattr(library, "schedule", | |
| lambda upload_id, force_ocr=False: scheduled.append((upload_id, force_ocr))) | |
| return scheduled | |