50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from cryptography.fernet import Fernet
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.config import Settings
|
|
from app.db.models import Base
|
|
from app.db.repositories import ActivityRepository, UserRepository
|
|
from app.main import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def db_session() -> Session:
|
|
engine = create_engine(
|
|
"sqlite://",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
Base.metadata.create_all(engine)
|
|
factory = sessionmaker(bind=engine, expire_on_commit=False)
|
|
with factory() as session:
|
|
yield session
|
|
|
|
|
|
@pytest.fixture
|
|
def user_repository(db_session: Session) -> UserRepository:
|
|
return UserRepository(db_session)
|
|
|
|
|
|
@pytest.fixture
|
|
def activity_repository(db_session: Session) -> ActivityRepository:
|
|
return ActivityRepository(db_session)
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path: Path) -> TestClient:
|
|
settings = Settings(
|
|
ADMIN_PASSWORD="admin-secret",
|
|
SECRET_KEY="0123456789abcdef0123456789abcdef",
|
|
CREDENTIAL_ENCRYPTION_KEY=Fernet.generate_key().decode("ascii"),
|
|
DATA_DIR=str(tmp_path),
|
|
DATABASE_URL=f"sqlite:///{tmp_path / 'app.db'}",
|
|
SYNC_INTERVAL_MINUTES=5,
|
|
)
|
|
return TestClient(create_app(settings))
|