Fixed current coverage

This commit is contained in:
Alex Root Junior 2021-09-20 23:38:58 +03:00
parent 9866e321a3
commit a5892f63f4
12 changed files with 407 additions and 2 deletions

View file

@ -1,4 +1,6 @@
import io
import os
from tempfile import mkstemp
import aiofiles
import pytest
@ -6,6 +8,7 @@ from aresponses import ResponsesMockServer
from aiogram import Bot
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.client.telegram import TelegramAPIServer
from aiogram.methods import GetFile, GetMe
from aiogram.types import File, PhotoSize
from tests.mocked_bot import MockedBot
@ -128,3 +131,15 @@ class TestBot:
await bot.download(
[PhotoSize(file_id="file id", file_unique_id="file id", width=123, height=123)]
)
async def test_download_local_file(self, bot: MockedBot):
bot.session.api = TelegramAPIServer.from_base("http://localhost:8081", is_local=True)
fd, tmp = mkstemp(prefix="test-", suffix=".txt")
value = b"KABOOM"
try:
with open(fd, "wb") as f:
f.write(value)
content = await bot.download_file(tmp)
assert content.getvalue() == value
finally:
os.unlink(tmp)

View file

@ -0,0 +1,49 @@
from inspect import isclass
import pytest
from aiogram.dispatcher.filters import StateFilter
from aiogram.dispatcher.fsm.state import State, StatesGroup
from aiogram.types import Update
pytestmark = pytest.mark.asyncio
class MyGroup(StatesGroup):
state = State()
class TestStateFilter:
@pytest.mark.parametrize(
"state", [None, State("test"), MyGroup, MyGroup(), "state", ["state"]]
)
def test_validator(self, state):
f = StateFilter(state=state)
assert isinstance(f.state, list)
value = f.state[0]
assert (
isinstance(value, (State, str, MyGroup))
or (isclass(value) and issubclass(value, StatesGroup))
or value is None
)
@pytest.mark.parametrize(
"state,current_state,result",
[
[State("state"), "@:state", True],
[[State("state")], "@:state", True],
[MyGroup, "MyGroup:state", True],
[[MyGroup], "MyGroup:state", True],
[MyGroup(), "MyGroup:state", True],
[[MyGroup()], "MyGroup:state", True],
["*", "state", True],
[None, None, True],
[[None], None, True],
[None, "state", False],
[[], "state", False],
],
)
@pytestmark
async def test_filter(self, state, current_state, result):
f = StateFilter(state=state)
assert bool(await f(obj=Update(update_id=42), raw_state=current_state)) is result

View file

@ -0,0 +1,14 @@
import pytest
from aiogram.dispatcher.middlewares.user_context import UserContextMiddleware
async def next_handler(*args, **kwargs):
pass
class TestUserContextMiddleware:
@pytest.mark.asyncio
async def test_unexpected_event_type(self):
with pytest.raises(RuntimeError):
await UserContextMiddleware()(next_handler, object(), {})