summaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorChristian Cleberg <[email protected]>2026-04-09 19:45:45 -0500
committerChristian Cleberg <[email protected]>2026-04-09 19:45:45 -0500
commitacbff854f2da96bddcaede1385e7fefeba0fb34b (patch)
tree48d4707cd3276d2825370f0793008a6ffcfff936 /tests/conftest.py
downloadhutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.tar.gz
hutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.tar.bz2
hutch-stats-acbff854f2da96bddcaede1385e7fefeba0fb34b.zip
initial commit
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..6de9c8e
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,58 @@
+from __future__ import annotations
+
+from collections.abc import Generator
+
+import pytest
+from fastapi.testclient import TestClient
+from sqlalchemy.orm import Session, sessionmaker
+
+from srht_contrib.config import Settings
+from srht_contrib.db import Base, make_engine
+from srht_contrib.main import create_app
+
+
+def settings() -> Settings:
+ return Settings(
+ API_KEY="test-api-key",
+ ENABLE_SCHEDULER=False,
+ SRHT_TOKEN="test-token",
+ DATABASE_URL="sqlite://",
+ TODO_SRHT_ENDPOINT="https://todo.sr.ht/query",
+ GIT_SRHT_ENDPOINT="https://git.sr.ht/query",
+ DEFAULT_ACTOR="~ccleberg",
+ POLL_INTERVAL_SECONDS=3600,
+ GIT_TRACKED_REPOSITORIES=[],
+ )
+
+
+def db_engine(settings: Settings):
+ engine = make_engine(settings)
+ Base.metadata.create_all(bind=engine)
+ try:
+ yield engine
+ finally:
+ Base.metadata.drop_all(bind=engine)
+
+
+def session_factory(db_engine) -> sessionmaker[Session]:
+ return sessionmaker(bind=db_engine, autoflush=False, autocommit=False, expire_on_commit=False)
+
+
+def db_session(session_factory: sessionmaker[Session]) -> Generator[Session, None, None]:
+ session = session_factory()
+ try:
+ yield session
+ finally:
+ session.close()
+
+
+def client(settings: Settings, db_engine, session_factory: sessionmaker[Session]) -> Generator[TestClient, None, None]:
+ app = create_app(settings, engine=db_engine, session_factory=session_factory)
+ with TestClient(app) as test_client:
+ test_client.headers.update({"X-API-Key": settings.api_key})
+ yield test_client