diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..0bcf21c --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,2 @@ +pytest +pytest-asyncio \ No newline at end of file diff --git a/tests/test_client.py b/tests/test_client.py new file mode 100644 index 0000000..4ea947f --- /dev/null +++ b/tests/test_client.py @@ -0,0 +1,28 @@ +import pytest +import os +from tgram import TgBot +from tgram.types import Message + +BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN") +CHAT_ID = 5117901887 +ALLOWED_UPDATES = ["message"] +PARSE_MODE = "HTML" + + +@pytest.fixture +def bot(): + return TgBot(BOT_TOKEN, allowed_updates=ALLOWED_UPDATES, parse_mode=PARSE_MODE) + + +@pytest.mark.asyncio +async def test_bot_initialization(bot): + assert bot.bot_token == BOT_TOKEN + assert bot.allowed_updates == ALLOWED_UPDATES + assert bot.parse_mode == PARSE_MODE + + +@pytest.mark.asyncio +async def test_bot_send_message(bot): + message = Message(text="Hello, world!") + response = await bot.send_message(chat_id=CHAT_ID, text=message.text) + assert response.text == message.text diff --git a/tests/test_cutom_types.py b/tests/test_cutom_types.py new file mode 100644 index 0000000..fabc724 --- /dev/null +++ b/tests/test_cutom_types.py @@ -0,0 +1,28 @@ +import pytest +import os +from tgram import TgBot +from tgram.types import Message + +BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN") +CHAT_ID = 5117901887 + + +class CustomizedMessage(Message): + @property + def check(self): + return "Check Passed!" + + +@pytest.fixture +def bot(): + bot = TgBot(BOT_TOKEN) + bot.customize(Message, CustomizedMessage) + + return bot + + +@pytest.mark.asyncio +async def test_customized_message(bot): + response = await bot.send_message(chat_id=CHAT_ID, text="Cheaking..") + assert response.__class__.__name__ == "CustomizedMessage" + assert response.check == "Check Passed!" diff --git a/tests/test_error_handling.py b/tests/test_error_handling.py new file mode 100644 index 0000000..8f1ca6f --- /dev/null +++ b/tests/test_error_handling.py @@ -0,0 +1,21 @@ +import pytest +import os +from tgram import TgBot + +BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN") +CHAT_ID = 5117901887 + + +@pytest.fixture +def bot(): + bot = TgBot(BOT_TOKEN) + + return bot + + +@pytest.mark.asyncio +async def test_customized_message(bot): + try: + await bot.send_message(chat_id=CHAT_ID, text="") + except Exception as e: + assert e.__class__.__name__ == "MessageTextEmpty" \ No newline at end of file diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 0000000..6bcd0a6 --- /dev/null +++ b/tests/test_filters.py @@ -0,0 +1,25 @@ +import pytest +import os +from tgram import TgBot, filters + +BOT_TOKEN = os.environ.get("BOT_TOKEN", "YOUR_BOT_TOKEN") +CHAT_ID = 5117901887 + + +@pytest.fixture +def bot(): + bot = TgBot(BOT_TOKEN) + + return bot + + +@pytest.mark.asyncio +async def test_customized_message(bot): + response = await bot.send_message(chat_id=CHAT_ID, text="Cheaking..") + assert await filters.text(bot, response) is True + + ## Trying again with media message and text filter.. + response = await bot.send_photo( + chat_id=CHAT_ID, photo="https://avatars.githubusercontent.com/u/162994967?v=4" + ) + assert await filters.text(bot, response) is False