Skip to content

Commit

Permalink
test: include probability in sad message testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorres committed Dec 15, 2024
1 parent 5eeabe5 commit 8aded2b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
13 changes: 11 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ def __init__(self):
"You might need to specify one or more environment variables."
)

# Probability to avoid overcrowding Telegram chats with dog pictures
self.sad_message_response_probability: float = 0.45

# Fetches list of dog breeds from the Dogs API
self.fetch_breeds()

Expand Down Expand Up @@ -307,8 +310,14 @@ async def handle_text_messages(self, update, context):
elif has_wolf_reference:
await self.send_wolf_picture(update, context)
elif is_sad_message:
sad_caption = "Don't be sad, have a cute dog!"
await self.send_dog_picture(update, context, mentioned_breed, sad_caption)
# Easter Egg: if the message is sad, send a dog picture
# with a comforting message

# To avoid overloading the chat with dog pictures, only
# send a picture with a certain probability
if random.random() < self.sad_message_response_probability:
sad_caption = "Don't be sad, have a cute dog!"
await self.send_dog_picture(update, context, mentioned_breed, sad_caption)
elif any([should_trigger_picture, is_personal_chat, mentions_a_breed]):
await self.send_dog_picture(update, context, mentioned_breed)

Expand Down
47 changes: 28 additions & 19 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,25 +423,34 @@ async def test_handle_text_messages_for_sad_message(
message, the bot replies with a dog picture and a particular caption.
"""

# instantiating mock bot
bot = get_mock_bot(monkeypatch)
update = get_mock_update(message=sad_message)
context = get_mock_context()

# context is empty of sent photos
assert len(context.bot.photos) == 0

await bot.handle_text_messages(update, context)

# one picture sent through context
assert len(context.bot.photos) == 1

# contains the chat_id, original message id, photo url and caption
chat_id, reply_to_message_id, photo_url, caption = context.bot.photos[0]
assert chat_id == update.message.chat_id
assert reply_to_message_id == update.message.message_id
assert photo_url == "https://dog.pics/dog.png"
assert caption == "Don't be sad, have a cute dog!"
for probability in [0.0, 1.0]:
# we're either testing with a 100% probability or a 0% probability
# so we can assert the outcome of the test
should_send: bool = probability == 1.0

# instantiating mock bot
bot = get_mock_bot(monkeypatch)
bot.sad_message_response_probability = probability
update = get_mock_update(message=sad_message)
context = get_mock_context()

# context is empty of sent photos
assert len(context.bot.photos) == 0

await bot.handle_text_messages(update, context)

if should_send:
# one picture sent through context
assert len(context.bot.photos) == 1

# contains the chat_id, original message id, photo url and caption
chat_id, reply_to_message_id, photo_url, caption = context.bot.photos[0]
assert chat_id == update.message.chat_id
assert reply_to_message_id == update.message.message_id
assert photo_url == "https://dog.pics/dog.png"
assert caption == "Don't be sad, have a cute dog!"
else:
assert len(context.bot.photos) == 0


@pytest.mark.parametrize(
Expand Down

0 comments on commit 8aded2b

Please sign in to comment.