Skip to content

Commit 16a07d6

Browse files
Merge pull request #149 from chatmartin/main
Test suites for blackjack and main with pytest
2 parents bdb7a73 + 0e168ea commit 16a07d6

File tree

10 files changed

+308
-1
lines changed

10 files changed

+308
-1
lines changed

casino/games/uno/uno.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import time
33
import random
44

5-
from .player import Player
5+
from .Player import Player
66
from casino.types import GameContext
77
from casino.utils import clear_screen, cprint, cinput, display_topbar
88
from casino.cards import UnoDeck, UnoCard

tests/__init__.py

Whitespace-only changes.

tests/test_blackjack_eu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for Blackjack EU

tests/test_blackjack_us.py

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
from casino.games.blackjack.blackjack import *
2+
from casino.game_types import *
3+
4+
#Add more tests as needed, potentially testing user inputs
5+
6+
def test_aces():
7+
"""
8+
Tests extreme cases for having aces in your hand to ensure calculation is correct
9+
"""
10+
player = Player(account=Account.generate('test', 100))
11+
player.hand.append(StandardCard(suit="hearts", rank="A"))
12+
player.hand.append(StandardCard(suit="hearts", rank="A"))
13+
assert(player.hand_total == 12)
14+
player.hand.append(StandardCard(suit="hearts", rank="A"))
15+
player.hand[0].rank = 6
16+
assert(player.hand_total == 18)
17+
player.hand.pop(1)
18+
assert(player.hand_total == 17)
19+
player.hand[0].rank = 'A'
20+
total = 12
21+
while total < 21:
22+
player.hand.append(StandardCard(suit="hearts", rank="A"))
23+
total += 1
24+
assert(player.hand_total == total)
25+
while len(player.hand) > 3:
26+
player.hand.pop()
27+
player.hand[0].rank = 10
28+
player.hand[1].rank = 10
29+
assert(player.hand_total == 21)
30+
player.hand.pop(0)
31+
assert(player.hand_total == 21)
32+
33+
def test_bust():
34+
"""
35+
tests scenarios in which the dealer or player busts
36+
"""
37+
ctx = GameContext(account=Account.generate('test', 100),config=Config.default())
38+
blackjack = StandardBlackjack(ctx)
39+
player = blackjack.players[0]
40+
player.bet = 10
41+
#First test case where the player busts and loses
42+
blackjack.deal_cards()
43+
#Add cards to the player's hand to ensure that they lose
44+
while player.hand_total <= 21:
45+
player.hand.append(StandardCard(suit="hearts",rank="K"))
46+
blackjack.check_win()
47+
assert(blackjack.player_win_status[0]=='lose')
48+
assert ('bust' in blackjack.round_results[len(blackjack.round_results) - 1])
49+
50+
#Next case: Dealer busts and loses
51+
blackjack.deal_cards()
52+
while blackjack.dealer.hand_total <= 21:
53+
blackjack.dealer.hand.append(StandardCard(suit="hearts",rank="K"))
54+
blackjack.check_win()
55+
assert(blackjack.player_win_status[0]=='win')
56+
assert ('bust' in blackjack.round_results[len(blackjack.round_results) - 1])
57+
58+
#Next case: Player has 10-10-A and Dealer busts
59+
blackjack.deal_cards()
60+
player.hand[0] = StandardCard(suit="hearts",rank="K")
61+
player.hand[1] = StandardCard(suit="hearts", rank="K")
62+
player.hand.append(StandardCard(suit="hearts", rank="A"))
63+
blackjack.dealer.hand[0] = StandardCard(suit="hearts",rank="K")
64+
blackjack.dealer.hand[1] = StandardCard(suit="hearts",rank="K")
65+
blackjack.dealer.hand.append(StandardCard(suit="hearts",rank="K"))
66+
blackjack.check_win()
67+
assert(blackjack.player_win_status[0]=='win')
68+
assert('bust' in blackjack.round_results[len(blackjack.round_results)-1])
69+
#We will test the payout in this scenario as well
70+
balance = player.account.balance
71+
blackjack.payout()
72+
assert(player.account.balance == (balance+player.bet*2))
73+
74+
def test_payout():
75+
"""
76+
Tests the changes in the user's account for every standard scenario
77+
Normal win: profit = amount bet
78+
Push: profit = 0
79+
Loss: profit = -bet
80+
"""
81+
ctx = GameContext(account=Account.generate('test', 100), config=Config.default())
82+
blackjack = StandardBlackjack(ctx)
83+
player = blackjack.players[0]
84+
dealer = blackjack.dealer
85+
player.bet = 10
86+
#Case 1: the player beats the dealer normally
87+
player.hand.append(StandardCard(suit="hearts",rank="K"))
88+
player.hand.append(StandardCard(suit="hearts", rank="K"))
89+
dealer.hand.append(StandardCard(suit="hearts", rank="K"))
90+
dealer.hand.append(StandardCard(suit="hearts", rank=9))
91+
dealer.hand.append(StandardCard(suit="hearts", rank=3))
92+
#Make sure the win result is correct
93+
blackjack.check_win()
94+
assert(blackjack.player_win_status[0] == 'win')
95+
#Test the payout
96+
balance = player.account.balance
97+
blackjack.payout()
98+
#Should be balance before payout + bet*2 because you get back the original money you bet plus the profit
99+
assert(player.account.balance == (balance+player.bet*2))
100+
101+
#Case 2: the dealer beats the player normally
102+
player.hand.pop()
103+
player.hand.pop()
104+
dealer.hand.pop()
105+
dealer.hand.pop()
106+
dealer.hand.pop()
107+
player.hand.append(StandardCard(suit="hearts", rank="10"))
108+
player.hand.append(StandardCard(suit="hearts", rank="10"))
109+
dealer.hand.append(StandardCard(suit="hearts", rank="10"))
110+
dealer.hand.append(StandardCard(suit="hearts", rank="9"))
111+
dealer.hand.append(StandardCard(suit="hearts", rank="2"))
112+
#Make sure the win result is correct
113+
blackjack.check_win()
114+
assert (blackjack.player_win_status[0] == 'lose')
115+
#Test the payout
116+
balance = player.account.balance
117+
blackjack.payout()
118+
#Balance should be the same as before because you lost the amount you bet already
119+
assert(player.account.balance == balance)
120+
121+
#Case 3: Ties
122+
player.hand.pop()
123+
player.hand.pop()
124+
dealer.hand.pop()
125+
dealer.hand.pop()
126+
dealer.hand.pop()
127+
player.hand.append(StandardCard(suit="hearts", rank="K"))
128+
player.hand.append(StandardCard(suit="hearts", rank="K"))
129+
dealer.hand.append(StandardCard(suit="hearts", rank="K"))
130+
dealer.hand.append(StandardCard(suit="hearts", rank="K"))
131+
#Make sure the win result is correct
132+
blackjack.check_win()
133+
assert (blackjack.player_win_status[0] == 'tie')
134+
#Test the payout
135+
balance = player.account.balance
136+
blackjack.payout()
137+
#Balance should be higher by the amount bet since it is returned in the case of a tie
138+
assert(player.account.balance == (balance+player.bet))
139+
140+
def test_blackjack_payout():
141+
"""
142+
Tests the wins/payouts for every blackjack scenario
143+
Both get blackjack round 1: Tie
144+
Player gets blackjack and dealer does not: Blackjack win (profit = 1.5*amount bet)
145+
Player gets blackjack round 1 and dealer has 21 in round 2: Blackjack win
146+
Player gets 21 in round 2 and dealer has blackjack: Loss
147+
Both player and dealer have 21 but neither have blackjack: Tie
148+
"""
149+
ctx = GameContext(account=Account.generate('test', 100), config=Config.default())
150+
blackjack = StandardBlackjack(ctx)
151+
player = blackjack.players[0]
152+
dealer = blackjack.dealer
153+
player.bet = 10
154+
#Case 1: Both the dealer and the player get blackjack
155+
player.hand.append(StandardCard(suit="hearts", rank="A"))
156+
player.hand.append(StandardCard(suit="hearts", rank="K"))
157+
dealer.hand.append(StandardCard(suit="hearts", rank="A"))
158+
dealer.hand.append(StandardCard(suit="hearts", rank=10))
159+
#Make sure the win result is correct
160+
blackjack.blackjack_check()
161+
assert player.has_blackjack and dealer.has_blackjack
162+
blackjack.check_win()
163+
assert(blackjack.player_win_status[0] == 'tie')
164+
assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1])
165+
#Test the payout
166+
balance = player.account.balance
167+
blackjack.payout()
168+
assert(player.account.balance == (balance+player.bet))
169+
170+
#Case 2: The player gets blackjack and the dealer does not
171+
dealer.hand[0].rank = 10
172+
dealer.hand[1].rank = 'A'
173+
dealer.hand.append(StandardCard(suit="hearts", rank="A"))
174+
#Make sure the results are correct
175+
blackjack.blackjack_check()
176+
assert(player.has_blackjack and not dealer.has_blackjack)
177+
blackjack.check_win()
178+
assert(blackjack.player_win_status[0] == 'win')
179+
assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1])
180+
#Test the payout
181+
balance = player.account.balance
182+
blackjack.payout()
183+
assert(player.account.balance == (balance+player.bet*(1+BLACKJACK_MULTIPLIER)))
184+
185+
#Case 3: Player has blackjack, dealer 21 with more than 2 cards
186+
dealer.hand[1].rank = 10
187+
blackjack.blackjack_check()
188+
assert player.has_blackjack and not dealer.has_blackjack
189+
blackjack.check_win()
190+
assert(blackjack.player_win_status[0] == 'win')
191+
assert('blackjack' in blackjack.round_results[len(blackjack.round_results) - 1])
192+
#Test the payout
193+
balance = player.account.balance
194+
blackjack.payout()
195+
assert(player.account.balance == (balance+player.bet*(1+BLACKJACK_MULTIPLIER)))
196+
197+
#Case 4: Both dealer and player have 21 but more than 2 cards
198+
player.hand[0].rank = 'K'
199+
player.hand.append(StandardCard(suit="hearts", rank='A'))
200+
blackjack.blackjack_check()
201+
assert not(player.has_blackjack or dealer.has_blackjack)
202+
blackjack.check_win()
203+
assert(blackjack.player_win_status[0] == 'tie')
204+
assert ('blackjack' not in blackjack.round_results[len(blackjack.round_results) - 1])
205+
#Test the payout
206+
balance = player.account.balance
207+
blackjack.payout()
208+
assert(player.account.balance == (balance+player.bet))
209+
210+
#Case 5: Player has 21 but dealer has blackjack
211+
dealer.hand.pop()
212+
dealer.hand[0].rank = 'A'
213+
dealer.hand[1].rank = 'Q'
214+
blackjack.blackjack_check()
215+
assert not player.has_blackjack and dealer.has_blackjack
216+
blackjack.check_win()
217+
assert(blackjack.player_win_status[0] == 'lose')
218+
assert 'blackjack' in blackjack.round_results[len(blackjack.round_results) - 1]
219+
#Test the payout
220+
balance = player.account.balance
221+
blackjack.payout()
222+
assert(player.account.balance == balance)
223+
224+
def test_dealer_decision():
225+
"""
226+
Tests the dealer's decisions in certain situations
227+
Dealer total is less than or equal to 16: Hit
228+
Dealer total is greater than or equal to 17: Stand
229+
Edge case: Dealer stands on Ace + 6
230+
"""
231+
ctx = GameContext(account=Account.generate('test', 100), config=Config.default())
232+
blackjack = StandardBlackjack(ctx)
233+
dealer = blackjack.dealer
234+
dealer.hand.append(StandardCard(suit="hearts", rank="K"))
235+
dealer.hand.append(StandardCard(suit="hearts", rank=8))
236+
blackjack.dealer_draw()
237+
assert(len(dealer.hand) == 2)
238+
dealer.hand[1].rank = 6
239+
blackjack.dealer_draw()
240+
assert(len(dealer.hand) == 3)
241+
dealer.hand.pop()
242+
dealer.hand[0].rank = 'A'
243+
blackjack.dealer_draw()
244+
assert(len(dealer.hand) == 2)

