huesoporro/tests/conftest.py

92 lines
2.3 KiB
Python

from pathlib import Path
import pytest
from caribou.migrate import Database as CaribouDatabase
from caribou.migrate import load_migrations
from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory.pytest_plugin import register_fixture
from src.huesoporro.infra.db import Database
from src.huesoporro.models import ChatbotSettings, Quote, User
from src.huesoporro.settings import Settings
from src.huesoporro.svc.backoff_service import BackoffService
from src.huesoporro.svc.is_mod import IsModSvc
@pytest.fixture
def user() -> User:
return User(
user="huesoporro",
external_auth={
"twitch": {"token": "twitch_token"},
"discord": {"token": "discord_token"},
},
)
@pytest.fixture
def s(tmp_path: Path, user: User) -> Settings:
return Settings(
static_files_path=tmp_path / "static_files",
db_filepath=tmp_path / "huesoporro.db",
twitch_client_id="test_client_id",
twitch_client_secret="test_client_secret", # type: ignore[arg-type] # noqa: S106
jwt_secret="test_jwt_secret", # type: ignore[arg-type] # noqa: S106
allowed_users=[user.user],
)
@pytest.fixture
def db(s) -> Database:
cdb = CaribouDatabase(
db_url=s.db_filepath,
)
cdb.initialize_version_control()
migrations = load_migrations(Path(__file__).parents[1] / "migrations")
cdb.upgrade(migrations)
return Database(s=s)
@pytest.fixture
def is_mod_svc(db) -> IsModSvc:
return IsModSvc(db=db)
@pytest.fixture
async def chatbot_settings(db: Database, user) -> ChatbotSettings:
cbs = ChatbotSettings(mods=[user.user, "allowed_user"])
await db.save_chatbot_settings(user=user, chatbot_settings=cbs)
return cbs
@pytest.fixture
def backoff_callable():
def foo():
return "foo"
return foo
@pytest.fixture
def async_backoff_callable():
async def async_foo():
return "async foo"
return async_foo
@pytest.fixture
async def backoff_svc(backoff_callable, async_backoff_callable):
backoff_svc = BackoffService()
backoff_svc.add_callable(backoff_callable, 3)
backoff_svc.add_callable(async_backoff_callable, 3)
return backoff_svc
@register_fixture()
class QuoteFactory(ModelFactory[Quote]): ...
@pytest.fixture
def quote(quote_factory):
return quote_factory.build()