aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_contest/test_milestone.py
blob: 7312a6478a2678bdf0d6c0a3ab3af3e0a0a6e02b (plain)
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
from datetime import datetime, timezone

from generalresearch.models.thl.contest.definitions import (
    ContestStatus,
    ContestEndReason,
)
from generalresearch.models.thl.contest.milestone import (
    MilestoneContest,
    MilestoneContestCreate,
    MilestoneUserView,
    ContestEntryTrigger,
)
from generalresearch.models.thl.product import Product
from generalresearch.models.thl.user import User
from test_utils.managers.contest.conftest import (
    milestone_contest as contest,
    milestone_contest_in_db as contest_in_db,
    milestone_contest_create as contest_create,
    milestone_contest_factory as contest_factory,
)


class TestMilestoneContest:

    def test_should_end(self, contest: MilestoneContest, thl_lm, contest_manager):
        # contest is active and has no entries
        should, msg = contest.should_end()
        assert not should, msg

        # Change so that the contest ends now
        contest.end_condition.ends_at = datetime.now(tz=timezone.utc)
        should, msg = contest.should_end()
        assert should
        assert msg == ContestEndReason.ENDS_AT

        # Change the win amount it thinks it past over the target
        contest.end_condition.ends_at = None
        contest.end_condition.max_winners = 10
        contest.win_count = 10
        should, msg = contest.should_end()
        assert should
        assert msg == ContestEndReason.MAX_WINNERS


class TestMilestoneContestCRUD:

    def test_create(
        self,
        contest_create: MilestoneContestCreate,
        product_user_wallet_yes: Product,
        thl_lm,
        contest_manager,
    ):
        c = contest_manager.create(
            product_id=product_user_wallet_yes.uuid, contest_create=contest_create
        )
        c_out = contest_manager.get(c.uuid)
        assert c == c_out

        assert isinstance(c, MilestoneContest)
        assert c.prize_count == 2
        assert c.status == ContestStatus.ACTIVE
        assert c.end_condition.max_winners == 5
        assert c.entry_trigger == ContestEntryTrigger.TASK_COMPLETE
        assert c.target_amount == 3
        assert c.win_count == 0

    def test_enter(
        self,
        user_with_wallet: User,
        contest_in_db: MilestoneContest,
        thl_lm,
        contest_manager,
    ):
        # Users CANNOT directly enter a milestone contest through the api,
        #   but we'll call this manager method when a trigger is hit.
        contest = contest_in_db
        user = user_with_wallet

        contest_manager.enter_milestone_contest(
            contest_uuid=contest.uuid,
            user=user,
            country_iso="us",
            ledger_manager=thl_lm,
            incr=1,
        )

        c: MilestoneContest = contest_manager.get(contest_uuid=contest.uuid)
        assert c.status == ContestStatus.ACTIVE
        assert not hasattr(c, "current_amount")
        assert not hasattr(c, "current_participants")

        c: MilestoneUserView = contest_manager.get_milestone_user_view(
            contest_uuid=contest.uuid, user=user_with_wallet
        )
        assert c.user_amount == 1

        # Contest wallet should have 0 bc there is no ledger
        contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
            contest_uuid=contest.uuid
        )
        assert thl_lm.get_account_balance(contest_wallet) == 0

        # Enter again!
        contest_manager.enter_milestone_contest(
            contest_uuid=contest.uuid,
            user=user,
            country_iso="us",
            ledger_manager=thl_lm,
            incr=1,
        )
        c: MilestoneUserView = contest_manager.get_milestone_user_view(
            contest_uuid=contest.uuid, user=user_with_wallet
        )
        assert c.user_amount == 2

        # We should have ONE entry with a value of 2
        e = contest_manager.get_entries_by_contest_id(c.id)
        assert len(e) == 1
        assert e[0].amount == 2

    def test_enter_win(
        self,
        user_with_wallet: User,
        contest_in_db: MilestoneContest,
        thl_lm,
        contest_manager,
    ):
        # User enters contest, which brings the USER'S total amount above the limit,
        #   and the user reaches the milestone
        contest = contest_in_db
        user = user_with_wallet

        user_wallet = thl_lm.get_account_or_create_user_wallet(user=user)
        user_balance = thl_lm.get_account_balance(account=user_wallet)
        bp_wallet = thl_lm.get_account_or_create_bp_wallet_by_uuid(
            product_uuid=user.product_id
        )
        bp_wallet_balance = thl_lm.get_account_balance(account=bp_wallet)

        c: MilestoneUserView = contest_manager.get_milestone_user_view(
            contest_uuid=contest.uuid, user=user_with_wallet
        )
        assert c.user_amount == 0
        res, msg = c.is_user_eligible(country_iso="us")
        assert res, msg

        # User reaches the milestone after 3 completes/whatevers.
        for _ in range(3):
            contest_manager.enter_milestone_contest(
                contest_uuid=contest.uuid,
                user=user,
                country_iso="us",
                ledger_manager=thl_lm,
                incr=1,
            )

        # to be clear, the contest itself doesn't end!
        c: MilestoneContest = contest_manager.get(contest_uuid=contest.uuid)
        assert c.status == ContestStatus.ACTIVE

        c: MilestoneUserView = contest_manager.get_milestone_user_view(
            contest_uuid=contest.uuid, user=user_with_wallet
        )
        assert c.user_amount == 3
        res, msg = c.is_user_eligible(country_iso="us")
        assert not res
        assert msg == "User should have won already"

        assert len(c.user_winnings) == 2
        assert c.win_count == 1

        # The prize was awarded! User should have won $1.00
        assert thl_lm.get_account_balance(user_wallet) - user_balance == 100
        # Which was paid from the BP's balance
        assert thl_lm.get_account_balance(bp_wallet) - bp_wallet_balance == -100

        # winnings = cm.get_winnings_by_user(user=user)
        # assert len(winnings) == 1
        # win = winnings[0]
        # assert win.product_user_id == user.product_user_id

    def test_enter_ends(
        self,
        user_factory,
        product_user_wallet_yes: Product,
        contest_in_db: MilestoneContest,
        thl_lm,
        contest_manager,
    ):
        # Multiple users reach the milestone. Contest ends after 5 wins.
        users = [user_factory(product=product_user_wallet_yes) for _ in range(5)]
        contest = contest_in_db

        for u in users:
            contest_manager.enter_milestone_contest(
                contest_uuid=contest.uuid,
                user=u,
                country_iso="us",
                ledger_manager=thl_lm,
                incr=3,
            )

        c: MilestoneContest = contest_manager.get(contest_uuid=contest.uuid)
        assert c.status == ContestStatus.COMPLETED
        assert c.end_reason == ContestEndReason.MAX_WINNERS

    def test_trigger(
        self,
        user_with_wallet: User,
        contest_in_db: MilestoneContest,
        thl_lm,
        contest_manager,
    ):
        # Pretend user just got a complete
        cnt = contest_manager.hit_milestone_triggers(
            country_iso="us",
            user=user_with_wallet,
            event=ContestEntryTrigger.TASK_COMPLETE,
            ledger_manager=thl_lm,
        )
        assert cnt == 1

        # Assert this contest got entered
        c: MilestoneUserView = contest_manager.get_milestone_user_view(
            contest_uuid=contest_in_db.uuid, user=user_with_wallet
        )
        assert c.user_amount == 1


