huesoporro/tests/test_svc.py

96 lines
3.2 KiB
Python

import asyncio
import time
import pytest
from huesoporro.models import ChatbotSettings, User
from huesoporro.svc.is_mod import IsModSvc
async def test_is_mod_svc_returns_true_for_channel(is_mod_svc: IsModSvc, user: User):
is_mod = await is_mod_svc.run(user=user, username="TestUser", channel="TestUser")
assert is_mod
async def test_is_mod_svc_returns_true_for_user_in_modlist(
is_mod_svc: IsModSvc,
user: User,
chatbot_settings: ChatbotSettings,
):
is_mod = await is_mod_svc.run(
user=user, username=chatbot_settings.mods[1], channel=user.user
)
assert is_mod
async def test_is_mod_svc_returns_false_for_settingless_user(
is_mod_svc: IsModSvc, user: User
):
is_mod = await is_mod_svc.run(user=user, username="TestUser", channel="TestUser2")
assert not is_mod
@pytest.mark.usefixtures("chatbot_settings")
async def test_is_mod_svc_returns_false_for_user_not_in_modlist(
is_mod_svc: IsModSvc, user: User
):
is_mod = await is_mod_svc.run(user=user, username="TestUser2", channel=user.user)
assert not is_mod
async def test_backoff_svc_returns_for_first_attempt(
backoff_svc, backoff_callable, async_backoff_callable
):
assert backoff_svc.call(backoff_callable) == "foo"
assert await backoff_svc.call_async(async_backoff_callable) == "async foo"
async def test_backoff_svc_returns_none_for_second_attempt(
backoff_svc, backoff_callable, async_backoff_callable
):
assert backoff_svc.call(backoff_callable) == "foo"
assert backoff_svc.call(backoff_callable) is None
assert await backoff_svc.call_async(async_backoff_callable) == "async foo"
assert await backoff_svc.call_async(async_backoff_callable) is None
async def test_backoff_svc_returns_for_second_attempt_after_delay(
backoff_svc, backoff_callable, async_backoff_callable
):
assert backoff_svc.call(backoff_callable) == "foo"
assert backoff_svc.call(backoff_callable) is None
time.sleep(3)
assert backoff_svc.call(backoff_callable) == "foo"
assert await backoff_svc.call_async(async_backoff_callable) == "async foo"
assert await backoff_svc.call_async(async_backoff_callable) is None
await asyncio.sleep(3)
assert await backoff_svc.call_async(async_backoff_callable) == "async foo"
async def test_backoff_svc_raises_value_error_for_unknown_callable(backoff_svc):
with pytest.raises(ValueError, match="not registered with backoff service"):
backoff_svc.call(lambda: "foo")
async def test_backoff_svc_raises_value_error_for_unknown_async_callable(backoff_svc):
with pytest.raises(ValueError, match="not registered with backoff service"):
await backoff_svc.call_async(lambda: "foo")
async def test_backoff_svc_raises_value_error_for_async_called_from_sync(
backoff_svc, backoff_callable
):
with pytest.raises(
ValueError, match="Cannot call sync function with .call_async()"
):
await backoff_svc.call_async(backoff_callable)
async def test_backoff_svc_raises_value_error_for_sync_called_from_async(
backoff_svc, async_backoff_callable
):
with pytest.raises(ValueError, match="Cannot call async function with .call()"):
backoff_svc.call(async_backoff_callable)