tests/test_inputs.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from unittest.mock import patch,ANY,call,Mock
2+
from casino.main import *
3+
from casino.games import *
4+
import pytest
5+
6+
#Add more tests as needed
7+
8+
9+
def test_empty_name_and_quit():
10+
inputs = ["","TEST","1","8","q"]
11+
12+
with patch("casino.main.cinput",side_effect=inputs), \
13+
patch("casino.main.get_theme"), \
14+
patch("casino.main.Account.generate") as mock_generate, \
15+
patch("casino.main.cprint") as mock_print, \
16+
patch("casino.main.clear_screen"), \
17+
patch("casino.main.display_topbar"):
18+
19+
main()
20+
21+
mock_generate.assert_called_with('TEST',ANY)
22+
mock_print.assert_called_with("\nGoodbye!\n")
23+
24+
def test_interrupt():
25+
with patch("casino.main.cinput", side_effect=KeyboardInterrupt):
26+
with pytest.raises(KeyboardInterrupt):
27+
main()
28+
29+
def test_invalid_game():
30+
ctx = GameContext(account=Account.generate('test', 100), config=Config.default())
31+
inputs = ["E","Poker","Blackjack","[1]","20","1.5","Quit","-1",KeyboardInterrupt]
32+
with patch("casino.main.cinput",side_effect=inputs), \
33+
patch("casino.main.cprint") as mock_print:
34+
with pytest.raises(KeyboardInterrupt):
35+
main_menu(ctx)
36+
mock_print.assert_called_with("\nInvalid input. Please try again.\n")
37+
assert mock_print.call_args_list.count(call("\nInvalid input. Please try again.\n"))==len(inputs)-2
38+
39+
@pytest.mark.parametrize("game_index, game_name", [(str(i + 1), name) for i, name in enumerate(ALL_GAMES)])
40+
def test_game_handler_called(game_index, game_name):
41+
ctx = GameContext(account=Account.generate("test", 100), config=Config.default())
42+
handlers = {name: Mock() for name in ALL_GAMES}
43+
with patch("casino.main.prompt_with_refresh", side_effect=["e", game_index, "q"]), \
44+
patch("casino.main.GAME_HANDLERS", handlers), \
45+
patch("casino.main.ALL_GAMES", ALL_GAMES), \
46+
patch("casino.main.cprint"), \
47+
patch("casino.main.clear_screen"), \
48+
patch("casino.main.display_topbar"), \
49+
patch("casino.main.get_theme"), \
50+
patch("casino.main.cinput", return_value="q"):
51+
main_menu(ctx)
52+
53+
handlers[game_name].assert_called_once()
54+
55+
for other_name, mock_func in handlers.items():
56+
if other_name != game_name:
57+
mock_func.assert_not_called()

tests/test_poker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for poker

tests/test_roulette.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for roulette

tests/test_roulette_eu.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for roulette EU

tests/test_slots.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for slots

tests/test_uno.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#TODO: Add test cases for Uno

0 commit comments

Comments
 (0)