diff --git a/callbacks/echo.py b/callbacks/echo.py index 15f338b..2bbf2e4 100644 --- a/callbacks/echo.py +++ b/callbacks/echo.py @@ -29,7 +29,8 @@ def echo(update: Update, context: CallbackContext): answer = "Prima di iniziare con le danze, avvia una partita, per farlo usa /start" context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) else: - players, day, time, target, custom_message, pitch, teams, bot_last_message_id = find_all_info_by_chat_id(chat_id) + players, day, time, target, _, pitch, teams, bot_last_message_id = find_all_info_by_chat_id(chat_id) + if new_message == 'aggiungimi': sender = "@" + get_sender_name(update) if is_already_present(chat_id, sender): @@ -82,11 +83,13 @@ def echo(update: Update, context: CallbackContext): update_players_on_db(chat_id, sender, "remove") update_players_on_db(chat_id, sender + maybe_placeholder, "add") show_summary = True + revoked_teams = teams is not None else: if not players or len(players) <= target - 1: answer = 'Ok, ' + sender + ', ti propongo' update_players_on_db(chat_id, sender + maybe_placeholder, "add") show_summary = True + revoked_teams = teams is not None else: answer = 'Siete già in ' + str(target) show_summary = False @@ -101,11 +104,13 @@ def echo(update: Update, context: CallbackContext): update_players_on_db(chat_id, to_be_added, "remove") update_players_on_db(chat_id, to_be_added + maybe_placeholder, "add") show_summary = True + revoked_teams = teams is not None else: if not players or len(players) <= target - 1: answer = 'Ok, propongo ' + to_be_added update_players_on_db(chat_id, to_be_added + maybe_placeholder, "add") show_summary = True + revoked_teams = teams is not None else: answer = 'Siete già in ' + str(target) show_summary = False @@ -172,8 +177,7 @@ def echo(update: Update, context: CallbackContext): context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) if show_summary: - players, day, time, target, default_message, pitch, teams, bot_last_message_id = find_all_info_by_chat_id( - chat_id) + 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) if bot_last_message_id is None: msg = print_new_summary(current_situation, update, context) diff --git a/callbacks/participants.py b/callbacks/participants.py index 5f8290d..37d195f 100644 --- a/callbacks/participants.py +++ b/callbacks/participants.py @@ -9,10 +9,14 @@ def participants(update: Update, context: CallbackContext): row = find_row_by_chat_id(chat_id) if row is None: - answer = "Prima di iniziare con le danze, avvia una partita, per farlo usa /start" - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) - else: - players, day, time, target, default_message, pitch, _, _ = find_all_info_by_chat_id(chat_id) - current_situation = format_summary(players, day, time, target, default_message, pitch) - msg = print_new_summary(current_situation, update, context) - update_bot_last_message_id_on_db(chat_id, msg.message_id) + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + return + + players, day, time, target, default_message, pitch, _, _ = find_all_info_by_chat_id(chat_id) + current_situation = format_summary(players, day, time, target, default_message, pitch) + msg = print_new_summary(current_situation, update, context) + update_bot_last_message_id_on_db(chat_id, msg.message_id) diff --git a/callbacks/set_day.py b/callbacks/set_day.py index 36c42c6..57a0213 100644 --- a/callbacks/set_day.py +++ b/callbacks/set_day.py @@ -13,25 +13,38 @@ def set_day(update: Update, context: CallbackContext): row = find_row_by_chat_id(chat_id) if row is None: - answer = "Prima di iniziare con le danze, avvia una partita, per farlo usa /start" - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + return + + if len(context.args) == 0: + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito il giorno: scrivi /setday " + ) + return + + day = flatten_args(context.args) + update_day_on_db(chat_id, day) + remove_job_if_exists(str(chat_id), context) + players, day, time, target, _, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) + trigger_payment_reminder(update, context, day, time) + sender = "@" + get_sender_name(update) + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text=escape_markdown(f"Ok, {sender}! Ho impostato il giorno della partita il {day}") + ) + + players, day, time, target, default_message, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) + current_situation = format_summary(players, day, time, target, default_message, pitch) + + if bot_last_message_id is None: + msg = print_new_summary(current_situation, update, context) + update_bot_last_message_id_on_db(chat_id, msg.message_id) else: - if len(context.args) == 0: - answer = "Non hai inserito il giorno: scrivi /setday " - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) - else: - day = flatten_args(context.args) - update_day_on_db(chat_id, day) - remove_job_if_exists(str(chat_id), context) - players, day, time, target, _, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) - trigger_payment_reminder(update, context, day, time) - sender = "@" + get_sender_name(update) - answer = "Ok, " + sender + "! Ho impostato il giorno della partita il " + day - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=escape_markdown(answer)) - players, day, time, target, default_message, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) - current_situation = format_summary(players, day, time, target, default_message, pitch) - if bot_last_message_id is None: - 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) + edit_summary(current_situation, bot_last_message_id, update, context) \ No newline at end of file diff --git a/callbacks/set_description.py b/callbacks/set_description.py index 3b5e181..8c9e46a 100644 --- a/callbacks/set_description.py +++ b/callbacks/set_description.py @@ -11,22 +11,35 @@ def set_description(update: Update, context: CallbackContext): row = find_row_by_chat_id(chat_id) if row is None: - answer = "Prima di iniziare con le danze, avvia una partita, per farlo usa /start" - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + return + + if len(context.args) == 0: + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito la descrizione: scrivi /setdescription " + ) + return + + description = flatten_args(context.args) + "\n" + update_description_on_db(chat_id, description) + sender = "@" + get_sender_name(update) + context.bot.send_message( + chat_id=update.effective_chat.id, + parse_mode='markdown', + text=escape_markdown(f"Ok, {sender}! Ho aggiornato la descrizione!") + ) + + players, day, time, target, default_message, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) + current_situation = format_summary(players, day, time, target, default_message, pitch) + + if bot_last_message_id is None: + msg = print_new_summary(current_situation, update, context) + update_bot_last_message_id_on_db(chat_id, msg.message_id) else: - if len(context.args) == 0: - answer = "Non hai inserito la descrizione: scrivi /setdescription " - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) - else: - description = flatten_args(context.args) + "\n" - update_description_on_db(chat_id, description) - sender = "@" + get_sender_name(update) - answer = "Ok, " + sender + "! Ho aggiornato la descrizione!" - context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=escape_markdown(answer)) - players, day, time, target, default_message, pitch, _, bot_last_message_id = find_all_info_by_chat_id(chat_id) - current_situation = format_summary(players, day, time, target, default_message, pitch) - if bot_last_message_id is None: - 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) + edit_summary(current_situation, bot_last_message_id, update, context) \ No newline at end of file diff --git a/callbacks/set_number.py b/callbacks/set_number.py index 7c9e39b..820b3ee 100644 --- a/callbacks/set_number.py +++ b/callbacks/set_number.py @@ -20,7 +20,7 @@ def set_number(update: Update, context: CallbackContext): if len(context.args) > 1: answer = "Hai messo più di un numero, probabilmente intendevi /setnumber " + context.args[0] else: - choosen_number_str = context.args[0] + chosen_number_str = context.args[0] sender = "@" + get_sender_name(update) players = row[0] if players is None: @@ -28,14 +28,14 @@ def set_number(update: Update, context: CallbackContext): else: participants_num = len(exclude_maybe(players)) - if choosen_number_str.isnumeric(): - choosen_number = int(choosen_number_str) - if choosen_number <= 0 or choosen_number > 40: + if chosen_number_str.isnumeric(): + chosen_number = int(chosen_number_str) + if chosen_number <= 0 or chosen_number > 40: answer = "Non è un numero valido di partecipanti 🌚" - elif choosen_number < participants_num: - answer = "Hai ridotto i partecipanti ma c'è ancora gente nella lista. Io non saprei chi togliere, puoi farlo tu? 🙏" - elif choosen_number < 2: + elif chosen_number < 2: answer = "Il numero che hai inserito non va bene 👎" + elif chosen_number < participants_num: + answer = "Hai ridotto i partecipanti ma c'è ancora gente nella lista. Io non saprei chi togliere, puoi farlo tu? 🙏" else: if teams is not None: update_teams_on_db(chat_id, None) @@ -43,9 +43,9 @@ def set_number(update: Update, context: CallbackContext): answer = "*SQUADRE ANNULLATE*" context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=answer) - update_target_on_db(chat_id, choosen_number) - answer = "Ok, " + sender + "! Ho impostato il numero di partecipanti a " + str(choosen_number) - reached_target = players and participants_num == choosen_number + update_target_on_db(chat_id, chosen_number) + answer = "Ok, " + sender + "! Ho impostato il numero di partecipanti a " + str(chosen_number) + reached_target = players and participants_num == chosen_number 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) if bot_last_message_id is None: diff --git a/test/test_callbacks.py b/test/test_callbacks.py deleted file mode 100644 index 44296e2..0000000 --- a/test/test_callbacks.py +++ /dev/null @@ -1,151 +0,0 @@ -import unittest -from unittest.mock import patch, MagicMock -from callbacks.help_func import help_func -from callbacks.participants import participants -from callbacks.set_day import set_day - -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 _, \n" - "- per essere aggiunto o confermato rispondi _aggiungimi_,\n" - "- per aggiungere o confermare qualcuno usa _aggiungi _,\n" - "- per essere rimosso _toglimi_, \n" - "- per rimuovere qualcuno _togli _, \n" - "- per modificare le squadre scrivi _scambia con _. \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" - ) - - @patch('callbacks.participants.update_bot_last_message_id_on_db') - @patch('callbacks.participants.print_new_summary') - @patch('callbacks.participants.format_summary') - @patch('callbacks.participants.find_all_info_by_chat_id') - @patch('callbacks.participants.find_row_by_chat_id') - def test_participants(self, mock_find_row_by_chat_id, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): - mock_update = MagicMock() - mock_context = MagicMock() - - # Test case 1: row is None - with self.subTest("Row is None"): - mock_find_row_by_chat_id.return_value = None - participants(mock_update, mock_context) - mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_context.bot.send_message.assert_called_once_with( - chat_id=mock_update.effective_chat.id, - parse_mode='markdown', - text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" - ) - - # Reset mocks for the next subtest - mock_find_row_by_chat_id.reset_mock() - mock_context.bot.send_message.reset_mock() - - # Test case 2: row is not None - with self.subTest("Row is not None"): - mock_find_row_by_chat_id.return_value = "row" - mock_find_all_info_by_chat_id.return_value = ("player1, player2", "day", "time", "target", "default_message", "pitch", None, None) - mock_format_summary.return_value = "formatted_summary" - mock_print_new_summary.return_value = MagicMock(message_id=123) - - participants(mock_update, mock_context) - - mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_format_summary.assert_called_once_with("player1, player2", "day", "time", "target", "default_message", "pitch") - mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) - mock_context.bot.send_message.assert_not_called() - mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) - - @patch('callbacks.set_day.update_bot_last_message_id_on_db') - @patch('callbacks.set_day.print_new_summary') - @patch('callbacks.set_day.format_summary') - @patch('callbacks.set_day.find_all_info_by_chat_id') - @patch('callbacks.set_day.get_sender_name') - @patch('callbacks.set_day.update_day_on_db') - @patch('callbacks.set_day.find_row_by_chat_id') - def test_set_day(self, mock_find_row_by_chat_id, mock_update_day_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): - mock_update = MagicMock() - mock_context = MagicMock() - - # Test case 1: row is None - with self.subTest("Row is None"): - mock_find_row_by_chat_id.return_value = None - set_day(mock_update, mock_context) - mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_context.bot.send_message.assert_called_once_with( - chat_id=mock_update.effective_chat.id, - parse_mode='markdown', - text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" - ) - - # Reset mocks for the next subtest - mock_find_row_by_chat_id.reset_mock() - mock_context.bot.send_message.reset_mock() - - # Test case 2: no arguments provided - with self.subTest("No arguments provided"): - mock_find_row_by_chat_id.return_value = "row" - mock_context.args = [] - set_day(mock_update, mock_context) - mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_context.bot.send_message.assert_called_once_with( - chat_id=mock_update.effective_chat.id, - parse_mode='markdown', - text="Non hai inserito il giorno: scrivi /setday " - ) - - # Reset mocks for the next subtest - mock_find_row_by_chat_id.reset_mock() - mock_context.bot.send_message.reset_mock() - - # Test case 3: valid arguments provided - with self.subTest("Valid arguments provided"): - mock_find_row_by_chat_id.return_value = "row" - mock_context.args = ["Monday"] - mock_find_all_info_by_chat_id.return_value = ("player1, player2", "Monday", "20:00", "10", "default_message", "pitch", None, None) - mock_format_summary.return_value = "formatted_summary" - mock_print_new_summary.return_value = MagicMock(message_id=123) - mock_get_sender_name.return_value = "John" - - set_day(mock_update, mock_context) - - mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) - mock_update_day_on_db.assert_called_once_with(mock_update.message.chat_id, "Monday") - mock_get_sender_name.assert_called_once_with(mock_update) - self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) - mock_find_all_info_by_chat_id.assert_called_with(mock_update.message.chat_id) - mock_format_summary.assert_called_once_with("player1, player2", "Monday", "20:00", "10", "default_message", "pitch") - mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) - mock_context.bot.send_message.assert_called_with( - chat_id=mock_update.effective_chat.id, - parse_mode='markdown', - text="Ok, @John! Ho impostato il giorno della partita il Monday" - ) - mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) - -if __name__ == '__main__': - unittest.main() \ No newline at end of file diff --git a/test/test_echo.py b/test/test_echo.py new file mode 100644 index 0000000..6cb65d5 --- /dev/null +++ b/test/test_echo.py @@ -0,0 +1,915 @@ +import unittest +from unittest.mock import patch, MagicMock, call +from callbacks.echo import echo + +class TestEcho(unittest.TestCase): + + def setUp(self): + self.mock_update = MagicMock() + self.mock_context = MagicMock() + self.mock_update.message.chat_id = 12345 + self.mock_update.effective_chat.id = 12345 + self.mock_update.message.text = "" + + def assert_send_message_called_once_with(self, text): + self.mock_context.bot.send_message.assert_called_once_with( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text=text + ) + + @patch('callbacks.echo.trigger_payment_reminder') + @patch('callbacks.echo.edit_summary') + @patch('callbacks.echo.update_bot_last_message_id_on_db') + @patch('callbacks.echo.print_new_summary') + @patch('callbacks.echo.format_summary') + @patch('callbacks.echo.find_all_info_by_chat_id') + @patch('callbacks.echo.update_teams_on_db') + @patch('callbacks.echo.update_players_on_db') + @patch('callbacks.echo.is_already_present') + @patch('callbacks.echo.get_sender_name') + @patch('callbacks.echo.find_row_by_chat_id') + def test_echo(self, mock_find_row_by_chat_id, mock_get_sender_name, mock_is_already_present, mock_update_players_on_db, mock_update_teams_on_db, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db, mock_edit_summary, mock_trigger_payment_reminder): + + # Test case 1: row is None + with self.subTest("Row is None"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = None + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assert_send_message_called_once_with( + "Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + + # Test case 2: aggiungimi - already present + with self.subTest("aggiungimi - already present"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.return_value = (["@John", "@player2"], "day", "time", 10, "default_message", "pitch", None, None) + mock_get_sender_name.return_value = "John" + mock_is_already_present.return_value = True + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_get_sender_name.assert_called_once_with(self.mock_update) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@John") + self.assert_send_message_called_once_with( + "Ma @John, sei già nella lista" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 3: aggiungimi - not present + with self.subTest("aggiungimi - not present"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_get_sender_name.return_value = "John" + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John", "add") + self.assert_send_message_called_once_with( + "Ok, @John, ti aggiungo" + ) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_edit_summary.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_update_players_on_db.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 4: aggiungimi - not present - edit summary + with self.subTest("aggiungimi - not present - edit summary"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, 123), + (["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, 123) + ] + mock_get_sender_name.return_value = "John" + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John", "add") + self.assert_send_message_called_once_with( + "Ok, @John, ti aggiungo" + ) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch") + mock_edit_summary.assert_called_once_with("formatted_summary", 123, self.mock_update, self.mock_context) + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_update_players_on_db.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 5: aggiungimi - not present - match full + with self.subTest("aggiungimi - not present - match full"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@player3"], "day", "time", 3, "default_message", "pitch", None, None) + ] + mock_get_sender_name.return_value = "John" + mock_is_already_present.side_effect = [False, False] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.assert_send_message_called_once_with( + "Siete già in 3" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_update_players_on_db.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 6: aggiungimi - not present - reached target + with self.subTest("aggiungimi - not present - reached target"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 3, "default_message", "pitch", None, 123), + (["@player1", "@player2", "@John"], "day", "time", 3, "default_message", "pitch", None, 123) + ] + mock_get_sender_name.return_value = "John" + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.mock_context.bot.send_message.assert_has_calls([ + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John, ti aggiungo" + ), + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="🚀 *SI GIOCA* 🚀 facciamo le squadre? /teams 😎" + ) + ]) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John", "add") + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John"], "day", "time", 3, "default_message", "pitch") + mock_edit_summary.assert_called_once_with("formatted_summary", 123, self.mock_update, self.mock_context) + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_called_once_with(self.mock_update, self.mock_context, "day", "time") + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_update_players_on_db.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 7: aggiungimi - maybe present - confirmation + with self.subTest("aggiungimi - maybe present confirmation"): + self.mock_update.message.text = "aggiungimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_get_sender_name.return_value = "John" + mock_is_already_present.side_effect = [False, True] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.assert_send_message_called_once_with( + "Ok, @John, ti confermo" + ) + mock_update_players_on_db.assert_has_calls([ + call(self.mock_update.message.chat_id, "@John%is%maybe%present", "remove"), + call(self.mock_update.message.chat_id, "@John", "add") + ]) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_edit_summary.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_update_players_on_db.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 8: aggiungi - already present + with self.subTest("aggiungi - already present"): + self.mock_update.message.text = "aggiungi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@John", "@player2"], "day", "time", 10, "default_message", "pitch", None, None)] + mock_is_already_present.side_effect = [True] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@john") + self.assert_send_message_called_once_with( + "@john è già nella lista" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 9: aggiungi - not present + with self.subTest("aggiungi - not present"): + self.mock_update.message.text = "aggiungi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john%is%maybe%present") + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + self.assert_send_message_called_once_with( + "Ok, aggiungo @john" + ) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@john", "add") + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 10: proponimi - already maybe present + with self.subTest("proponimi - already maybe present"): + self.mock_update.message.text = "proponimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@John", "@player2"], "day", "time", 10, "default_message", "pitch", None, None)] + mock_is_already_present.side_effect = [True] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.assert_send_message_called_once_with( + "Ma @John, sei già in forse" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 11: proponimi - not present + with self.subTest("proponimi - not present"): + self.mock_update.message.text = "proponimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + self.assert_send_message_called_once_with( + "Ok, @John, ti propongo" + ) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John%is%maybe%present", "add") + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 12: proponimi - not present - match full + with self.subTest("proponimi - not present - match full"): + self.mock_update.message.text = "proponimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@player3"], "day", "time", 3, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [False, False] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + self.assert_send_message_called_once_with( + "Siete già in 3" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 13: proponimi - present - becoming maybe present + with self.subTest("proponimi - present - becoming maybe present"): + self.mock_update.message.text = "proponimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@John"], "day", "time", 3, "default_message", "pitch", None, 123), + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 3, "default_message", "pitch", None, 123) + ] + mock_is_already_present.side_effect = [False, True] + mock_format_summary.return_value = "formatted_summary" + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + self.assert_send_message_called_once_with( + "Ok, @John, ti metto in forse" + ) + mock_update_players_on_db.assert_has_calls([ + call(self.mock_update.message.chat_id, "@John", "remove"), + call(self.mock_update.message.chat_id, "@John%is%maybe%present", "add") + ]) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 3, "default_message", "pitch") + mock_edit_summary.assert_called_once_with("formatted_summary", 123, self.mock_update, self.mock_context) + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_edit_summary.reset_mock() + + # Test case 14: proponi - already present + with self.subTest("proponi - already present"): + self.mock_update.message.text = "proponi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@John", "@player2"], "day", "time", 10, "default_message", "pitch", None, None)] + mock_is_already_present.side_effect = [True] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@john%is%maybe%present") + self.assert_send_message_called_once_with( + "@john è già nella lista" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 15: proponi - not present + with self.subTest("proponi - not present"): + self.mock_update.message.text = "proponi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [False, False] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + self.assert_send_message_called_once_with( + "Ok, propongo @john" + ) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@john%is%maybe%present", "add") + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 16: proponi - present - becoming maybe present + with self.subTest("proponi - present - becoming maybe present"): + self.mock_update.message.text = "proponi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@john"], "day", "time", 5, "default_message", "pitch", None, 123), + (["@player1", "@player2", "@john%is%maybe%present"], "day", "time", 5, "default_message", "pitch", None, 123) + ] + mock_is_already_present.side_effect = [False, True] + mock_format_summary.return_value = "formatted_summary" + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + self.assert_send_message_called_once_with( + "Va bene, metto in forse @john" + ) + mock_update_players_on_db.assert_has_calls([ + call(self.mock_update.message.chat_id, "@john", "remove"), + call(self.mock_update.message.chat_id, "@john%is%maybe%present", "add") + ]) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@john%is%maybe%present"], "day", "time", 5, "default_message", "pitch") + mock_edit_summary.assert_called_once_with("formatted_summary", 123, self.mock_update, self.mock_context) + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_edit_summary.reset_mock() + + # Test case 17: proponi - present - becoming maybe present - revoked teams + with self.subTest("proponi - present - becoming maybe present - revoked teams"): + self.mock_update.message.text = "proponi @John" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@john"], "day", "time", 3, "default_message", "pitch", "teams", 123), + (["@player1", "@player2", "@john%is%maybe%present"], "day", "time", 3, "default_message", "pitch", "teams", 123) + ] + mock_is_already_present.side_effect = [False, True] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john%is%maybe%present") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + self.mock_context.bot.send_message.assert_has_calls([ + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="Va bene, metto in forse @john" + ), + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="*SQUADRE ANNULLATE*" + ) + ]) + mock_update_players_on_db.assert_has_calls([ + call(self.mock_update.message.chat_id, "@john", "remove"), + call(self.mock_update.message.chat_id, "@john%is%maybe%present", "add") + ]) + mock_update_teams_on_db.assert_called_once_with(self.mock_update.message.chat_id, None) + mock_format_summary.assert_called_once_with(["@player1", "@player2", "@john%is%maybe%present"], "day", "time", 3, "default_message", "pitch") + mock_edit_summary.assert_called_once_with("formatted_summary", 123, self.mock_update, self.mock_context) + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_edit_summary.reset_mock() + mock_update_teams_on_db.reset_mock() + + # Test case 18: toglimi - not present + with self.subTest("toglimi - not present"): + self.mock_update.message.text = "toglimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None)] + mock_is_already_present.side_effect = [False, False] + mock_get_sender_name.return_value = "John" + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_get_sender_name.assert_called_once_with(self.mock_update) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.assert_send_message_called_once_with( + "Non eri in lista neanche prima" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 19: toglimi - present + with self.subTest("toglimi - present"): + self.mock_update.message.text = "toglimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [True] + mock_get_sender_name.return_value = "John" + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@John") + self.assert_send_message_called_once_with( + "Mamma mia... che paccaro" + ) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John", "remove") + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 20: toglimi - maybe present + with self.subTest("toglimi - maybe present"): + self.mock_update.message.text = "toglimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 10, "default_message", "pitch", None, None), + (["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", None, None) + ] + mock_is_already_present.side_effect = [False, True] + mock_get_sender_name.return_value = "John" + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.assert_send_message_called_once_with( + "Peccato, un po' ci avevo sperato" + ) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John%is%maybe%present", "remove") + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", 10, "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", self.mock_update, self.mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(self.mock_update.message.chat_id, 123) + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 21: toglimi - maybe present - revoked teams + with self.subTest("toglimi - maybe present - revoked teams"): + self.mock_update.message.text = "toglimi" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2", "@John%is%maybe%present"], "day", "time", 3, "default_message", "pitch", "teams", 123), + (["@player1", "@player2"], "day", "time", 3, "default_message", "pitch", "teams", 123) + ] + mock_is_already_present.side_effect = [False, True] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@John%is%maybe%present") + self.mock_context.bot.send_message.assert_has_calls([ + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="Peccato, un po' ci avevo sperato" + ), + call( + chat_id=self.mock_update.effective_chat.id, + parse_mode='markdown', + text="*SQUADRE ANNULLATE*" + ) + ]) + mock_update_players_on_db.assert_called_once_with(self.mock_update.message.chat_id, "@John%is%maybe%present", "remove") + mock_update_teams_on_db.assert_called_once_with(self.mock_update.message.chat_id, None) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", 3, "default_message", "pitch") + mock_print_new_summary.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_players_on_db.reset_mock() + mock_update_teams_on_db.reset_mock() + + # Test case 22: scambia - no teams + with self.subTest("scambia - no teams"): + self.mock_update.message.text = "scambia @John con @player1" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2", "@John"], "day", "time", 10, "default_message", "pitch", None, None)] + mock_is_already_present.return_value = False + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_is_already_present.assert_not_called() + self.assert_send_message_called_once_with( + "Per usare questa funzionalità devi prima formare delle squadre con /teams" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 23: scambia - y not present + with self.subTest("scambia - y not present"): + self.mock_update.message.text = "scambia @John con @player3" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", {"black": ["@John"], "white": ["@player1", "@player2"]}, 123)] + mock_is_already_present.side_effect = [True, False] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@player3") + self.assert_send_message_called_once_with( + "@player3 non c'è" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 24: scambia - x not present + with self.subTest("scambia - x not present"): + self.mock_update.message.text = "scambia @John con @player3" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2"], "day", "time", 10, "default_message", "pitch", {"black": ["@player1", "@player2"], "white": ["@John"]}, 123)] + mock_is_already_present.side_effect = [False, True] + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_is_already_present.assert_called_once_with(self.mock_update.message.chat_id, "@john") + self.assert_send_message_called_once_with( + "@john non c'è" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + + # Test case 25: scambia - x and y in the same team + with self.subTest("scambia - x and y in the same team"): + self.mock_update.message.text = "scambia @John con @player2" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2", "@player3, @john"], "day", "time", 4, "default_message", "pitch", {"black": ["@john", "@player2"], "white": ["@player1", "@player3"]}, 123)] + mock_is_already_present.side_effect = [True, True] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@player2") + self.assert_send_message_called_once_with( + "@john e @player2 sono nella stessa squadra!" + ) + mock_update_teams_on_db.assert_not_called() + mock_trigger_payment_reminder.assert_not_called() + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + self.mock_context.bot.send_message.reset_mock() + mock_is_already_present.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset + mock_update_teams_on_db.reset_mock() + mock_trigger_payment_reminder.reset_mock() + + # Test case 26: scambia - x and y present + with self.subTest("scambia - x and y present"): + self.mock_update.message.text = "scambia @John con @player1" + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2", "@player3, @john"], "day", "time", 4, "default_message", "pitch", {"black": ["@john", "@player2"], "white": ["@player1", "@player3"]}, 123)] + mock_is_already_present.side_effect = [True, True] + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + echo(self.mock_update, self.mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(self.mock_update.message.chat_id) + self.assertEqual(mock_is_already_present.call_count, 2) + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@john") + mock_is_already_present.assert_any_call(self.mock_update.message.chat_id, "@player1") + self.assert_send_message_called_once_with( + "Perfetto, ho scambiato @john con @player1" + ) + mock_update_teams_on_db.assert_called_once_with(self.mock_update.message.chat_id, '{"black": ["@player1", "@player2"], "white": ["@john", "@player3"]}') + mock_trigger_payment_reminder.assert_not_called() +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/test/test_help_func.py b/test/test_help_func.py new file mode 100644 index 0000000..fd3857e --- /dev/null +++ b/test/test_help_func.py @@ -0,0 +1,41 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.help_func import help_func + +class TestHelpFunc(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 _, \n" + "- per essere aggiunto o confermato rispondi _aggiungimi_,\n" + "- per aggiungere o confermare qualcuno usa _aggiungi _,\n" + "- per essere rimosso _toglimi_, \n" + "- per rimuovere qualcuno _togli _, \n" + "- per modificare le squadre scrivi _scambia con _. \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() diff --git a/test/test_participants.py b/test/test_participants.py new file mode 100644 index 0000000..bcc739a --- /dev/null +++ b/test/test_participants.py @@ -0,0 +1,47 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.participants import participants + +class TestParticipants(unittest.TestCase): + @patch('callbacks.participants.update_bot_last_message_id_on_db') + @patch('callbacks.participants.print_new_summary') + @patch('callbacks.participants.format_summary') + @patch('callbacks.participants.find_all_info_by_chat_id') + @patch('callbacks.participants.find_row_by_chat_id') + def test_participants(self, mock_find_row_by_chat_id, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_row_by_chat_id.return_value = None + participants(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: row is not None + with self.subTest("Row is not None"): + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "day", "time", "target", "default_message", "pitch", None, None) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + + participants(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", "target", "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_context.bot.send_message.assert_not_called() + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_set_day.py b/test/test_set_day.py new file mode 100644 index 0000000..8676def --- /dev/null +++ b/test/test_set_day.py @@ -0,0 +1,109 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.set_day import set_day + +class TestSetDay(unittest.TestCase): + @patch('callbacks.set_day.update_bot_last_message_id_on_db') + @patch('callbacks.set_day.print_new_summary') + @patch('callbacks.set_day.format_summary') + @patch('callbacks.set_day.find_all_info_by_chat_id') + @patch('callbacks.set_day.get_sender_name') + @patch('callbacks.set_day.update_day_on_db') + @patch('callbacks.set_day.find_row_by_chat_id') + def test_set_day(self, mock_find_row_by_chat_id, mock_update_day_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_row_by_chat_id.return_value = None + set_day(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: no arguments provided + with self.subTest("No arguments provided"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = [] + set_day(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito il giorno: scrivi /setday " + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 3: valid arguments provided and print new summary + with self.subTest("Valid arguments provided and print new summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["Monday"] + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_day(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_day_on_db.assert_called_once_with(mock_update.message.chat_id, "Monday") + mock_get_sender_name.assert_called_once_with(mock_update) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_find_all_info_by_chat_id.assert_called_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_context.bot.send_message.assert_called_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho impostato il giorno della partita il Monday" + ) + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_update_day_on_db.reset_mock() + mock_get_sender_name.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + + # Test case 4: valid arguments provided and edit summary + with self.subTest("Valid arguments provided and edit summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["Monday"] + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, 123) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_day(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_day_on_db.assert_called_once_with(mock_update.message.chat_id, "Monday") + mock_get_sender_name.assert_called_once_with(mock_update) + self.assertEqual(mock_find_all_info_by_chat_id.call_count, 2) + mock_find_all_info_by_chat_id.assert_called_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch") + mock_print_new_summary.assert_not_called() + mock_context.bot.send_message.assert_called_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho impostato il giorno della partita il Monday" + ) + mock_update_bot_last_message_id_on_db.assert_not_called() + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_set_description.py b/test/test_set_description.py new file mode 100644 index 0000000..786789a --- /dev/null +++ b/test/test_set_description.py @@ -0,0 +1,107 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.set_description import set_description + +class TestSetDescription(unittest.TestCase): + @patch('callbacks.set_description.update_bot_last_message_id_on_db') + @patch('callbacks.set_description.print_new_summary') + @patch('callbacks.set_description.format_summary') + @patch('callbacks.set_description.find_all_info_by_chat_id') + @patch('callbacks.set_description.get_sender_name') + @patch('callbacks.set_description.update_description_on_db') + @patch('callbacks.set_description.find_row_by_chat_id') + def test_set_description(self, mock_find_row_by_chat_id, mock_update_description_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_row_by_chat_id.return_value = None + set_description(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: no arguments provided + with self.subTest("No arguments provided"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = [] + set_description(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito la descrizione: scrivi /setdescription " + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 3: valid arguments provided and print new summary + with self.subTest("Valid arguments provided and print new summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["This is a description"] + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "day", "time", "target", "default_message", "pitch", None, None) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_description(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_description_on_db.assert_called_once_with(mock_update.message.chat_id, "This is a description\n") + mock_get_sender_name.assert_called_once_with(mock_update) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", "target", "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_context.bot.send_message.assert_called_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho aggiornato la descrizione!" + ) + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_update_description_on_db.reset_mock() + mock_get_sender_name.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + + # Test case 4: valid arguments provided and edit summary + with self.subTest("Valid arguments provided and edit summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["This is a description"] + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "day", "time", "target", "default_message", "pitch", None, 123) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_description(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_description_on_db.assert_called_once_with(mock_update.message.chat_id, "This is a description\n") + mock_get_sender_name.assert_called_once_with(mock_update) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "day", "time", "target", "default_message", "pitch") + mock_print_new_summary.assert_not_called() + mock_context.bot.send_message.assert_called_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho aggiornato la descrizione!" + ) + mock_update_bot_last_message_id_on_db.assert_not_called() + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/test/test_set_number.py b/test/test_set_number.py new file mode 100644 index 0000000..1eb2938 --- /dev/null +++ b/test/test_set_number.py @@ -0,0 +1,205 @@ +import unittest +from unittest.mock import patch, MagicMock, call +from callbacks.set_number import set_number + +class TestSetNumber(unittest.TestCase): + @patch('callbacks.set_number.update_bot_last_message_id_on_db') + @patch('callbacks.set_number.edit_summary') + @patch('callbacks.set_number.print_new_summary') + @patch('callbacks.set_number.format_summary') + @patch('callbacks.set_number.find_all_info_by_chat_id') + @patch('callbacks.set_number.get_sender_name') + @patch('callbacks.set_number.update_target_on_db') + @patch('callbacks.set_number.update_teams_on_db') + @patch('callbacks.set_number.remove_job_if_exists') + def test_set_number(self, mock_remove_job_if_exists, mock_update_teams_on_db, mock_update_target_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_edit_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_all_info_by_chat_id.return_value = None + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: more than one argument provided + with self.subTest("More than one argument provided"): + mock_find_all_info_by_chat_id.return_value = "row" + mock_context.args = ["1", "2"] + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Hai messo più di un numero, probabilmente intendevi /setnumber 1" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 3: invalid number provided + with self.subTest("Invalid number provided"): + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_context.args = ["0"] + mock_get_sender_name.return_value = "John" + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non è un numero valido di partecipanti 🌚" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + + # Test case 4: not a number + with self.subTest("Not a number"): + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_context.args = ["not_a_number"] + mock_get_sender_name.return_value = "John" + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="@John, quello ti sembra un numero? 😂" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + + # Test case 5: number less than participants + with self.subTest("Number less than participants"): + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2", "@player3"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_context.args = ["2"] + mock_get_sender_name.return_value = "John" + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Hai ridotto i partecipanti ma c'è ancora gente nella lista. Io non saprei chi togliere, puoi farlo tu? 🙏" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + + # Test case 6: number less than 2 + with self.subTest("Number less than 2"): + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_context.args = ["1"] + mock_get_sender_name.return_value = "John" + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Il numero che hai inserito non va bene 👎" + ) + + # Reset mocks for the next subtest + mock_find_all_info_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + mock_get_sender_name.reset_mock() + + # Test case 7: teams is not None + with self.subTest("Teams is not None"): + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", "teams", 123), + (["@player1", "@player2"], "Monday", "20:00", "8", "default_message", "pitch", None, 123) + ] + mock_context.args = ["8"] + mock_get_sender_name.return_value = "John" + mock_format_summary.return_value = "formatted_summary" + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_has_calls([ + call(mock_update.message.chat_id), + call(mock_update.message.chat_id) + ]) + mock_get_sender_name.assert_called_once_with(mock_update) + mock_update_teams_on_db.assert_called_once_with(mock_update.message.chat_id, None) + mock_remove_job_if_exists.assert_called_once_with(str(mock_update.message.chat_id), mock_context) + mock_update_target_on_db.assert_called_once_with(mock_update.message.chat_id, 8) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "8", "default_message", "pitch") + mock_context.bot.send_message.assert_has_calls([ + call(chat_id=mock_update.effective_chat.id, parse_mode='markdown', text="*SQUADRE ANNULLATE*"), + call(chat_id=mock_update.effective_chat.id, parse_mode='markdown', text="Ok, @John! Ho impostato il numero di partecipanti a 8") + ]) + mock_edit_summary.assert_called_once_with("formatted_summary", 123, mock_update, mock_context) + + # Reset mocks for the next subtest + mock_update_teams_on_db.reset_mock() + mock_remove_job_if_exists.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_update_target_on_db.reset_mock() + mock_get_sender_name.reset_mock() + mock_format_summary.reset_mock() + mock_edit_summary.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 8: reached target + with self.subTest("Reached target"): + mock_find_all_info_by_chat_id.side_effect = [ + (["@player1", "@player2"], "Monday", "20:00", "4", "default_message", "pitch", None, None), + (["@player1", "@player2"], "Monday", "20:00", "2", "default_message", "pitch", None, None) + ] + mock_context.args = ["2"] + mock_get_sender_name.return_value = "John" + mock_print_new_summary.return_value = MagicMock(message_id=123) + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_has_calls([ + call(mock_update.message.chat_id), + call(mock_update.message.chat_id) + ]) + mock_update_target_on_db.assert_called_once_with(mock_update.message.chat_id, 2) + mock_get_sender_name.assert_called_once_with(mock_update) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "2", "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_context.bot.send_message.assert_has_calls([ + call(chat_id=mock_update.effective_chat.id, parse_mode='markdown', text="🚀 *SI GIOCA* 🚀 facciamo le squadre? /teams 😎"), + call(chat_id=mock_update.effective_chat.id, parse_mode='markdown', text="Ok, @John! Ho impostato il numero di partecipanti a 2") + ]) + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + + # Reset mocks for the next subtest + mock_update_teams_on_db.reset_mock() + mock_remove_job_if_exists.reset_mock() + mock_find_all_info_by_chat_id.reset_mock() + mock_update_target_on_db.reset_mock() + mock_get_sender_name.reset_mock() + mock_format_summary.reset_mock() + mock_print_new_summary.reset_mock() + mock_update_bot_last_message_id_on_db.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 9: /setnumber without an argument + with self.subTest("/setnumber without an argument"): + mock_find_all_info_by_chat_id.side_effect = [(["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None)] + mock_context.args = [] + set_number(mock_update, mock_context) + mock_find_all_info_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito il numero: scrivi /setnumber " + ) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_set_pitch.py b/test/test_set_pitch.py new file mode 100644 index 0000000..2c60c5b --- /dev/null +++ b/test/test_set_pitch.py @@ -0,0 +1,72 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.set_pitch import set_pitch + +class TestSetPitch(unittest.TestCase): + @patch('callbacks.set_pitch.update_bot_last_message_id_on_db') + @patch('callbacks.set_pitch.print_new_summary') + @patch('callbacks.set_pitch.format_summary') + @patch('callbacks.set_pitch.find_all_info_by_chat_id') + @patch('callbacks.set_pitch.get_sender_name') + @patch('callbacks.set_pitch.update_pitch_on_db') + @patch('callbacks.set_pitch.find_row_by_chat_id') + def test_set_pitch(self, mock_find_row_by_chat_id, mock_update_pitch_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_row_by_chat_id.return_value = None + set_pitch(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: no arguments provided + with self.subTest("No arguments provided"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = [] + set_pitch(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito il campo: scrivi /setpitch " + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 3: valid arguments provided and print new summary + with self.subTest("Valid arguments provided and print new summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["Campo Test"] + mock_find_row_by_chat_id.return_value = "row" + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "Campo Test", None, None) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_pitch(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_pitch_on_db.assert_called_once_with(mock_update.message.chat_id, "Campo Test") + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho aggiornato il campo!" + ) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "Campo Test") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + +if __name__ == '__main__': + unittest.main() diff --git a/test/test_set_time.py b/test/test_set_time.py new file mode 100644 index 0000000..f37b745 --- /dev/null +++ b/test/test_set_time.py @@ -0,0 +1,72 @@ +import unittest +from unittest.mock import patch, MagicMock +from callbacks.set_time import set_time + +class TestSetTime(unittest.TestCase): + @patch('callbacks.set_time.update_bot_last_message_id_on_db') + @patch('callbacks.set_time.print_new_summary') + @patch('callbacks.set_time.format_summary') + @patch('callbacks.set_time.find_all_info_by_chat_id') + @patch('callbacks.set_time.get_sender_name') + @patch('callbacks.set_time.update_time_on_db') + @patch('callbacks.set_time.find_row_by_chat_id') + def test_set_time(self, mock_find_row_by_chat_id, mock_update_time_on_db, mock_get_sender_name, mock_find_all_info_by_chat_id, mock_format_summary, mock_print_new_summary, mock_update_bot_last_message_id_on_db): + mock_update = MagicMock() + mock_context = MagicMock() + + # Test case 1: row is None + with self.subTest("Row is None"): + mock_find_row_by_chat_id.return_value = None + set_time(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Prima di iniziare con le danze, avvia una partita, per farlo usa /start" + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 2: no arguments provided + with self.subTest("No arguments provided"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = [] + set_time(mock_update, mock_context) + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Non hai inserito l'orario: scrivi /settime " + ) + + # Reset mocks for the next subtest + mock_find_row_by_chat_id.reset_mock() + mock_context.bot.send_message.reset_mock() + + # Test case 3: valid arguments provided and print new summary + with self.subTest("Valid arguments provided and print new summary"): + mock_find_row_by_chat_id.return_value = "row" + mock_context.args = ["20:00"] + mock_find_all_info_by_chat_id.return_value = (["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch", None, None) + mock_format_summary.return_value = "formatted_summary" + mock_print_new_summary.return_value = MagicMock(message_id=123) + mock_get_sender_name.return_value = "John" + + set_time(mock_update, mock_context) + + mock_find_row_by_chat_id.assert_called_once_with(mock_update.message.chat_id) + mock_update_time_on_db.assert_called_once_with(mock_update.message.chat_id, "20:00") + mock_context.bot.send_message.assert_called_once_with( + chat_id=mock_update.effective_chat.id, + parse_mode='markdown', + text="Ok, @John! Ho impostato l'orario della partita alle 20:00" + ) + mock_find_all_info_by_chat_id.assert_called_with(mock_update.message.chat_id) + mock_format_summary.assert_called_once_with(["@player1", "@player2"], "Monday", "20:00", "10", "default_message", "pitch") + mock_print_new_summary.assert_called_once_with("formatted_summary", mock_update, mock_context) + mock_update_bot_last_message_id_on_db.assert_called_once_with(mock_update.message.chat_id, 123) + +if __name__ == '__main__': + unittest.main()