Skip to content

Commit

Permalink
feat: added callbacks tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgiolaga committed Nov 2, 2024
1 parent edab7d1 commit 00e7dcc
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
3 changes: 1 addition & 2 deletions callbacks/participants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions callbacks/set_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions test/test_callbacks.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down Expand Up @@ -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 <giorno>"
)

# 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()

0 comments on commit 00e7dcc

Please sign in to comment.