-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from iamgiolaga/test
feat: added test_behaviours
- Loading branch information
Showing
10 changed files
with
162 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
from telegram.utils.helpers import escape_markdown | ||
from db.queries import update_bot_last_message_id_on_db | ||
|
||
def print_new_summary(chat_id, current_situation, update: Update, context: CallbackContext): | ||
markdown_error = False | ||
def print_new_summary(current_situation, update: Update, context: CallbackContext): | ||
error_message = ( | ||
"Sembra che tu abbia inserito nella descrizione un carattere speciale di telegram (`, *, _).\n" | ||
"Per favore cambiala con /setdescription <descrizione> assicurandoti di non inserire uno di questi caratteri.\n" | ||
"Se la tua intenzione era, invece, di formattare il testo, ricordati di usare anche il carattere di chiusura, come in questo *esempio*." | ||
) | ||
|
||
try: | ||
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', | ||
text=current_situation) | ||
except: | ||
markdown_error = True | ||
error_message = "Sembra che tu abbia inserito nella descrizione un carattere speciale di telegram (`, *, _).\n" \ | ||
"Per favore cambiala con /setdescription <descrizione> assicurandoti di non inserire uno di questi caratteri.\n" \ | ||
"Se la tua intenzione era, invece, di formattare il testo, ricordati di usare anche il carattere di chiusura, come in questo *esempio*." | ||
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', | ||
text=escape_markdown(error_message)) | ||
if not markdown_error: | ||
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=current_situation) | ||
except Exception: | ||
msg = context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=escape_markdown(error_message)) | ||
else: | ||
try: | ||
context.bot.pin_chat_message(chat_id=update.effective_chat.id, message_id=msg.message_id) | ||
except: | ||
except Exception: | ||
print("No admin rights to pin the message") | ||
update_bot_last_message_id_on_db(chat_id, msg.message_id) | ||
return msg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from behaviours.edit_summary import edit_summary | ||
from behaviours.print_new_summary import print_new_summary | ||
from telegram.utils.helpers import escape_markdown | ||
|
||
class TestBehaviours(unittest.TestCase): | ||
|
||
@patch('behaviours.edit_summary.CallbackContext') | ||
def test_edit_summary(self, mock_context_class): | ||
mock_context = MagicMock() | ||
mock_context_class.return_value = mock_context | ||
current_situation = "current_situation" | ||
last_message_id = 123 | ||
update = MagicMock() | ||
context = MagicMock() | ||
|
||
edit_summary(current_situation, last_message_id, update, context) | ||
|
||
context.bot.edit_message_text.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
message_id=last_message_id, | ||
parse_mode='markdown', | ||
text=current_situation | ||
) | ||
context.bot.pin_chat_message.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
message_id=last_message_id | ||
) | ||
|
||
def test_print_new_summary(self): | ||
current_situation = "Some message" | ||
|
||
# Test case 1: no markdown error | ||
with self.subTest("No markdown error"): | ||
update = MagicMock() | ||
context = MagicMock() | ||
msg = print_new_summary(current_situation, update, context) | ||
context.bot.send_message.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
parse_mode='markdown', | ||
text="Some message" | ||
) | ||
context.bot.pin_chat_message.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
message_id=msg.message_id | ||
) | ||
self.assertEqual(msg, context.bot.send_message.return_value) | ||
|
||
# Test case 2: markdown error | ||
with self.subTest("Markdown error"): | ||
update = MagicMock() | ||
context = MagicMock() | ||
mock_response = MagicMock() | ||
context.bot.send_message.side_effect = [Exception("Some error"), mock_response] | ||
msg = print_new_summary(current_situation, update, context) | ||
|
||
error_message = "Sembra che tu abbia inserito nella descrizione un carattere speciale di telegram (`, *, _).\n" \ | ||
"Per favore cambiala con /setdescription <descrizione> assicurandoti di non inserire uno di questi caratteri.\n" \ | ||
"Se la tua intenzione era, invece, di formattare il testo, ricordati di usare anche il carattere di chiusura, come in questo *esempio*." | ||
|
||
context.bot.send_message.assert_called_with( | ||
chat_id=update.effective_chat.id, | ||
parse_mode='markdown', | ||
text=escape_markdown(error_message) | ||
) | ||
context.bot.pin_chat_message.assert_not_called() | ||
self.assertEqual(msg, mock_response) | ||
|
||
# Test case 3: error while pinning the message | ||
with self.subTest("Error while pinning the message"): | ||
update = MagicMock() | ||
context = MagicMock() | ||
context.bot.pin_chat_message.side_effect = Exception("Some error") | ||
msg = print_new_summary(current_situation, update, context) | ||
context.bot.send_message.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
parse_mode='markdown', | ||
text="Some message" | ||
) | ||
context.bot.pin_chat_message.assert_called_once_with( | ||
chat_id=update.effective_chat.id, | ||
message_id=msg.message_id | ||
) | ||
self.assertEqual(msg, context.bot.send_message.return_value) | ||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from callbacks.help_func import help_func | ||
|
||
class TestCallbacks(unittest.TestCase): | ||
|
||
@patch('callbacks.help_func.CallbackContext') | ||
def test_help_func(self, mock_context_class): | ||
mock_update = MagicMock() | ||
mock_context = MagicMock() | ||
mock_context_class.return_value = mock_context | ||
|
||
help_func(mock_update, mock_context) | ||
|
||
mock_context.bot.send_message.assert_called_once_with( | ||
chat_id=mock_update.effective_chat.id, | ||
parse_mode='markdown', | ||
text="Ecco a te la lista completa dei comandi di questo bot: \n" | ||
"- se sei in forse scrivi _proponimi_, \n" | ||
"- se vuoi proporre qualcuno scrivi _proponi <nome>_, \n" | ||
"- per essere aggiunto o confermato rispondi _aggiungimi_,\n" | ||
"- per aggiungere o confermare qualcuno usa _aggiungi <nome>_,\n" | ||
"- per essere rimosso _toglimi_, \n" | ||
"- per rimuovere qualcuno _togli <nome>_, \n" | ||
"- per modificare le squadre scrivi _scambia <nome 1> con <nome 2>_. \n\n" | ||
"Posso anche pinnare i messaggi se vuoi " | ||
"ma per farlo ricordati di aggiungermi come amministratore.\n" | ||
"\n" | ||
"/start - Crea una nuova partita \n" | ||
"/setnumber - Imposta il numero di partecipanti \n" | ||
"/setday - Imposta il giorno della partita \n" | ||
"/settime - Imposta l’orario della partita \n" | ||
"/setdescription - Imposta la descrizione sotto i partecipanti \n" | ||
"/setpitch - Imposta il campo \n" | ||
"/participants - Mostra i partecipanti della partita attuale \n" | ||
"/teams - Mostra le squadre della partita attuale \n" | ||
"/stop - Rimuovi la partita \n" | ||
"/help - Mostra la lista di comandi disponibili" | ||
) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |