From 00e7dcc7b2c68c9fcfc78d8a84cba219b8266924 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giovanni=20Lagan=C3=A0?= Date: Sat, 2 Nov 2024 01:08:58 +0100 Subject: [PATCH 1/3] feat: added callbacks tests --- callbacks/participants.py | 3 +- callbacks/set_day.py | 3 +- test/test_callbacks.py | 109 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) diff --git a/callbacks/participants.py b/callbacks/participants.py index 8888ab9..904135c 100644 --- a/callbacks/participants.py +++ b/callbacks/participants.py @@ -12,8 +12,7 @@ def participants(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, default_message, pitch, teams, bot_last_message_id = find_all_info_by_chat_id( - chat_id) + 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 f532125..e9e1df2 100644 --- a/callbacks/set_day.py +++ b/callbacks/set_day.py @@ -28,8 +28,7 @@ def set_day(update: Update, context: CallbackContext): 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, 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/test/test_callbacks.py b/test/test_callbacks.py index 686cf3a..9285a44 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -1,6 +1,8 @@ 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): @@ -38,5 +40,112 @@ def test_help_func(self, mock_context_class): "/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") + 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 From 4ce9b1b08f1ecb102d86d1c3e358dd0cedb2b578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giovanni=20Lagan=C3=A0?= Date: Sun, 3 Nov 2024 06:46:00 +0100 Subject: [PATCH 2/3] fix: env vars are handled in a .env file --- .gitignore | 1 + conf/{prod.py => conf.py} | 1 + conf/switch.py | 8 -------- db/connection.py | 2 +- db/queries.py | 2 +- main.py | 2 +- 6 files changed, 5 insertions(+), 11 deletions(-) rename conf/{prod.py => conf.py} (89%) delete mode 100644 conf/switch.py diff --git a/.gitignore b/.gitignore index e4cd442..e7730b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .DS_Store +.env __pycache__ venv conf/local.py diff --git a/conf/prod.py b/conf/conf.py similarity index 89% rename from conf/prod.py rename to conf/conf.py index 479ef15..a565fc4 100644 --- a/conf/prod.py +++ b/conf/conf.py @@ -1,5 +1,6 @@ import os +env = os.getenv('ENV', 'local') token = os.getenv("PB_TG_TOKEN") hosting_url = os.getenv("PB_URL") table_name = os.getenv("PB_DB_TABLE_NAME") diff --git a/conf/switch.py b/conf/switch.py deleted file mode 100644 index dec2bf9..0000000 --- a/conf/switch.py +++ /dev/null @@ -1,8 +0,0 @@ -import os - -env = os.getenv('ENV', 'local') - -if env == 'prod': - from conf.prod import * -else: - from conf.local import * \ No newline at end of file diff --git a/db/connection.py b/db/connection.py index e60ff3f..8826882 100644 --- a/db/connection.py +++ b/db/connection.py @@ -1,5 +1,5 @@ import psycopg2 -from conf.switch import host, database, user, password, port +from conf.conf import host, database, user, password, port class Connection: _instance = None diff --git a/db/queries.py b/db/queries.py index d694606..4dd650e 100644 --- a/db/queries.py +++ b/db/queries.py @@ -1,4 +1,4 @@ -from conf.switch import table_name +from conf.conf import table_name from utils.constants import default_target, default_time, default_message from psycopg2 import sql from utils.utils import compute_next_wednesday diff --git a/main.py b/main.py index c7b7929..718f6bb 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ from telegram.ext import CommandHandler, MessageHandler, Filters, Updater -from conf.switch import token, hosting_url, env +from conf.conf import token, hosting_url, env from callbacks.start import start from callbacks.stop import stop from callbacks.set_number import set_number From 5ac0bd8a919eca773a667c709c9c9d30aff0d26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giovanni=20Lagan=C3=A0?= Date: Sun, 3 Nov 2024 07:05:30 +0100 Subject: [PATCH 3/3] fix: fixed values unpacking --- callbacks/participants.py | 2 +- callbacks/set_day.py | 4 ++-- callbacks/set_description.py | 3 +-- callbacks/set_number.py | 3 +-- callbacks/set_pitch.py | 3 +-- callbacks/set_time.py | 5 ++--- test/test_callbacks.py | 2 +- 7 files changed, 9 insertions(+), 13 deletions(-) diff --git a/callbacks/participants.py b/callbacks/participants.py index 904135c..5f8290d 100644 --- a/callbacks/participants.py +++ b/callbacks/participants.py @@ -12,7 +12,7 @@ def participants(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, default_message, pitch = find_all_info_by_chat_id(chat_id) + 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 e9e1df2..36c42c6 100644 --- a/callbacks/set_day.py +++ b/callbacks/set_day.py @@ -23,12 +23,12 @@ def set_day(update: Update, context: CallbackContext): day = flatten_args(context.args) update_day_on_db(chat_id, day) remove_job_if_exists(str(chat_id), context) - 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, _, 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, teams, bot_last_message_id = find_all_info_by_chat_id(chat_id) + 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) diff --git a/callbacks/set_description.py b/callbacks/set_description.py index 3d2d1d4..3b5e181 100644 --- a/callbacks/set_description.py +++ b/callbacks/set_description.py @@ -23,8 +23,7 @@ def set_description(update: Update, context: CallbackContext): 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, teams, bot_last_message_id = find_all_info_by_chat_id( - chat_id) + 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) diff --git a/callbacks/set_number.py b/callbacks/set_number.py index 96b5a85..7c9e39b 100644 --- a/callbacks/set_number.py +++ b/callbacks/set_number.py @@ -46,8 +46,7 @@ def set_number(update: Update, context: CallbackContext): 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 - 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/set_pitch.py b/callbacks/set_pitch.py index af62bcf..4d82386 100644 --- a/callbacks/set_pitch.py +++ b/callbacks/set_pitch.py @@ -23,8 +23,7 @@ def set_pitch(update: Update, context: CallbackContext): sender = "@" + get_sender_name(update) answer = "Ok, " + sender + "! Ho aggiornato il campo!" context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=escape_markdown(answer)) - 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, _, 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/set_time.py b/callbacks/set_time.py index 6268e77..6cb3246 100644 --- a/callbacks/set_time.py +++ b/callbacks/set_time.py @@ -23,13 +23,12 @@ def set_time(update: Update, context: CallbackContext): time = flatten_args(context.args) update_time_on_db(chat_id, time) remove_job_if_exists(str(chat_id), context) - 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, _, 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 l'orario della partita alle " + time context.bot.send_message(chat_id=update.effective_chat.id, parse_mode='markdown', text=escape_markdown(answer)) - 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, _, 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/test/test_callbacks.py b/test/test_callbacks.py index 9285a44..44296e2 100644 --- a/test/test_callbacks.py +++ b/test/test_callbacks.py @@ -67,7 +67,7 @@ def test_participants(self, mock_find_row_by_chat_id, mock_find_all_info_by_chat # 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") + 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)