mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Resolves #302. We decided to use sentinel pattern (https://python-patterns.guide/python/sentinel-object/) as a solution, but got a few problems with plain `object()`, so instead we use unittest.mock.sentinel and we hope it won't cause side effects. Most of work done via tg-codegen (https://github.com/aiogram/tg-codegen/pull/1), so it's good to review only implementation of sentinel, processing sentinel in `prepare_parse_mode`, changes in base method model and little test fixes.
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
from typing import Dict, Optional
|
|
|
|
import pytest
|
|
|
|
from aiogram import Bot
|
|
from aiogram.api.methods.base import prepare_parse_mode
|
|
|
|
|
|
class TestPrepareFile:
|
|
# TODO: Add tests
|
|
pass
|
|
|
|
|
|
class TestPrepareInputMedia:
|
|
# TODO: Add tests
|
|
pass
|
|
|
|
|
|
class TestPrepareMediaFile:
|
|
# TODO: Add tests
|
|
pass
|
|
|
|
|
|
class TestPrepareParseMode:
|
|
@pytest.mark.parametrize(
|
|
"parse_mode,data,result",
|
|
[
|
|
[None, {}, None],
|
|
["HTML", {}, "HTML"],
|
|
["Markdown", {}, "Markdown"],
|
|
[None, {"parse_mode": "HTML"}, "HTML"],
|
|
["HTML", {"parse_mode": "HTML"}, "HTML"],
|
|
["Markdown", {"parse_mode": "HTML"}, "HTML"],
|
|
],
|
|
)
|
|
@pytest.mark.asyncio
|
|
async def test_default_parse_mode(
|
|
self, parse_mode: str, data: Dict[str, str], result: Optional[str]
|
|
):
|
|
async with Bot(token="42:TEST", parse_mode=parse_mode).context() as bot:
|
|
assert bot.parse_mode == parse_mode
|
|
prepare_parse_mode(data)
|
|
assert data.get("parse_mode") == result
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list(self):
|
|
data = [{}] * 2
|
|
data.append({"parse_mode": "HTML"})
|
|
async with Bot(token="42:TEST", parse_mode="Markdown").context():
|
|
prepare_parse_mode(data)
|
|
|
|
assert isinstance(data, list)
|
|
assert len(data) == 3
|
|
assert all("parse_mode" in item for item in data)
|
|
assert data[0]["parse_mode"] == "Markdown"
|
|
assert data[1]["parse_mode"] == "Markdown"
|
|
assert data[2]["parse_mode"] == "HTML"
|
|
|
|
def test_bot_not_in_context(self):
|
|
data = {}
|
|
prepare_parse_mode(data)
|
|
assert data["parse_mode"] is None
|