Skip to content

Commit

Permalink
Tests in separated files (UNICT-DMI#176)
Browse files Browse the repository at this point in the history
* fix: remove useless duplicated test file

* refactor: tests splitted

* fix: markdown error for user with underscore

* test: adds for nested buttons in help command

* fix(test): sleep for profs 'cause multiple messages

* fix(test): rappresentati button in help command

* style(black): skip replacing of single-quote when formats

* tests: assert response text with real expected text

* tests(aulario): right/left buttons in calendar

* fix: rappr button after UNICT-DMI#173
  • Loading branch information
boozec authored Sep 14, 2021
1 parent 9ea3753 commit f3b2985
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 391 deletions.
2 changes: 1 addition & 1 deletion data/markdown/rappresentanti_informatica.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Pistorio Anna - @annapist25


*Rappresentanti Corso di Laurea Magistrale*
Trupia Andrea - @andreatrups
Trupia Andrea - @andreatrups
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
skip-string-normalization = true
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Module for testing"""
from module.shared import config_map

TIMEOUT = 8
bot_tag = config_map['test']['tag']
22 changes: 16 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Test configuration"""
import asyncio
import warnings

import pytest
from telethon.sync import TelegramClient
from telethon.sessions import StringSession
from telegram.ext import Updater
from telethon.sessions import StringSession
from telethon.sync import TelegramClient

from main import add_handlers
from module.shared import config_map

warnings.filterwarnings("ignore",
message="If 'per_message=False', 'CallbackQueryHandler' will not be tracked for every message.")
warnings.filterwarnings(
"ignore",
message="If 'per_message=False', 'CallbackQueryHandler' will not be tracked for every message.",
)

api_id = config_map['test']['api_id']
api_hash = config_map['test']['api_hash']
Expand Down Expand Up @@ -49,7 +53,11 @@ async def bot():
if test_key in config_map:
config_map[test_key] = config_map['test'][test_key]

updater = Updater(config_map['token'], request_kwargs={'read_timeout': 20, 'connect_timeout': 20}, use_context=True)
updater = Updater(
config_map['token'],
request_kwargs={'read_timeout': 20, 'connect_timeout': 20},
use_context=True,
)
add_handlers(updater.dispatcher)
updater.start_polling()
await asyncio.sleep(2)
Expand All @@ -69,7 +77,9 @@ async def client(bot) -> TelegramClient:
Iterator[TelegramClient]: telegram client that will simulate the user
"""
print("[info] started telegram client")
tg_client = TelegramClient(StringSession(session), api_id, api_hash, sequential_updates=True)
tg_client = TelegramClient(
StringSession(session), api_id, api_hash, sequential_updates=True
)

await tg_client.connect() # Connect to the server
await tg_client.get_me() # Issue a high level command to start receiving message
Expand Down
56 changes: 56 additions & 0 deletions tests/test_aulario.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import calendar
from datetime import datetime

import pytest
from telethon.sync import TelegramClient
from telethon.tl.custom.conversation import Conversation
from telethon.tl.custom.message import Message

from . import TIMEOUT, bot_tag


@pytest.mark.asyncio
async def test_aulario_cmd(client: TelegramClient):
"""Tests the /aulario command
Args:
client (TelegramClient): client used to simulate the user
"""
conv: Conversation
async with client.conversation(bot_tag, timeout=TIMEOUT) as conv:

await conv.send_message("/aulario") # send a command
resp: Message = await conv.get_response()

assert resp.text == 'Seleziona la data della lezione che ti interessa.'

if "⚠️" in resp.text:
return

now = datetime.now()

await resp.click(text=f"{now.day}") # click the button
resp: Message = await conv.get_response()
assert resp.text

await resp.click(data="sm_aulario") # click the button
resp = await conv.get_edit()
assert resp.text

await resp.click(
text="{} ▶️".format(calendar.month_name[((now.month % 12) + 1)])
)
resp = await conv.get_edit()
assert resp.text

await resp.click(
text="{} ▶️".format(calendar.month_name[((now.month % 12) + 2)])
)
resp = await conv.get_edit()
assert resp.text

await resp.click(
text="◀️ {}".format(calendar.month_name[((now.month % 12) + 1)])
)
resp = await conv.get_edit()
assert resp.text
Loading

0 comments on commit f3b2985

Please sign in to comment.