Spaces:
Running on T4
Running on T4
Download backend/tests/conftest.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/conftest.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/conftest.py
3.17 kB
| """Test harness: a real Postgres database (migrated with Alembic), an in-process S3 server (moto) and an HTTP client. | |
| Each parallel test run must use its own database: set TEST_DATABASE_NAME (default app_test). | |
| Postgres comes from TEST_DATABASE_SERVER (default the local docker container on port 55440). | |
| """ | |
| import os | |
| import socket | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| ROOT = Path(__file__).resolve().parents[1] | |
| SERVER = os.environ.get("TEST_DATABASE_SERVER", "postgresql://app:app@localhost:55440") | |
| DB_NAME = os.environ.get("TEST_DATABASE_NAME", "app_test") | |
| def _free_port() -> int: | |
| with socket.socket() as s: | |
| s.bind(("127.0.0.1", 0)) | |
| return s.getsockname()[1] | |
| S3_PORT = _free_port() | |
| os.environ.update({ | |
| "ENV": "test", | |
| "DATABASE_URL": f"{SERVER.replace('postgresql://', 'postgresql+asyncpg://')}/{DB_NAME}", | |
| "JWT_SECRET": "test-secret-test-secret-test-secret", | |
| "STORAGE_ENDPOINT": f"http://127.0.0.1:{S3_PORT}", | |
| "STORAGE_REGION": "us-east-1", | |
| "STORAGE_BUCKET": "test-media", | |
| "STORAGE_KEY": "test", | |
| "STORAGE_SECRET": "test", | |
| "GOOGLE_API_KEY": "", # the AI coach uses its rule-based text in tests | |
| "COACH_CACHE_ENABLED": "false", | |
| }) | |
| import boto3 # noqa: E402 | |
| import httpx # noqa: E402 | |
| from moto.server import ThreadedMotoServer # noqa: E402 | |
| from sqlalchemy import text # noqa: E402 | |
| from app.core.database import get_engine # noqa: E402 | |
| from app.core.rate_limit import reset_all as reset_rate_limits # noqa: E402 | |
| from app.main import create_app # noqa: E402 | |
| from app.models import Base # noqa: E402 | |
| def _create_database() -> None: | |
| import asyncio | |
| import asyncpg | |
| async def run() -> None: | |
| conn = await asyncpg.connect(f"{SERVER}/postgres") | |
| try: | |
| await conn.execute(f'DROP DATABASE IF EXISTS "{DB_NAME}" WITH (FORCE)') | |
| await conn.execute(f'CREATE DATABASE "{DB_NAME}"') | |
| finally: | |
| await conn.close() | |
| asyncio.run(run()) | |
| def database() -> None: | |
| _create_database() | |
| subprocess.run([sys.executable, "-m", "alembic", "upgrade", "head"], cwd=ROOT, check=True, | |
| env=os.environ.copy(), capture_output=True) | |
| def s3_server(): | |
| server = ThreadedMotoServer(ip_address="127.0.0.1", port=S3_PORT) | |
| server.start() | |
| boto3.client("s3", endpoint_url=os.environ["STORAGE_ENDPOINT"], region_name="us-east-1", | |
| aws_access_key_id="test", aws_secret_access_key="test").create_bucket(Bucket="test-media") | |
| yield server | |
| server.stop() | |
| async def clean_tables(): | |
| reset_rate_limits() | |
| yield | |
| tables = ", ".join(t.name for t in Base.metadata.sorted_tables) | |
| async with get_engine().begin() as conn: | |
| await conn.execute(text(f"TRUNCATE {tables} RESTART IDENTITY CASCADE")) | |
| def app(): | |
| return create_app() | |
| async def client(app): | |
| async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://test") as c: | |
| yield c | |