-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_slots.py
246 lines (199 loc) · 6.65 KB
/
test_slots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import collections
from unittest import mock
import pytest
import slots
def test_return_money():
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
sm.insert_money(25)
returned = sm.return_money()
assert returned == 25
def test_pay_table(monkeypatch):
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
spin = mock.Mock()
monkeypatch.setattr(sm, '_spin', spin)
spin.side_effect = [[
slots.Symbol.BELL,
slots.Symbol.BELL,
slots.Symbol.BELL
]]
sm.insert_money(25)
assert sm.play() == 25 * 16
assert sm.reels() == [
slots.Symbol.BELL,
slots.Symbol.BELL,
slots.Symbol.BELL
]
spin.side_effect = [[
slots.Symbol.BELL,
slots.Symbol.BELL,
slots.Symbol.HEARTS
]]
sm.insert_money(25)
assert sm.play() == 25
assert sm.reels() == [
slots.Symbol.BELL,
slots.Symbol.BELL,
slots.Symbol.HEARTS
]
def test_raise_play_without_money():
def play(with_money):
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
if with_money:
sm.insert_money(25)
sm.play()
play(with_money=True)
with pytest.raises(slots.SlotException):
play(with_money=False)
def test_raise_excessive_withdrawal():
def withdraw(amount):
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
sm.adjust_reserves(-amount)
withdraw(500)
withdraw(1000)
with pytest.raises(slots.SlotException):
withdraw(1001)
def test_raise_below_minimum_play():
def insert_money(amount):
sm = slots.SlotMachine()
sm.insert_money(amount)
insert_money(25)
with pytest.raises(slots.SlotException):
insert_money(10)
def test_raise_double_insert():
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
sm.insert_money(25)
with pytest.raises(slots.SlotException):
sm.insert_money(25)
def test_weighted_spinning():
counter = collections.Counter()
for i in range(100000):
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
sm.insert_money(25)
sm.play()
counter.update(sm.reels())
def assert_approx(expected, actual):
assert abs(actual - expected) < expected * 0.05
assert sum(counter[symbol] for symbol in list(slots.Symbol)) == 300000
assert_approx(expected=counter[slots.Symbol.BELL] * 10,
actual=counter[slots.Symbol.HORSESHOES])
assert_approx(expected=counter[slots.Symbol.BELL] * 5,
actual=counter[slots.Symbol.DIAMONDS])
assert_approx(expected=counter[slots.Symbol.BELL] * 5,
actual=counter[slots.Symbol.SPADES])
assert_approx(expected=counter[slots.Symbol.BELL] * 3,
actual=counter[slots.Symbol.HEARTS])
def test_config_file_is_missing(monkeypatch, tmpdir):
config_file = tmpdir.join('.slots.cfg')
monkeypatch.setenv('SLOTS_CFG_PATH', str(config_file))
sm = slots.SlotMachine()
sm.adjust_reserves(1000)
with pytest.raises(slots.SlotException):
sm.insert_money(15)
sm.insert_money(25)
def test_config_minimum_play():
def insert_fifteen(configuration):
slots.SlotMachine(configuration).insert_money(15)
with pytest.raises(slots.SlotException):
insert_fifteen(slots.Configuration())
insert_fifteen(slots.Configuration(minimum_play=15))
def test_load_configuration_minimum_play(tmpdir):
config_file = tmpdir.join('.slots.cfg')
config_file.write('{"minimum_play": 15}')
assert slots.load_configuration(str(config_file)) == slots.Configuration(minimum_play=15)
def test_config_reels(tmpdir):
default_reels = slots.SlotMachine(slots.Configuration())
five_reels = slots.SlotMachine(slots.Configuration(reels=5))
assert len(default_reels.reels()) == 3
assert len(five_reels.reels()) == 5
five_reels.adjust_reserves(1000)
five_reels.insert_money(25)
five_reels.play()
assert len(five_reels.reels()) == 5
def test_load_configuration_reels(tmpdir):
config_file = tmpdir.join('.slots.cfg')
config_file.write('{"reels": 5}')
assert slots.load_configuration(str(config_file)) == slots.Configuration(reels=5)
def test_config_weights():
configuration = slots.Configuration(
weights=(
(slots.Symbol.HORSESHOES, 5),
(slots.Symbol.DIAMONDS, 4),
(slots.Symbol.SPADES, 3),
(slots.Symbol.HEARTS, 2),
(slots.Symbol.BELL, 1)
)
)
counter = collections.Counter()
for i in range(100000):
sm = slots.SlotMachine(configuration)
sm.adjust_reserves(1000)
sm.insert_money(25)
sm.play()
counter.update(sm.reels())
def assert_approx(expected, actual):
assert abs(actual - expected) < expected * 0.05
assert sum(counter[symbol] for symbol in list(slots.Symbol)) == 300000
assert_approx(expected=counter[slots.Symbol.BELL] * 5,
actual=counter[slots.Symbol.HORSESHOES])
assert_approx(expected=counter[slots.Symbol.BELL] * 4,
actual=counter[slots.Symbol.DIAMONDS])
assert_approx(expected=counter[slots.Symbol.BELL] * 3,
actual=counter[slots.Symbol.SPADES])
assert_approx(expected=counter[slots.Symbol.BELL] * 2,
actual=counter[slots.Symbol.HEARTS])
def test_load_configuration_weights(tmpdir):
config_file = tmpdir.join('.slots.cfg')
config_file.write('''{
"weights": {
"Horseshoes": 5,
"Diamonds": 4,
"Spades": 3,
"Hearts": 2,
"Bell": 1
}
}
''')
assert slots.load_configuration(str(config_file)) == slots.Configuration(
weights=(
(slots.Symbol.HORSESHOES, 5),
(slots.Symbol.DIAMONDS, 4),
(slots.Symbol.SPADES, 3),
(slots.Symbol.HEARTS, 2),
(slots.Symbol.BELL, 1)
)
)
def test_config_pay_table(monkeypatch):
sm = slots.SlotMachine(slots.Configuration(
pay_table=(
("Horseshoes Horseshoes Horseshoes", 10),
)
))
sm.adjust_reserves(1000)
spin = mock.Mock()
monkeypatch.setattr(sm, '_spin', spin)
spin.side_effect = [[
slots.Symbol.HORSESHOES,
slots.Symbol.HORSESHOES,
slots.Symbol.HORSESHOES
]]
sm.insert_money(25)
assert sm.play() == 25 * 10
def test_load_configuration_pay_table(tmpdir):
config_file = tmpdir.join('.slots.cfg')
config_file.write('''{
"pay_table": {
"Horseshoes Horseshoes Horseshoes": 10
}
}
''')
assert slots.load_configuration(str(config_file)) == slots.Configuration(
pay_table=(
("Horseshoes Horseshoes Horseshoes", 10),
)
)