Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Tests."""
- import itertools
- import random
- import requests
- import pytest
- from deck import Deck, Card
- from blackjack import Hand, Player, GameController
- from game_view import GameView, Move
- def request_faker(url: str) -> requests.models.Response:
- """Fake requests."""
- resp = requests.models.Response()
- def json_fake():
- """Json."""
- return dict()
- resp.json = json_fake
- resp.status_code = 200
- return resp
- requests.get = request_faker
- CARDS = ['AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '0S', 'JS', 'QS', 'KS',
- 'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '0D', 'JD', 'QD', 'KD',
- 'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '0C', 'JC', 'QC', 'KC',
- 'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '0H', 'JH', 'QH', 'KH']
- SUITS = {'S': 'SPADES', 'D': 'DIAMONDS', 'H': 'HEARTS', 'C': 'CLUBS'}
- VALUES = {'A': 'ACE', 'J': 'JACK', 'Q': 'QUEEN', 'K': 'KING', '0': '10'}
- @pytest.fixture()
- def _cards():
- return {
- c: Card(
- value=VALUES.get(c[0], c[0]),
- suit=SUITS.get(c[1]),
- code=c
- ) for c in CARDS
- }
- class MockView(GameView):
- def __init__(self, players_count, bots_count, decks_count, moves: list):
- self.players_count = players_count
- self.bots_count = bots_count
- self.decks_count = decks_count
- self.moves = moves
- self.players = []
- self.house = Hand()
- def ask_move(self) -> Move:
- return self.moves.pop(0)
- def ask_decks_count(self) -> int:
- return self.decks_count
- def ask_players_count(self) -> int:
- return self.players_count
- def ask_bots_count(self) -> int:
- return self.bots_count
- def ask_name(self, player_nr: int) -> str:
- return "testName"
- def show_table(self, players: list, house, player) -> None:
- self.players = players
- self.house = house
- def show_help(self):
- pass
- class MockDeck(Deck):
- """Deck."""
- def __init__(self, deck_count: int = 1, shuffle: bool = False, cards: list = None):
- """Constructor."""
- self.deck_count = deck_count
- self.is_shuffled = shuffle
- self.id = None
- self._backup_deck = cards if cards else list(itertools.chain(*[self._generate_backup_pile() for _ in range(deck_count)]))
- def draw_card(self, top_down: bool = False) -> Card:
- """
- Draw card from the deck.
- :return: card instance.
- """
- try:
- card = self._backup_deck.pop(0)
- print(card)
- card.top_down = top_down
- return card
- except IndexError:
- raise Exception("Deck is empty, you shouldn't get here! Go recheck code :p")
- @property
- def remaining(self):
- """Remaining."""
- return len(self._backup_deck)
- @staticmethod
- def _generate_backup_pile() -> list:
- """Generate backup pile."""
- values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'JACK', 'QUEEN', 'KING', 'ACE']
- suits = ['SPADES', 'DIAMONDS', 'HEARTS', 'CLUBS']
- return [Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits]
- @pytest.mark.timeout(1)
- def test_hand_has_right_attributes():
- assert Hand().cards == []
- assert Hand().score == 0
- assert Hand().is_soft_hand is False
- assert Hand().is_blackjack is False
- assert Hand().can_split is False
- @pytest.mark.timeout(1)
- def test_hand_add_card(_cards):
- hand = Hand()
- hand.add_card(_cards["2D"])
- hand.add_card(_cards["3S"])
- hand.add_card(_cards["5C"])
- hand.add_card(_cards["KH"])
- assert hand.cards == [_cards["2D"], _cards["3S"], _cards["5C"], _cards["KH"]]
- @pytest.mark.timeout(1)
- def test_hand_score_no_special_cards(_cards):
- hand = Hand()
- hand.add_card(_cards["2D"])
- hand.add_card(_cards["3S"])
- hand.add_card(_cards["5C"])
- hand.add_card(_cards["9H"])
- assert hand.score == 19
- assert hand.is_blackjack is False
- assert hand.is_soft_hand is False
- @pytest.mark.timeout(1)
- def test_hand_score_10_jack_queen_king(_cards):
- hand = Hand()
- hand.add_card(_cards["0D"])
- hand.add_card(_cards["JD"])
- hand.add_card(_cards["QD"])
- hand.add_card(_cards["KD"])
- assert hand.score == 40
- @pytest.mark.timeout(1)
- def test_hand_score_ace_value_11_soft(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["2C"])
- assert hand.score == 13
- assert hand.is_soft_hand is True
- @pytest.mark.timeout(1)
- def test_hand_score_ace_value_1(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["KD"])
- hand.add_card(_cards["4H"])
- assert hand.score == 15
- @pytest.mark.timeout(1)
- def test_hand_score_multiple_aces(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AS"])
- hand.add_card(_cards["AD"])
- hand.add_card(_cards["AH"])
- hand.add_card(_cards["2H"])
- assert hand.score == 16
- @pytest.mark.timeout(1)
- def test_hand_score_blackjack(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["KS"])
- assert hand.score == 21
- assert hand.is_blackjack is True
- @pytest.mark.timeout(1)
- def test_hand_cannot_split(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["KS"])
- assert hand.score == 21
- assert hand.can_split is False
- hand.add_card(_cards["2H"])
- assert hand.can_split is False
- def test_hand_cannot_split_more_cards(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AC"])
- assert hand.can_split is False
- hand = Hand()
- hand.add_card(_cards["2C"])
- hand.add_card(_cards["2H"])
- hand.add_card(_cards["AC"])
- assert hand.can_split is False
- @pytest.mark.timeout(1)
- def test_hand_can_split(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AC"])
- assert hand.can_split is True
- hand = Hand()
- hand.add_card(_cards["2C"])
- hand.add_card(_cards["2H"])
- assert hand.can_split is True
- @pytest.mark.timeout(1)
- def test_hand_double_down(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AC"])
- assert hand.is_double_down is False
- hand.double_down(_cards["AH"])
- assert hand.is_double_down is True
- assert hand.score == 13
- assert hand.can_split is False
- @pytest.mark.timeout(1)
- def test_hand_split(_cards):
- hand = Hand()
- hand.add_card(_cards["AC"])
- hand.add_card(_cards["AC"])
- assert hand.can_split is True
- l = hand.split()
- assert isinstance(l, Hand)
- assert l.score == 11
- assert l.can_split is False
- assert hand.score == 11
- assert hand.can_split is False
- hand.add_card(_cards["AC"])
- assert hand.can_split is True
- @pytest.mark.timeout(1)
- def test_house_hand():
- game_controller = GameController(MockView(3, 0, 1, [Move.STAND for _ in range(5)]))
- values = ['10' for _ in range(50)]
- suits = ['SPADES']
- game_controller.start_game()
- game_controller.deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.play_round()
- assert len(game_controller.house.cards) >= 2
- assert len([x for x in game_controller.house.cards if not x.top_down]) == 2
- @pytest.mark.timeout(1)
- def test_game_init():
- game_controller = GameController(MockView(2, 3, 1, []))
- game_controller.start_game()
- assert len(game_controller.players) == 5
- assert game_controller.deck.deck_count == 1
- for p in game_controller.players:
- assert p.coins == game_controller.PLAYER_START_COINS
- @pytest.mark.timeout(1)
- def test_game_1_round_stand():
- game_controller = GameController(MockView(3, 0, 1, [Move.STAND for _ in range(5)]))
- game_controller.start_game()
- game_controller.play_round()
- assert len(game_controller.players) == 3
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 2
- def _check_cards(cards, card_code_str_list):
- for i, card in enumerate(cards):
- assert card.code == card_code_str_list[i]
- @pytest.mark.timeout(1)
- def test_game_1_round_stand_correct_order_of_cards():
- values = [str(i) for i in range(2, 10)]
- suits = ['SPADES']
- game_controller = GameController(MockView(2, 0, 1, [Move.STAND for _ in range(10)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- # S1, S2, S3 -> p1, p2, house
- # S4, S5, S6 -> p1, p2, house
- _check_cards(game_controller.players[0].hands[0].cards, ['2S', '5S'])
- _check_cards(game_controller.players[1].hands[0].cards, ['3S', '6S'])
- @pytest.mark.timeout(1)
- def test_game_1_round_hit():
- game_controller = GameController(MockView(4, 0, 1, [Move.HIT for _ in range(20)]))
- game_controller.start_game()
- game_controller.play_round()
- assert len(game_controller.players) == 4
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) >= 3
- @pytest.mark.timeout(1)
- def test_game_1_round_surrender():
- game_controller = GameController(MockView(5, 0, 1, [Move.SURRENDER for _ in range(10)]))
- game_controller.start_game()
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 2
- assert p.hands[0].is_surrendered is True
- assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST / 2
- @pytest.mark.timeout(1)
- def test_game_1_round_double_down_lose():
- values = ['2' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.hands[0].is_double_down is True
- assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST * 2
- @pytest.mark.timeout(1)
- def test_game_1_round_double_down_tie():
- values = ['7' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.hands[0].is_double_down is True
- assert p.coins == game_controller.PLAYER_START_COINS
- @pytest.mark.timeout(1)
- def test_game_1_round_double_down_win():
- values = ['6' for _ in range(17)] + ['KING' for _ in range(10)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.hands[0].is_double_down is True
- assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST * 2
- @pytest.mark.timeout(1)
- def test_game_1_round_split_no_can_do():
- values = [str(i) for i in range(2, 10)]
- suits = ['SPADES']
- game_controller = GameController(MockView(2, 0, 1, [Move.SPLIT] * 2 + [Move.STAND for _ in range(10)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 2
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 2
- @pytest.mark.timeout(1)
- def test_game_1_round_split_and_win():
- values = ['8' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.SPLIT if i % 3 == 0 else Move.STAND for i in range(200)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 2
- assert len(p.hands[0].cards) == 2
- assert len(p.hands[1].cards) == 2
- assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST * 2
- @pytest.mark.timeout(1)
- def test_game_1_round_split_and_lose():
- values = ['2' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.SPLIT if i % 3 == 0 else Move.STAND for i in range(200)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 2
- assert len(p.hands[0].cards) == 2
- assert len(p.hands[1].cards) == 2
- assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST * 2
- @pytest.mark.timeout(1)
- def test_game_1_round_lose_when_busted():
- values = ['8' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.HIT for i in range(200)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST
- @pytest.mark.timeout(1)
- def test_game_multiple_rounds_lose_busted():
- values = ['8' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.HIT for i in range(200)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 3
- assert p.coins == game_controller.PLAYER_START_COINS - 2 * game_controller.BUY_IN_COST
- @pytest.mark.timeout(1)
- def test_game_multiple_rounds_win():
- values = ['8' for _ in range(50)]
- suits = ['SPADES']
- game_controller = GameController(MockView(5, 0, 1, [Move.STAND for i in range(200)]))
- mock_deck = MockDeck(50, False, cards=[Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits])
- game_controller.start_game()
- game_controller.deck = mock_deck
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 2
- assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST
- game_controller.play_round()
- assert len(game_controller.players) == 5
- for p in game_controller.players:
- assert len(p.hands) == 1
- assert len(p.hands[0].cards) == 2
- assert p.coins == game_controller.PLAYER_START_COINS + 2 * game_controller.BUY_IN_COST
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement