Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests directory. #54

Merged
merged 5 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
pytest-asyncio
28 changes: 28 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions tests/test_cutom_types.py
Original file line number Diff line number Diff line change
@@ -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!"
21 changes: 21 additions & 0 deletions tests/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 25 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -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
Loading