Skip to content

Commit

Permalink
Merge pull request #51 from iamgiolaga/test
Browse files Browse the repository at this point in the history
feat: added test_behaviours
  • Loading branch information
iamgiolaga authored Nov 1, 2024
2 parents 1d7e98f + 9d9a27c commit edab7d1
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 28 deletions.
28 changes: 13 additions & 15 deletions behaviours/print_new_summary.py
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
5 changes: 3 additions & 2 deletions callbacks/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from behaviours.remove_job_if_exists import remove_job_if_exists
from behaviours.trigger_payment_reminder import trigger_payment_reminder
from db.queries import find_row_by_chat_id, find_all_info_by_chat_id, update_players_on_db, update_teams_on_db, \
is_already_present
is_already_present, update_bot_last_message_id_on_db
from utils.utils import flatten_args, get_sender_name, exclude_maybe, swap_players, \
format_summary
from utils.constants import maybe_placeholder
Expand Down Expand Up @@ -176,7 +176,8 @@ def echo(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)

Expand Down
5 changes: 3 additions & 2 deletions callbacks/participants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from telegram import Update
from telegram.ext import CallbackContext
from behaviours.print_new_summary import print_new_summary
from db.queries import find_row_by_chat_id, find_all_info_by_chat_id
from db.queries import find_row_by_chat_id, find_all_info_by_chat_id, update_bot_last_message_id_on_db
from utils.utils import format_summary

def participants(update: Update, context: CallbackContext):
Expand All @@ -15,4 +15,5 @@ def participants(update: Update, context: CallbackContext):
players, day, time, target, default_message, pitch, teams, bot_last_message_id = find_all_info_by_chat_id(
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
3 changes: 2 additions & 1 deletion callbacks/set_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def set_day(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)
5 changes: 3 additions & 2 deletions callbacks/set_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from telegram.utils.helpers import escape_markdown
from behaviours.edit_summary import edit_summary
from behaviours.print_new_summary import print_new_summary
from db.queries import find_row_by_chat_id, update_description_on_db, find_all_info_by_chat_id
from db.queries import find_row_by_chat_id, update_description_on_db, find_all_info_by_chat_id, update_bot_last_message_id_on_db
from utils.utils import flatten_args, get_sender_name, format_summary

def set_description(update: Update, context: CallbackContext):
Expand All @@ -27,6 +27,7 @@ def set_description(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)
5 changes: 3 additions & 2 deletions callbacks/set_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from behaviours.print_new_summary import print_new_summary
from behaviours.remove_job_if_exists import remove_job_if_exists
from behaviours.trigger_payment_reminder import trigger_payment_reminder
from db.queries import find_all_info_by_chat_id, update_teams_on_db, update_target_on_db
from db.queries import find_all_info_by_chat_id, update_teams_on_db, update_target_on_db, update_bot_last_message_id_on_db
from utils.utils import get_sender_name, exclude_maybe, format_summary

def set_number(update: Update, context: CallbackContext):
Expand Down Expand Up @@ -50,7 +50,8 @@ def set_number(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)

Expand Down
5 changes: 3 additions & 2 deletions callbacks/set_pitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from telegram.utils.helpers import escape_markdown
from behaviours.edit_summary import edit_summary
from behaviours.print_new_summary import print_new_summary
from db.queries import find_row_by_chat_id, update_pitch_on_db, find_all_info_by_chat_id
from db.queries import find_row_by_chat_id, update_pitch_on_db, find_all_info_by_chat_id, update_bot_last_message_id_on_db
from utils.utils import flatten_args, get_sender_name, format_summary

def set_pitch(update: Update, context: CallbackContext):
Expand All @@ -27,6 +27,7 @@ def set_pitch(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)
5 changes: 3 additions & 2 deletions callbacks/set_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from behaviours.print_new_summary import print_new_summary
from behaviours.remove_job_if_exists import remove_job_if_exists
from behaviours.trigger_payment_reminder import trigger_payment_reminder
from db.queries import find_row_by_chat_id, find_all_info_by_chat_id, update_time_on_db
from db.queries import find_row_by_chat_id, find_all_info_by_chat_id, update_time_on_db, update_bot_last_message_id_on_db
from utils.utils import flatten_args, get_sender_name, format_summary

def set_time(update: Update, context: CallbackContext):
Expand All @@ -32,6 +32,7 @@ def set_time(update: Update, context: CallbackContext):
chat_id)
current_situation = format_summary(players, day, time, target, default_message, pitch)
if bot_last_message_id is None:
print_new_summary(chat_id, current_situation, update, context)
msg = print_new_summary(current_situation, update, context)
update_bot_last_message_id_on_db(chat_id, msg.message_id)
else:
edit_summary(current_situation, bot_last_message_id, update, context)
87 changes: 87 additions & 0 deletions test/test_behaviours.py
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()
42 changes: 42 additions & 0 deletions test/test_callbacks.py
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()

0 comments on commit edab7d1

Please sign in to comment.