From 47fd1403a4c5fc2cce9d8747b2e3c8c8400e22ce Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 22 Jan 2025 20:48:16 +0200 Subject: [PATCH 1/5] test: Add unit tests for customized message handling in TgBot --- tests/test_cutom_types.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_cutom_types.py 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!" From 17861ed98a46b25f5ffd31e4493e579d0ac30f88 Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 22 Jan 2025 20:48:25 +0200 Subject: [PATCH 2/5] test: Add unit tests for TgBot initialization and message sending --- tests/test_client.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_client.py 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 From 350dc324930a630cc75553d515c755627c54ea0d Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 22 Jan 2025 20:48:31 +0200 Subject: [PATCH 3/5] chore: Add requirements file for test dependencies --- tests/requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tests/requirements.txt 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 From 0d0e6556589184f846e72bead0aea8886a6fa65a Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 22 Jan 2025 20:56:41 +0200 Subject: [PATCH 4/5] test: Add unit tests for message filters in TgBot --- tests/test_filters.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/test_filters.py 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 From 29d3ebaf21a1d850ca0c01fec2d3970f79cae3a9 Mon Sep 17 00:00:00 2001 From: Zaid Date: Wed, 22 Jan 2025 20:59:02 +0200 Subject: [PATCH 5/5] test: Add unit tests for error handling in TgBot message sending --- tests/test_error_handling.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tests/test_error_handling.py 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