class TestMilestoneContestUserViews:
    def test_list_user_eligible_country(
        self, user_with_wallet: User, contest_factory, thl_lm, contest_manager
    ):
        # No contests exists
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_wallet, country_iso="us"
        )
        assert len(cs) == 0

        # Create a contest. It'll be in the US/CA
        contest_factory(country_isos={"us", "ca"})

        # Not eligible in mexico
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_wallet, country_iso="mx"
        )
        assert len(cs) == 0
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_wallet, country_iso="us"
        )
        assert len(cs) == 1

        # Create another, any country
        contest_factory(country_isos=None)
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_wallet, country_iso="mx"
        )
        assert len(cs) == 1
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_wallet, country_iso="us"
        )
        assert len(cs) == 2

    def test_list_user_eligible(
        self, user_with_money: User, contest_factory, thl_lm, contest_manager
    ):
        # User reaches milestone after 1 complete
        c = contest_factory(target_amount=1)
        user = user_with_money

        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_money, country_iso="us"
        )
        assert len(cs) == 1

        contest_manager.enter_milestone_contest(
            contest_uuid=c.uuid, user=user, country_iso="us", ledger_manager=thl_lm
        )

        # User isn't eligible anymore
        cs = contest_manager.get_many_by_user_eligible(
            user=user_with_money, country_iso="us"
        )
        assert len(cs) == 0

        # But it comes back in the list entered
        cs = contest_manager.get_many_by_user_entered(user=user_with_money)
        assert len(cs) == 1
        c = cs[0]
        assert c.user_amount == 1
        assert isinstance(c, MilestoneUserView)
        assert not hasattr(c, "current_win_probability")

        # They won one contest with 2 prizes
        assert len(contest_manager.get_winnings_by_user(user_with_money)) == 2