Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions blackjackbot/commands/game/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ def join_callback(update, context):

try:
game.add_player(user.id, user.first_name)
# Players joining a group game never sent /start, so they might not be stored in the database yet
Database().add_user(user.id, user.language_code, user.first_name, user.last_name, user.username)
update.effective_message.edit_text(text=translator("mp_request_join").format(game.get_player_list()),
reply_markup=get_join_keyboard(game.id, lang_id))
update.callback_query.answer(translator("mp_join_callback").format(user.first_name))
Expand Down
43 changes: 43 additions & 0 deletions blackjackbot/commands/game/tests/commands_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import unittest
from unittest.mock import Mock, patch

from blackjack.game import BlackJackGame
from blackjackbot.commands.game.commands import join_callback
from blackjackbot.gamestore import GameStore


class GameCommandsTest(unittest.TestCase):

@patch("blackjackbot.commands.util.decorators.Database")
@patch("blackjackbot.commands.game.commands.Database")
def test_join_callback_stores_user(self, db_mock, decorator_db_mock):
"""Players joining a group game via the join button must be stored in the database, so their stats are tracked"""
db_mock.return_value.get_lang_id.return_value = "en"
decorator_db_mock.return_value.get_lang_id.return_value = "en"
chat_id = -1004321

game = BlackJackGame(gametype=BlackJackGame.Type.MULTIPLAYER_GROUP)
game.add_player(1, "A")

game_store = GameStore()
game_store.add_game(chat_id, game)
self.addCleanup(game_store.remove_game, chat_id)

update = Mock()
update.effective_chat.id = chat_id
update.effective_user.id = 2
update.effective_user.first_name = "B"
update.effective_user.last_name = "Bee"
update.effective_user.username = "bbb"
update.effective_user.language_code = "de"
update.callback_query.data = "join_{}".format(game.id)

join_callback(update, Mock())

self.assertEqual(2, len(game.players))
db_mock.return_value.add_user.assert_called_once_with(2, "de", "B", "Bee", "bbb")


if __name__ == '__main__':
unittest.main()
Loading