aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_task_adjustment.py
blob: 839bbe12bcf3e302afade26c3a2e6c63fc162a6a (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import logging
from random import randint

import pytest
from datetime import datetime, timezone, timedelta
from decimal import Decimal

from generalresearch.models import Source
from generalresearch.models.thl.definitions import (
    Status,
    StatusCode1,
    WallAdjustedStatus,
)


@pytest.fixture()
def session_complete(session_with_tx_factory, user):
    return session_with_tx_factory(
        user=user, final_status=Status.COMPLETE, wall_req_cpi=Decimal("1.23")
    )


@pytest.fixture()
def session_complete_with_wallet(session_with_tx_factory, user_with_wallet):
    return session_with_tx_factory(
        user=user_with_wallet,
        final_status=Status.COMPLETE,
        wall_req_cpi=Decimal("1.23"),
    )


@pytest.fixture()
def session_fail(user, session_manager, wall_manager):
    session = session_manager.create_dummy(
        started=datetime.now(timezone.utc), user=user
    )
    wall1 = wall_manager.create_dummy(
        session_id=session.id,
        user_id=user.user_id,
        source=Source.DYNATA,
        req_survey_id="72723",
        req_cpi=Decimal("3.22"),
        started=datetime.now(timezone.utc),
    )
    wall_manager.finish(
        wall=wall1,
        status=Status.FAIL,
        status_code_1=StatusCode1.PS_FAIL,
        finished=wall1.started + timedelta(seconds=randint(a=60 * 2, b=60 * 10)),
    )
    session.wall_events.append(wall1)
    return session


class TestHandleRecons:

    def test_complete_to_recon(
        self,
        session_complete,
        thl_lm,
        task_adjustment_manager,
        wall_manager,
        session_manager,
        caplog,
    ):
        print(wall_manager.pg_config.dsn)
        mid = session_complete.uuid
        wall_uuid = session_complete.wall_events[-1].uuid
        s = session_complete
        ledger_manager = thl_lm

        revenue_account = ledger_manager.get_account_task_complete_revenue()
        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert (
            current_amount == 123
        ), "this is the amount of revenue from this task complete"

        bp_wallet_account = ledger_manager.get_account_or_create_bp_wallet(
            s.user.product
        )
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 117, "this is the amount paid to the BP"

        # Do the work here !! ----v
        task_adjustment_manager.handle_single_recon(
            ledger_manager=thl_lm,
            wall_uuid=wall_uuid,
            adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
        )
        assert (
            len(task_adjustment_manager.filter_by_wall_uuid(wall_uuid=wall_uuid)) == 1
        )

        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 0, "after recon, it should be zeroed"
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 0, "this is the amount paid to the BP"
        commission_account = ledger_manager.get_account_or_create_bp_commission(
            s.user.product
        )
        assert ledger_manager.get_account_balance(commission_account) == 0

        # Now, say we get the exact same *adjust to incomplete* msg again. It should do nothing!
        adjusted_timestamp = datetime.now(tz=timezone.utc)
        wall = wall_manager.get_from_uuid(wall_uuid=wall_uuid)
        with pytest.raises(match=" is already "):
            wall_manager.adjust_status(
                wall,
                adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
                adjusted_timestamp=adjusted_timestamp,
                adjusted_cpi=Decimal(0),
            )

        session = session_manager.get_from_id(wall.session_id)
        user = session.user
        with caplog.at_level(logging.INFO):
            ledger_manager.create_tx_task_adjustment(
                wall, user=user, created=adjusted_timestamp
            )
        assert "No transactions needed" in caplog.text

        session.wall_events = wall_manager.get_wall_events(session.id)
        session.user.prefetch_product(wall_manager.pg_config)

        with caplog.at_level(logging.INFO, logger="Wall"):
            session_manager.adjust_status(session)
        assert "is already f" in caplog.text or "is already Status.FAIL" in caplog.text

        with caplog.at_level(logging.INFO, logger="LedgerManager"):
            ledger_manager.create_tx_bp_adjustment(session, created=adjusted_timestamp)
        assert "No transactions needed" in caplog.text

        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 0, "after recon, it should be zeroed"
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 0, "this is the amount paid to the BP"

        # And if we get an adj to fail, and handle it, it should do nothing at all
        task_adjustment_manager.handle_single_recon(
            ledger_manager=thl_lm,
            wall_uuid=wall_uuid,
            adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
        )
        assert (
            len(task_adjustment_manager.filter_by_wall_uuid(wall_uuid=wall_uuid)) == 1
        )
        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 0, "after recon, it should be zeroed"

    def test_fail_to_complete(self, session_fail, thl_lm, task_adjustment_manager):
        s = session_fail
        mid = session_fail.uuid
        wall_uuid = session_fail.wall_events[-1].uuid
        ledger_manager = thl_lm

        revenue_account = ledger_manager.get_account_task_complete_revenue()
        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", mid
        )
        assert (
            current_amount == 0
        ), "this is the amount of revenue from this task complete"

        bp_wallet_account = ledger_manager.get_account_or_create_bp_wallet(
            s.user.product
        )
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 0, "this is the amount paid to the BP"

        task_adjustment_manager.handle_single_recon(
            ledger_manager=thl_lm,
            wall_uuid=wall_uuid,
            adjusted_status=WallAdjustedStatus.ADJUSTED_TO_COMPLETE,
        )

        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 322, "after recon, we should be paid"
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 306, "this is the amount paid to the BP"

        # Now reverse it back to fail
        task_adjustment_manager.handle_single_recon(
            ledger_manager=thl_lm,
            wall_uuid=wall_uuid,
            adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
        )

        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 0
        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 0

        commission_account = ledger_manager.get_account_or_create_bp_commission(
            s.user.product
        )
        assert ledger_manager.get_account_balance(commission_account) == 0

    def test_complete_already_complete(
        self, session_complete, thl_lm, task_adjustment_manager
    ):
        s = session_complete
        mid = session_complete.uuid
        wall_uuid = session_complete.wall_events[-1].uuid
        ledger_manager = thl_lm

        for _ in range(4):
            # just run it 4 times to make sure nothing happens 4 times
            task_adjustment_manager.handle_single_recon(
                ledger_manager=thl_lm,
                wall_uuid=wall_uuid,
                adjusted_status=WallAdjustedStatus.ADJUSTED_TO_COMPLETE,
            )

        revenue_account = ledger_manager.get_account_task_complete_revenue()
        bp_wallet_account = ledger_manager.get_account_or_create_bp_wallet(
            s.user.product
        )
        commission_account = ledger_manager.get_account_or_create_bp_commission(
            s.user.product
        )
        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert current_amount == 123
        assert ledger_manager.get_account_balance(commission_account) == 6

        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 117

    def test_incomplete_already_incomplete(
        self, session_fail, thl_lm, task_adjustment_manager
    ):
        s = session_fail
        mid = session_fail.uuid
        wall_uuid = session_fail.wall_events[-1].uuid
        ledger_manager = thl_lm

        for _ in range(4):
            task_adjustment_manager.handle_single_recon(
                ledger_manager=thl_lm,
                wall_uuid=wall_uuid,
                adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
            )

        revenue_account = ledger_manager.get_account_task_complete_revenue()
        bp_wallet_account = ledger_manager.get_account_or_create_bp_wallet(
            s.user.product
        )
        commission_account = ledger_manager.get_account_or_create_bp_commission(
            s.user.product
        )
        current_amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", mid
        )
        assert current_amount == 0
        assert ledger_manager.get_account_balance(commission_account) == 0

        current_bp_payout = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert current_bp_payout == 0

    def test_complete_to_recon_user_wallet(
        self,
        session_complete_with_wallet,
        user_with_wallet,
        thl_lm,
        task_adjustment_manager,
    ):
        s = session_complete_with_wallet
        mid = s.uuid
        wall_uuid = s.wall_events[-1].uuid
        ledger_manager = thl_lm

        revenue_account = ledger_manager.get_account_task_complete_revenue()
        amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert amount == 123, "this is the amount of revenue from this task complete"

        bp_wallet_account = ledger_manager.get_account_or_create_bp_wallet(
            s.user.product
        )
        user_wallet_account = ledger_manager.get_account_or_create_user_wallet(s.user)
        commission_account = ledger_manager.get_account_or_create_bp_commission(
            s.user.product
        )
        amount = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert amount == 70, "this is the amount paid to the BP"
        amount = ledger_manager.get_account_filtered_balance(
            user_wallet_account, "thl_session", mid
        )
        assert amount == 47, "this is the amount paid to the user"
        assert (
            ledger_manager.get_account_balance(commission_account) == 6
        ), "earned commission"

        task_adjustment_manager.handle_single_recon(
            ledger_manager=thl_lm,
            wall_uuid=wall_uuid,
            adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
        )

        amount = ledger_manager.get_account_filtered_balance(
            revenue_account, "thl_wall", wall_uuid
        )
        assert amount == 0
        amount = ledger_manager.get_account_filtered_balance(
            bp_wallet_account, "thl_session", mid
        )
        assert amount == 0
        amount = ledger_manager.get_account_filtered_balance(
            user_wallet_account, "thl_session", mid
        )
        assert amount == 0
        assert (
            ledger_manager.get_account_balance(commission_account) == 0
        ), "earned commission"