krz/hutch-stats
Server-side utility for calculating contributions for sourcehut users.
clone: git clone https://gitbay.org/krz/hutch-stats.git
1from __future__ import annotations
2
3from collections.abc import Generator
4
5import pytest
6from fastapi.testclient import TestClient
7from sqlalchemy.orm import Session, sessionmaker
8
9from srht_contrib.config import Settings
10from srht_contrib.db import Base, make_engine
11from srht_contrib.main import create_app
12
13
14@pytest.fixture()
15def settings() -> Settings:
16 return Settings(
17 API_KEY="test-api-key",
18 ENABLE_SCHEDULER=False,
19 SRHT_TOKEN="test-token",
20 DATABASE_URL="sqlite://",
21 TODO_SRHT_ENDPOINT="https://todo.sr.ht/query",
22 GIT_SRHT_ENDPOINT="https://git.sr.ht/query",
23 DEFAULT_ACTOR="~ccleberg",
24 POLL_INTERVAL_SECONDS=3600,
25 GIT_TRACKED_REPOSITORIES=[],
26 )
27
28
29@pytest.fixture()
30def db_engine(settings: Settings):
31 engine = make_engine(settings)
32 Base.metadata.create_all(bind=engine)
33 try:
34 yield engine
35 finally:
36 Base.metadata.drop_all(bind=engine)
37
38
39@pytest.fixture()
40def session_factory(db_engine) -> sessionmaker[Session]:
41 return sessionmaker(bind=db_engine, autoflush=False, autocommit=False, expire_on_commit=False)
42
43
44@pytest.fixture()
45def db_session(session_factory: sessionmaker[Session]) -> Generator[Session, None, None]:
46 session = session_factory()
47 try:
48 yield session
49 finally:
50 session.close()
51
52
53@pytest.fixture()
54def client(settings: Settings, db_engine, session_factory: sessionmaker[Session]) -> Generator[TestClient, None, None]:
55 app = create_app(settings, engine=db_engine, session_factory=session_factory)
56 with TestClient(app) as test_client:
57 test_client.headers.update({"X-API-Key": settings.api_key})
58 yield test_client