Skip to content

Commit

Permalink
feat: added more callback tests (#53)
Browse files Browse the repository at this point in the history
## What
added separate tests for callbacks
  • Loading branch information
iamgiolaga authored Nov 11, 2024
2 parents f5f78ce + 277880c commit 3c45f67
Show file tree
Hide file tree
Showing 14 changed files with 1,661 additions and 210 deletions.
10 changes: 7 additions & 3 deletions callbacks/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions callbacks/participants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 34 additions & 21 deletions callbacks/set_day.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <giorno>"
)
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 <giorno>"
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)
49 changes: 31 additions & 18 deletions callbacks/set_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <descrizione>"
)
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 <descrizione>"
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)
20 changes: 10 additions & 10 deletions callbacks/set_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ 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:
participants_num = 0
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)
remove_job_if_exists(str(chat_id), context)
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:
Expand Down
Loading

0 comments on commit 3c45f67

Please sign in to comment.