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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
from collections import Counter
from uuid import uuid4
import pytest
from pytest import approx
from generalresearch.currency import USDCent
from generalresearch.models.thl.contest import (
ContestPrize,
ContestEndCondition,
)
from generalresearch.models.thl.contest.contest_entry import ContestEntry
from generalresearch.models.thl.contest.definitions import (
ContestEntryType,
ContestPrizeKind,
ContestType,
ContestStatus,
ContestEndReason,
)
from generalresearch.models.thl.contest.raffle import RaffleContest
from tests.models.thl.test_contest.test_contest import TestContest
class TestRaffleContest(TestContest):
@pytest.fixture(scope="function")
def raffle_contest(self, product) -> RaffleContest:
return RaffleContest(
product_id=product.uuid,
name=f"Raffle Contest {uuid4().hex}",
contest_type=ContestType.RAFFLE,
entry_type=ContestEntryType.CASH,
prizes=[
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
)
],
end_condition=ContestEndCondition(target_entry_amount=100),
)
@pytest.fixture(scope="function")
def ended_raffle_contest(self, raffle_contest, utc_now) -> RaffleContest:
# Fake ending the contest
raffle_contest = raffle_contest.model_copy()
raffle_contest.update(
status=ContestStatus.COMPLETED,
ended_at=utc_now,
end_reason=ContestEndReason.ENDS_AT,
)
return raffle_contest
class TestRaffleContestUserView(TestRaffleContest):
def test_user_view(self, raffle_contest, user):
from generalresearch.models.thl.contest.raffle import RaffleUserView
data = {
"current_amount": USDCent(1_00),
"product_user_id": user.product_user_id,
"user_amount": USDCent(1),
"user_amount_today": USDCent(1),
}
r = RaffleUserView.model_validate(raffle_contest.model_dump() | data)
res = r.model_dump(mode="json")
assert res["product_user_id"] == user.product_user_id
assert res["user_amount_today"] == 1
assert res["current_win_probability"] == approx(0.01, rel=0.000001)
assert res["projected_win_probability"] == approx(0.01, rel=0.000001)
# Now change the amount
r.current_amount = USDCent(1_01)
res = r.model_dump(mode="json")
assert res["current_win_probability"] == approx(0.0099, rel=0.001)
assert res["projected_win_probability"] == approx(0.0099, rel=0.001)
def test_win_pct(self, raffle_contest, user):
from generalresearch.models.thl.contest.raffle import RaffleUserView
data = {
"current_amount": USDCent(10),
"product_user_id": user.product_user_id,
"user_amount": USDCent(1),
"user_amount_today": USDCent(1),
}
r = RaffleUserView.model_validate(raffle_contest.model_dump() | data)
r.prizes = [
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
]
# Raffle has 10 entries, user has 1 entry.
# There are 2 prizes.
assert r.current_win_probability == approx(expected=0.2, rel=0.01)
# He can only possibly win 1 prize
assert r.current_prize_count_probability[1] == approx(expected=0.2, rel=0.01)
# He has a 0 prob of winning 2 prizes
assert r.current_prize_count_probability[2] == 0
# Contest end when there are 100 entries, so 1/100 * 2 prizes
assert r.projected_win_probability == approx(expected=0.02, rel=0.01)
# Change to user having 2 entries (out of 10)
# Still with 2 prizes
r.user_amount = USDCent(2)
assert r.current_win_probability == approx(expected=0.3777, rel=0.01)
# 2/10 chance of winning 1st, 8/9 change of not winning 2nd, plus the
# same in the other order
p = (2 / 10) * (8 / 9) * 2 # 0.355555
assert r.current_prize_count_probability[1] == approx(p, rel=0.01)
p = (2 / 10) * (1 / 9) # 0.02222
assert r.current_prize_count_probability[2] == approx(p, rel=0.01)
class TestRaffleContestWinners(TestRaffleContest):
def test_winners_1_prize(self, ended_raffle_contest, user_1, user_2, user_3):
ended_raffle_contest.entries = [
ContestEntry(
user=user_1,
amount=USDCent(1),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_2,
amount=USDCent(2),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_3,
amount=USDCent(3),
entry_type=ContestEntryType.CASH,
),
]
# There is 1 prize. If we select a winner 1000 times, we'd expect user 1
# to win ~ 1/6th of the time, user 2 ~2/6th and 3 3/6th.
winners = ended_raffle_contest.select_winners()
assert len(winners) == 1
c = Counter(
[
ended_raffle_contest.select_winners()[0].user.user_id
for _ in range(10000)
]
)
assert c[user_1.user_id] == approx(
10000 * 1 / 6, rel=0.1
) # 10% relative tolerance
assert c[user_2.user_id] == approx(10000 * 2 / 6, rel=0.1)
assert c[user_3.user_id] == approx(10000 * 3 / 6, rel=0.1)
def test_winners_2_prizes(self, ended_raffle_contest, user_1, user_2, user_3):
ended_raffle_contest.prizes.append(
ContestPrize(
name="iPod 64GB Black",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
)
)
ended_raffle_contest.entries = [
ContestEntry(
user=user_3,
amount=USDCent(1),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_1,
amount=USDCent(9999999),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_2,
amount=USDCent(1),
entry_type=ContestEntryType.CASH,
),
]
# In this scenario, user 1 should win both prizes
winners = ended_raffle_contest.select_winners()
assert len(winners) == 2
# Two different prizes
assert len({w.prize.name for w in winners}) == 2
# Same user
assert all(w.user.user_id == user_1.user_id for w in winners)
def test_winners_2_prizes_1_entry(self, ended_raffle_contest, user_3):
ended_raffle_contest.prizes = [
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
ContestPrize(
name="iPod 64GB Black",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
]
ended_raffle_contest.entries = [
ContestEntry(
user=user_3,
amount=USDCent(1),
entry_type=ContestEntryType.CASH,
),
]
# One prize goes unclaimed
winners = ended_raffle_contest.select_winners()
assert len(winners) == 1
def test_winners_2_prizes_1_entry_2_pennies(self, ended_raffle_contest, user_3):
ended_raffle_contest.prizes = [
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
ContestPrize(
name="iPod 64GB Black",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
]
ended_raffle_contest.entries = [
ContestEntry(
user=user_3,
amount=USDCent(2),
entry_type=ContestEntryType.CASH,
),
]
# User wins both prizes
winners = ended_raffle_contest.select_winners()
assert len(winners) == 2
def test_winners_3_prizes_3_entries(
self, ended_raffle_contest, product, user_1, user_2, user_3
):
ended_raffle_contest.prizes = [
ContestPrize(
name="iPod 64GB White",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
ContestPrize(
name="iPod 64GB Black",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
ContestPrize(
name="iPod 64GB Red",
kind=ContestPrizeKind.PHYSICAL,
estimated_cash_value=USDCent(100_00),
),
]
ended_raffle_contest.entries = [
ContestEntry(
user=user_1,
amount=USDCent(1),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_2,
amount=USDCent(2),
entry_type=ContestEntryType.CASH,
),
ContestEntry(
user=user_3,
amount=USDCent(3),
entry_type=ContestEntryType.CASH,
),
]
winners = ended_raffle_contest.select_winners()
assert len(winners) == 3
winners = [ended_raffle_contest.select_winners() for _ in range(10000)]
# There's 3 winners, the 1st should follow the same percentages
c = Counter([w[0].user.user_id for w in winners])
assert c[user_1.user_id] == approx(10000 * 1 / 6, rel=0.1)
assert c[user_2.user_id] == approx(10000 * 2 / 6, rel=0.1)
assert c[user_3.user_id] == approx(10000 * 3 / 6, rel=0.1)
# Assume the 1st user won
ended_raffle_contest.entries.pop(0)
winners = [ended_raffle_contest.select_winners() for _ in range(10000)]
c = Counter([w[0].user.user_id for w in winners])
assert c[user_2.user_id] == approx(10000 * 2 / 5, rel=0.1)
assert c[user_3.user_id] == approx(10000 * 3 / 5, rel=0.1)
|