Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.03 KB | None | 0 0
  1. """Tests."""
  2. import itertools
  3. import random
  4. import requests
  5.  
  6. import pytest
  7.  
  8. from deck import Deck, Card
  9. from blackjack import Hand, Player, GameController
  10. from game_view import GameView, Move
  11.  
  12.  
  13. def request_faker(url: str) -> requests.models.Response:
  14.     """Fake requests."""
  15.     resp = requests.models.Response()
  16.  
  17.     def json_fake():
  18.         """Json."""
  19.         return dict()
  20.  
  21.     resp.json = json_fake
  22.     resp.status_code = 200
  23.     return resp
  24.  
  25.  
  26. requests.get = request_faker
  27.  
  28.  
  29. CARDS = ['AS', '2S', '3S', '4S', '5S', '6S', '7S', '8S', '9S', '0S', 'JS', 'QS', 'KS',
  30.          'AD', '2D', '3D', '4D', '5D', '6D', '7D', '8D', '9D', '0D', 'JD', 'QD', 'KD',
  31.          'AC', '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', '0C', 'JC', 'QC', 'KC',
  32.          'AH', '2H', '3H', '4H', '5H', '6H', '7H', '8H', '9H', '0H', 'JH', 'QH', 'KH']
  33.  
  34. SUITS = {'S': 'SPADES', 'D': 'DIAMONDS', 'H': 'HEARTS', 'C': 'CLUBS'}
  35. VALUES = {'A': 'ACE', 'J': 'JACK', 'Q': 'QUEEN', 'K': 'KING', '0': '10'}
  36.  
  37.  
  38. @pytest.fixture()
  39. def _cards():
  40.     return {
  41.         c: Card(
  42.             value=VALUES.get(c[0], c[0]),
  43.             suit=SUITS.get(c[1]),
  44.             code=c
  45.         ) for c in CARDS
  46.     }
  47.  
  48.  
  49. class MockView(GameView):
  50.     def __init__(self, players_count, bots_count, decks_count, moves: list):
  51.         self.players_count = players_count
  52.         self.bots_count = bots_count
  53.         self.decks_count = decks_count
  54.         self.moves = moves
  55.         self.players = []
  56.         self.house = Hand()
  57.  
  58.     def ask_move(self) -> Move:
  59.         return self.moves.pop(0)
  60.  
  61.     def ask_decks_count(self) -> int:
  62.         return self.decks_count
  63.  
  64.     def ask_players_count(self) -> int:
  65.         return self.players_count
  66.  
  67.     def ask_bots_count(self) -> int:
  68.         return self.bots_count
  69.  
  70.     def ask_name(self, player_nr: int) -> str:
  71.         return "testName"
  72.  
  73.     def show_table(self, players: list, house, player) -> None:
  74.         self.players = players
  75.         self.house = house
  76.  
  77.     def show_help(self):
  78.         pass
  79.  
  80.  
  81. class MockDeck(Deck):
  82.     """Deck."""
  83.  
  84.     def __init__(self, deck_count: int = 1, shuffle: bool = False, cards: list = None):
  85.         """Constructor."""
  86.         self.deck_count = deck_count
  87.         self.is_shuffled = shuffle
  88.         self.id = None
  89.         self._backup_deck = cards if cards else list(itertools.chain(*[self._generate_backup_pile() for _ in range(deck_count)]))
  90.  
  91.     def draw_card(self, top_down: bool = False) -> Card:
  92.         """
  93.        Draw card from the deck.
  94.  
  95.        :return: card instance.
  96.        """
  97.         try:
  98.             card = self._backup_deck.pop(0)
  99.             print(card)
  100.  
  101.             card.top_down = top_down
  102.             return card
  103.         except IndexError:
  104.             raise Exception("Deck is empty, you shouldn't get here! Go recheck code :p")
  105.  
  106.     @property
  107.     def remaining(self):
  108.         """Remaining."""
  109.         return len(self._backup_deck)
  110.  
  111.     @staticmethod
  112.     def _generate_backup_pile() -> list:
  113.         """Generate backup pile."""
  114.         values = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'JACK', 'QUEEN', 'KING', 'ACE']
  115.         suits = ['SPADES', 'DIAMONDS', 'HEARTS', 'CLUBS']
  116.  
  117.         return [Card(v, s, (v[-1] if v.isdigit() else v[0]) + s[0]) for v in values for s in suits]
  118.  
  119.  
  120. @pytest.mark.timeout(1)
  121. def test_hand_has_right_attributes():
  122.     assert Hand().cards == []
  123.     assert Hand().score == 0
  124.     assert Hand().is_soft_hand is False
  125.     assert Hand().is_blackjack is False
  126.     assert Hand().can_split is False
  127.  
  128.  
  129. @pytest.mark.timeout(1)
  130. def test_hand_add_card(_cards):
  131.     hand = Hand()
  132.     hand.add_card(_cards["2D"])
  133.     hand.add_card(_cards["3S"])
  134.     hand.add_card(_cards["5C"])
  135.     hand.add_card(_cards["KH"])
  136.     assert hand.cards == [_cards["2D"], _cards["3S"], _cards["5C"], _cards["KH"]]
  137.  
  138.  
  139. @pytest.mark.timeout(1)
  140. def test_hand_score_no_special_cards(_cards):
  141.     hand = Hand()
  142.     hand.add_card(_cards["2D"])
  143.     hand.add_card(_cards["3S"])
  144.     hand.add_card(_cards["5C"])
  145.     hand.add_card(_cards["9H"])
  146.     assert hand.score == 19
  147.     assert hand.is_blackjack is False
  148.     assert hand.is_soft_hand is False
  149.  
  150.  
  151. @pytest.mark.timeout(1)
  152. def test_hand_score_10_jack_queen_king(_cards):
  153.     hand = Hand()
  154.     hand.add_card(_cards["0D"])
  155.     hand.add_card(_cards["JD"])
  156.     hand.add_card(_cards["QD"])
  157.     hand.add_card(_cards["KD"])
  158.     assert hand.score == 40
  159.  
  160.  
  161. @pytest.mark.timeout(1)
  162. def test_hand_score_ace_value_11_soft(_cards):
  163.     hand = Hand()
  164.     hand.add_card(_cards["AC"])
  165.     hand.add_card(_cards["2C"])
  166.     assert hand.score == 13
  167.     assert hand.is_soft_hand is True
  168.  
  169.  
  170. @pytest.mark.timeout(1)
  171. def test_hand_score_ace_value_1(_cards):
  172.     hand = Hand()
  173.     hand.add_card(_cards["AC"])
  174.     hand.add_card(_cards["KD"])
  175.     hand.add_card(_cards["4H"])
  176.     assert hand.score == 15
  177.  
  178.  
  179. @pytest.mark.timeout(1)
  180. def test_hand_score_multiple_aces(_cards):
  181.     hand = Hand()
  182.     hand.add_card(_cards["AC"])
  183.     hand.add_card(_cards["AS"])
  184.     hand.add_card(_cards["AD"])
  185.     hand.add_card(_cards["AH"])
  186.     hand.add_card(_cards["2H"])
  187.     assert hand.score == 16
  188.  
  189.  
  190. @pytest.mark.timeout(1)
  191. def test_hand_score_blackjack(_cards):
  192.     hand = Hand()
  193.     hand.add_card(_cards["AC"])
  194.     hand.add_card(_cards["KS"])
  195.     assert hand.score == 21
  196.     assert hand.is_blackjack is True
  197.  
  198.  
  199. @pytest.mark.timeout(1)
  200. def test_hand_cannot_split(_cards):
  201.     hand = Hand()
  202.     hand.add_card(_cards["AC"])
  203.     hand.add_card(_cards["KS"])
  204.     assert hand.score == 21
  205.     assert hand.can_split is False
  206.     hand.add_card(_cards["2H"])
  207.     assert hand.can_split is False
  208.  
  209.  
  210. def test_hand_cannot_split_more_cards(_cards):
  211.     hand = Hand()
  212.     hand.add_card(_cards["AC"])
  213.     hand.add_card(_cards["AC"])
  214.     hand.add_card(_cards["AC"])
  215.     assert hand.can_split is False
  216.     hand = Hand()
  217.     hand.add_card(_cards["2C"])
  218.     hand.add_card(_cards["2H"])
  219.     hand.add_card(_cards["AC"])
  220.     assert hand.can_split is False
  221.  
  222.  
  223. @pytest.mark.timeout(1)
  224. def test_hand_can_split(_cards):
  225.     hand = Hand()
  226.     hand.add_card(_cards["AC"])
  227.     hand.add_card(_cards["AC"])
  228.     assert hand.can_split is True
  229.     hand = Hand()
  230.     hand.add_card(_cards["2C"])
  231.     hand.add_card(_cards["2H"])
  232.     assert hand.can_split is True
  233.  
  234.  
  235. @pytest.mark.timeout(1)
  236. def test_hand_double_down(_cards):
  237.     hand = Hand()
  238.     hand.add_card(_cards["AC"])
  239.     hand.add_card(_cards["AC"])
  240.     assert hand.is_double_down is False
  241.     hand.double_down(_cards["AH"])
  242.     assert hand.is_double_down is True
  243.     assert hand.score == 13
  244.     assert hand.can_split is False
  245.  
  246.  
  247. @pytest.mark.timeout(1)
  248. def test_hand_split(_cards):
  249.     hand = Hand()
  250.     hand.add_card(_cards["AC"])
  251.     hand.add_card(_cards["AC"])
  252.     assert hand.can_split is True
  253.     l = hand.split()
  254.     assert isinstance(l, Hand)
  255.     assert l.score == 11
  256.     assert l.can_split is False
  257.     assert hand.score == 11
  258.     assert hand.can_split is False
  259.     hand.add_card(_cards["AC"])
  260.     assert hand.can_split is True
  261.  
  262. @pytest.mark.timeout(1)
  263. def test_house_hand():
  264.     game_controller = GameController(MockView(3, 0, 1, [Move.STAND for _ in range(5)]))
  265.     values = ['10' for _ in range(50)]
  266.     suits = ['SPADES']
  267.     game_controller.start_game()
  268.     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])
  269.     game_controller.play_round()
  270.     assert len(game_controller.house.cards) >= 2
  271.     assert len([x for x in game_controller.house.cards if not x.top_down]) == 2
  272.  
  273.  
  274. @pytest.mark.timeout(1)
  275. def test_game_init():
  276.     game_controller = GameController(MockView(2, 3, 1, []))
  277.     game_controller.start_game()
  278.     assert len(game_controller.players) == 5
  279.     assert game_controller.deck.deck_count == 1
  280.     for p in game_controller.players:
  281.         assert p.coins == game_controller.PLAYER_START_COINS
  282.  
  283.  
  284. @pytest.mark.timeout(1)
  285. def test_game_1_round_stand():
  286.     game_controller = GameController(MockView(3, 0, 1, [Move.STAND for _ in range(5)]))
  287.     game_controller.start_game()
  288.     game_controller.play_round()
  289.     assert len(game_controller.players) == 3
  290.     for p in game_controller.players:
  291.         assert len(p.hands) == 1
  292.         assert len(p.hands[0].cards) == 2
  293.  
  294.  
  295. def _check_cards(cards, card_code_str_list):
  296.     for i, card in enumerate(cards):
  297.         assert card.code == card_code_str_list[i]
  298.  
  299.  
  300. @pytest.mark.timeout(1)
  301. def test_game_1_round_stand_correct_order_of_cards():
  302.     values = [str(i) for i in range(2,  10)]
  303.     suits = ['SPADES']
  304.     game_controller = GameController(MockView(2, 0, 1, [Move.STAND for _ in range(10)]))
  305.     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])
  306.     game_controller.start_game()
  307.     game_controller.deck = mock_deck
  308.     game_controller.play_round()
  309.     # S1, S2, S3 -> p1, p2, house
  310.     # S4, S5, S6 -> p1, p2, house
  311.     _check_cards(game_controller.players[0].hands[0].cards, ['2S', '5S'])
  312.     _check_cards(game_controller.players[1].hands[0].cards, ['3S', '6S'])
  313.  
  314.  
  315. @pytest.mark.timeout(1)
  316. def test_game_1_round_hit():
  317.     game_controller = GameController(MockView(4, 0, 1, [Move.HIT for _ in range(20)]))
  318.     game_controller.start_game()
  319.     game_controller.play_round()
  320.     assert len(game_controller.players) == 4
  321.     for p in game_controller.players:
  322.         assert len(p.hands) == 1
  323.         assert len(p.hands[0].cards) >= 3
  324.  
  325.  
  326. @pytest.mark.timeout(1)
  327. def test_game_1_round_surrender():
  328.     game_controller = GameController(MockView(5, 0, 1, [Move.SURRENDER for _ in range(10)]))
  329.     game_controller.start_game()
  330.     game_controller.play_round()
  331.     assert len(game_controller.players) == 5
  332.     for p in game_controller.players:
  333.         assert len(p.hands) == 1
  334.         assert len(p.hands[0].cards) == 2
  335.         assert p.hands[0].is_surrendered is True
  336.         assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST / 2
  337.  
  338.  
  339. @pytest.mark.timeout(1)
  340. def test_game_1_round_double_down_lose():
  341.     values = ['2' for _ in range(50)]
  342.     suits = ['SPADES']
  343.  
  344.     game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
  345.     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])
  346.     game_controller.start_game()
  347.     game_controller.deck = mock_deck
  348.     game_controller.play_round()
  349.     assert len(game_controller.players) == 5
  350.     for p in game_controller.players:
  351.         assert len(p.hands) == 1
  352.         assert len(p.hands[0].cards) == 3
  353.         assert p.hands[0].is_double_down is True
  354.         assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST * 2
  355.  
  356.  
  357. @pytest.mark.timeout(1)
  358. def test_game_1_round_double_down_tie():
  359.     values = ['7' for _ in range(50)]
  360.     suits = ['SPADES']
  361.  
  362.     game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
  363.     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])
  364.     game_controller.start_game()
  365.     game_controller.deck = mock_deck
  366.     game_controller.play_round()
  367.     assert len(game_controller.players) == 5
  368.     for p in game_controller.players:
  369.         assert len(p.hands) == 1
  370.         assert len(p.hands[0].cards) == 3
  371.         assert p.hands[0].is_double_down is True
  372.         assert p.coins == game_controller.PLAYER_START_COINS
  373.  
  374.  
  375. @pytest.mark.timeout(1)
  376. def test_game_1_round_double_down_win():
  377.     values = ['6' for _ in range(17)] + ['KING' for _ in range(10)]
  378.     suits = ['SPADES']
  379.  
  380.     game_controller = GameController(MockView(5, 0, 1, [Move.DOUBLE_DOWN for _ in range(10)]))
  381.     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])
  382.     game_controller.start_game()
  383.     game_controller.deck = mock_deck
  384.     game_controller.play_round()
  385.     assert len(game_controller.players) == 5
  386.     for p in game_controller.players:
  387.         assert len(p.hands) == 1
  388.         assert len(p.hands[0].cards) == 3
  389.         assert p.hands[0].is_double_down is True
  390.         assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST * 2
  391.  
  392.  
  393. @pytest.mark.timeout(1)
  394. def test_game_1_round_split_no_can_do():
  395.     values = [str(i) for i in range(2,  10)]
  396.     suits = ['SPADES']
  397.     game_controller = GameController(MockView(2, 0, 1, [Move.SPLIT] * 2 + [Move.STAND for _ in range(10)]))
  398.     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])
  399.     game_controller.start_game()
  400.     game_controller.deck = mock_deck
  401.     game_controller.play_round()
  402.     assert len(game_controller.players) == 2
  403.     for p in game_controller.players:
  404.         assert len(p.hands) == 1
  405.         assert len(p.hands[0].cards) == 2
  406.  
  407.  
  408. @pytest.mark.timeout(1)
  409. def test_game_1_round_split_and_win():
  410.     values = ['8' for _ in range(50)]
  411.     suits = ['SPADES']
  412.  
  413.     game_controller = GameController(MockView(5, 0, 1, [Move.SPLIT if i % 3 == 0 else Move.STAND for i in range(200)]))
  414.     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])
  415.     game_controller.start_game()
  416.     game_controller.deck = mock_deck
  417.     game_controller.play_round()
  418.     assert len(game_controller.players) == 5
  419.     for p in game_controller.players:
  420.         assert len(p.hands) == 2
  421.         assert len(p.hands[0].cards) == 2
  422.         assert len(p.hands[1].cards) == 2
  423.         assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST * 2
  424.  
  425.  
  426. @pytest.mark.timeout(1)
  427. def test_game_1_round_split_and_lose():
  428.     values = ['2' for _ in range(50)]
  429.     suits = ['SPADES']
  430.  
  431.     game_controller = GameController(MockView(5, 0, 1, [Move.SPLIT if i % 3 == 0 else Move.STAND for i in range(200)]))
  432.     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])
  433.     game_controller.start_game()
  434.     game_controller.deck = mock_deck
  435.     game_controller.play_round()
  436.     assert len(game_controller.players) == 5
  437.     for p in game_controller.players:
  438.         assert len(p.hands) == 2
  439.         assert len(p.hands[0].cards) == 2
  440.         assert len(p.hands[1].cards) == 2
  441.         assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST * 2
  442.  
  443.  
  444. @pytest.mark.timeout(1)
  445. def test_game_1_round_lose_when_busted():
  446.     values = ['8' for _ in range(50)]
  447.     suits = ['SPADES']
  448.  
  449.     game_controller = GameController(MockView(5, 0, 1, [Move.HIT for i in range(200)]))
  450.     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])
  451.     game_controller.start_game()
  452.     game_controller.deck = mock_deck
  453.     game_controller.play_round()
  454.     assert len(game_controller.players) == 5
  455.     for p in game_controller.players:
  456.         assert len(p.hands) == 1
  457.         assert len(p.hands[0].cards) == 3
  458.         assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST
  459.  
  460. @pytest.mark.timeout(1)
  461. def test_game_multiple_rounds_lose_busted():
  462.     values = ['8' for _ in range(50)]
  463.     suits = ['SPADES']
  464.  
  465.     game_controller = GameController(MockView(5, 0, 1, [Move.HIT for i in range(200)]))
  466.     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])
  467.     game_controller.start_game()
  468.     game_controller.deck = mock_deck
  469.     game_controller.play_round()
  470.     assert len(game_controller.players) == 5
  471.     for p in game_controller.players:
  472.         assert len(p.hands) == 1
  473.         assert len(p.hands[0].cards) == 3
  474.         assert p.coins == game_controller.PLAYER_START_COINS - game_controller.BUY_IN_COST
  475.  
  476.     game_controller.play_round()
  477.     assert len(game_controller.players) == 5
  478.     for p in game_controller.players:
  479.         assert len(p.hands) == 1
  480.         assert len(p.hands[0].cards) == 3
  481.         assert p.coins == game_controller.PLAYER_START_COINS - 2 * game_controller.BUY_IN_COST
  482.  
  483.  
  484. @pytest.mark.timeout(1)
  485. def test_game_multiple_rounds_win():
  486.     values = ['8' for _ in range(50)]
  487.     suits = ['SPADES']
  488.  
  489.     game_controller = GameController(MockView(5, 0, 1, [Move.STAND for i in range(200)]))
  490.     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])
  491.     game_controller.start_game()
  492.     game_controller.deck = mock_deck
  493.     game_controller.play_round()
  494.     assert len(game_controller.players) == 5
  495.     for p in game_controller.players:
  496.         assert len(p.hands) == 1
  497.         assert len(p.hands[0].cards) == 2
  498.         assert p.coins == game_controller.PLAYER_START_COINS + game_controller.BUY_IN_COST
  499.  
  500.     game_controller.play_round()
  501.     assert len(game_controller.players) == 5
  502.     for p in game_controller.players:
  503.         assert len(p.hands) == 1
  504.         assert len(p.hands[0].cards) == 2
  505.         assert p.coins == game_controller.PLAYER_START_COINS + 2 * game_controller.BUY_IN_COST
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement