|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | import unittest |
3 | | -from unittest.mock import Mock |
| 3 | +from unittest.mock import Mock, patch |
4 | 4 |
|
5 | | -from blackjackbot.commands.game.functions import is_button_affiliated |
| 5 | +from blackjack.game import BlackJackGame, Card |
| 6 | +from blackjackbot.commands.game.functions import is_button_affiliated, next_player |
| 7 | +from blackjackbot.gamestore import GameStore |
6 | 8 |
|
7 | 9 |
|
8 | 10 | class GameCommandsFunctionsTest(unittest.TestCase): |
@@ -32,6 +34,38 @@ def test_is_button_affiliated_negative(self): |
32 | 34 | self.assertFalse(result) |
33 | 35 | update.callback_query.answer.assert_called_once() |
34 | 36 |
|
| 37 | + @patch("blackjackbot.commands.util.decorators.Database") |
| 38 | + @patch("blackjackbot.commands.game.functions.Database") |
| 39 | + def test_next_player_skips_player_with_blackjack(self, db_mock, decorator_db_mock): |
| 40 | + """When the next player was dealt a blackjack, their turn must be skipped even though another user triggered the update""" |
| 41 | + db_mock.return_value.get_lang_id.return_value = "en" |
| 42 | + decorator_db_mock.return_value.get_lang_id.return_value = "en" |
| 43 | + chat_id = -1001234 |
| 44 | + |
| 45 | + game = BlackJackGame(gametype=BlackJackGame.Type.MULTIPLAYER_GROUP) |
| 46 | + game.add_player(1, "A") |
| 47 | + game.add_player(2, "B") |
| 48 | + game.add_player(3, "C") |
| 49 | + game.running = True |
| 50 | + game.players[0]._cards = [Card(0), Card(1)] # 2 + 3 |
| 51 | + game.players[1]._cards = [Card(12), Card(9)] # Ace + Jack -> blackjack |
| 52 | + game.players[2]._cards = [Card(2), Card(3)] # 4 + 5 |
| 53 | + game.dealer._cards = [Card(8), Card(7)] |
| 54 | + |
| 55 | + game_store = GameStore() |
| 56 | + game_store.add_game(chat_id, game) |
| 57 | + self.addCleanup(game_store.remove_game, chat_id) |
| 58 | + |
| 59 | + update = Mock() |
| 60 | + update.effective_chat.id = chat_id |
| 61 | + update.effective_user.id = 1 # Player A presses "stand" |
| 62 | + |
| 63 | + next_player(update, Mock()) |
| 64 | + |
| 65 | + # B was skipped automatically, so it's now C's turn |
| 66 | + self.assertEqual(game.players[2], game.get_current_player()) |
| 67 | + update.callback_query.answer.assert_not_called() |
| 68 | + |
35 | 69 |
|
36 | 70 | if __name__ == '__main__': |
37 | 71 | unittest.main() |
0 commit comments