aboutsummaryrefslogtreecommitdiff
path: root/test_utils/models/conftest.py
blob: d5c9a7181d083b71e7d6aa1b64bb79f79ac40eb2 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from __future__ import annotations

from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from decimal import Decimal
from random import choice as randchoice
from random import randint
from typing import TYPE_CHECKING
from uuid import uuid4

import pytest
from pydantic import AwareDatetime, PositiveInt
from pytest import FixtureRequest
from pytest import FixtureRequest as Request

from generalresearch.models.definitions import Source
from generalresearch.models.thl.definitions import (
    WALL_ALLOWED_STATUS_STATUS_CODE,
    Status,
)
from generalresearch.models.thl.survey.model import Buyer, Survey

if TYPE_CHECKING:
    from generalresearch.currency import USDCent
    from generalresearch.managers.thl.buyer import BuyerManager
    from generalresearch.managers.thl.ipinfo import (
        IPGeonameManager,
        IPInformationManager,
    )
    from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
    from generalresearch.managers.thl.payout import (
        BusinessPayoutEventManager,
    )
    from generalresearch.managers.thl.product import ProductManager
    from generalresearch.managers.thl.session import SessionManager
    from generalresearch.managers.thl.survey import SurveyManager
    from generalresearch.managers.thl.user_manager.user_manager import UserManager
    from generalresearch.managers.thl.userhealth import AuditLogManager, IPRecordManager
    from generalresearch.managers.thl.wall import WallManager
    from generalresearch.models.gr.business import (
        Business,
    )
    from generalresearch.models.gr.team import Team
    from generalresearch.models.thl.ipinfo import IPGeoname, IPInformation
    from generalresearch.models.thl.payout import (
        BrokerageProductPayoutEvent,
    )
    from generalresearch.models.thl.product import (
        PayoutConfig,
        Product,
    )
    from generalresearch.models.thl.session import Session, Wall
    from generalresearch.models.thl.user import User
    from generalresearch.pg_helper import PostgresConfig

# === THL ===


@pytest.fixture
def user(
    request: FixtureRequest,
    user_manager: UserManager,
    thl_web_rr: PostgresConfig,
    product_factory: Callable[..., Product],
) -> User:
    product = getattr(request, "product", None)

    if product is None:
        product = product_factory()

    u = user_manager.create_dummy(product_id=product.id)
    u.prefetch_product(pg_config=thl_web_rr)

    return u


@pytest.fixture
def user_with_wallet(
    user_factory: Callable[..., User],
    product_user_wallet_yes: Product,
) -> User:
    # A user on a product with user wallet enabled, but they have no money
    return user_factory(product=product_user_wallet_yes)


@pytest.fixture
def user_with_wallet_amt(
    user_factory: Callable[..., User], product_amt_true: Product
) -> User:
    # A user on a product with user wallet enabled, on AMT, but they have no money
    return user_factory(product=product_amt_true)


@pytest.fixture(scope="function")
def user_factory(
    user_manager: UserManager, thl_web_rr: PostgresConfig
) -> Callable[..., User]:

    def _inner(product: Product, created: datetime | None = None) -> User:
        u = user_manager.create_dummy(product=product, created=created)
        u.prefetch_product(pg_config=thl_web_rr)

        return u

    return _inner


@pytest.fixture
def wall_factory(wall_manager: WallManager) -> Callable[..., Wall]:

    def _inner(
        session: Session, wall_status: Status, req_cpi: Decimal | None = None
    ) -> Wall:

        assert session.started <= datetime.now(
            tz=UTC
        ), "Session can't start in the future"

        if session.wall_events:
            # Subsequent Wall events
            wall = session.wall_events[-1]
            assert not wall.finished, "Can't add new Walls until prior finishes"
            # wall_started = last_wall.started + timedelta(milliseconds=1)
        else:
            # First Wall Event in a session
            wall_started = session.started + timedelta(milliseconds=1)

            wall = wall_manager.create_dummy(
                session_id=session.id,
                user_id=session.user_id,
                started=wall_started,
                req_cpi=req_cpi,
            )
            session.append_wall_event(w=wall)

        options = list(WALL_ALLOWED_STATUS_STATUS_CODE.get(wall_status, {}))
        wall.finish(
            finished=wall.started + timedelta(seconds=randint(a=60 * 2, b=60 * 10)),
            status=wall_status,
            status_code_1=randchoice(options),
        )

        return wall

    return _inner


@pytest.fixture
def wall(session: Session, user: User, wall_manager: WallManager) -> Wall | None:
    from generalresearch.models.thl.task_status import StatusCode1

    wall = wall_manager.create_dummy(session_id=session.id, user_id=user.user_id)
    # thl_session.append_wall_event(wall)
    wall.finish(
        finished=wall.started + timedelta(seconds=randint(a=60 * 2, b=60 * 10)),
        status=Status.COMPLETE,
        status_code_1=StatusCode1.COMPLETE,
    )
    return wall


@pytest.fixture
def session_factory(
    session_manager: SessionManager,
    wall_manager: WallManager,
    utc_hour_ago: datetime,
) -> Callable[..., Session]:
    from generalresearch.models.thl.session import Source

    def _inner(
        user: User,
        # Wall details
        wall_count: int = 5,
        wall_req_cpi: Decimal = Decimal(".50"),
        wall_req_cpis: list[Decimal] | None = None,
        wall_statuses: list[Status] | None = None,
        wall_source: Source = Source.TESTING,
        # Session details
        final_status: Status = Status.COMPLETE,
        started: datetime = utc_hour_ago,
    ) -> Session:
        if wall_req_cpis:
            assert len(wall_req_cpis) == wall_count
        if wall_statuses:
            assert len(wall_statuses) == wall_count

        s = session_manager.create_dummy(started=started, user=user, country_iso="us")
        for idx in range(wall_count):
            if idx == 0:
                # First Wall Event in a session
                wall_started = s.started + timedelta(milliseconds=1)
            else:
                # Subsequent Wall events
                last_wall = s.wall_events[-1]
                assert last_wall.finished, "Can't add new Walls until prior finishes"
                wall_started = last_wall.started + timedelta(milliseconds=1)

            w = wall_manager.create_dummy(
                session_id=s.id,
                source=wall_source,
                user_id=s.user_id,
                started=wall_started,
                req_cpi=wall_req_cpis[idx] if wall_req_cpis else wall_req_cpi,
            )
            s.append_wall_event(w=w)

            # If it's the last wall in the session, respect the final_status
            #   value for the Session
            if wall_statuses:
                _final_status = wall_statuses[idx]
            else:
                _final_status = final_status if idx == wall_count - 1 else Status.FAIL

            options = list(WALL_ALLOWED_STATUS_STATUS_CODE.get(_final_status, {}))
            wall_manager.finish(
                wall=w,
                status=_final_status,
                status_code_1=randchoice(options),
                finished=w.started + timedelta(seconds=randint(a=60 * 2, b=60 * 10)),
            )

        return s

    return _inner


@pytest.fixture(scope="function")
def finished_session_factory(
    session_factory: Callable[..., Session],
    session_manager: SessionManager,
    utc_hour_ago: datetime,
) -> Callable[..., Session]:
    from generalresearch.models.thl.session import Source

    def _inner(
        user: User,
        # Wall details
        wall_count: int = 5,
        wall_req_cpi: Decimal = Decimal(".50"),
        wall_req_cpis: list[Decimal] | None = None,
        wall_statuses: list[Status] | None = None,
        wall_source: Source = Source.TESTING,
        # Session details
        final_status: Status = Status.COMPLETE,
        started: datetime = utc_hour_ago,
    ) -> Session:
        s: Session = session_factory(
            user=user,
            wall_count=wall_count,
            wall_req_cpi=wall_req_cpi,
            wall_req_cpis=wall_req_cpis,
            wall_statuses=wall_statuses,
            wall_source=wall_source,
            final_status=final_status,
            started=started,
        )
        status, status_code_1 = s.determine_session_status()
        _, _, bp_pay, user_pay = s.determine_payments()
        session_manager.finish_with_status(
            s,
            finished=s.wall_events[-1].finished,
            payout=bp_pay,
            user_payout=user_pay,
            status=status,
            status_code_1=status_code_1,
        )
        return s

    return _inner


@pytest.fixture
def session(
    user: User, session_manager: SessionManager, wall_manager: WallManager
) -> Session:

    session: Session = session_manager.create_dummy(user=user, country_iso="us")
    wall: Wall = wall_manager.create_dummy(
        session_id=session.id,
        user_id=session.user_id,
        started=session.started,
    )
    session.append_wall_event(w=wall)

    return session


@pytest.fixture()
def payout_config(request: Request) -> PayoutConfig:
    from generalresearch.models.thl.product import (
        PayoutConfig,
        PayoutTransformation,
        PayoutTransformationPercentArgs,
    )

    return (
        request.param
        if hasattr(request, "payout_config")
        else PayoutConfig(
            payout_format="${payout/100:.2f}",
            payout_transformation=PayoutTransformation(
                f="payout_transformation_percent",
                kwargs=PayoutTransformationPercentArgs(pct=0.40),
            ),
        )
    )


@pytest.fixture
def product_user_wallet_yes(
    product_factory: Callable[..., Product],
    payout_config: PayoutConfig,
    product_manager: ProductManager,
) -> Product:
    from generalresearch.models.thl.product import UserWalletConfig

    return product_factory(
        payout_config=payout_config, user_wallet_config=UserWalletConfig(enabled=True)
    )


@pytest.fixture
def product_user_wallet_no(
    product_factory: Callable[..., Product], product_manager: ProductManager
) -> Product:
    from generalresearch.models.thl.product import UserWalletConfig

    return product_factory(user_wallet_config=UserWalletConfig(enabled=False))


@pytest.fixture
def product_amt_true(
    product_factory: Callable[..., Product],
    product_manager: ProductManager,
    payout_config: PayoutConfig,
) -> Product:
    from generalresearch.models.thl.product import UserWalletConfig

    return product_factory(
        user_wallet_config=UserWalletConfig(amt=True, enabled=True),
        payout_config=payout_config,
    )


@pytest.fixture
def bp_payout_factory(
    thl_ledger_manager: ThlLedgerManager,
    product_manager: ProductManager,
    business_payout_event_manager: BusinessPayoutEventManager,
) -> Callable[..., BrokerageProductPayoutEvent]:

    def _inner(
        product: Product | None = None,
        amount: USDCent | None = None,
        ext_ref_id: str | None = None,
        created: AwareDatetime | None = None,
        skip_wallet_balance_check: bool = False,
        skip_one_per_day_check: bool = False,
    ) -> BrokerageProductPayoutEvent:
        from generalresearch.currency import USDCent

        product = product or product_manager.create_dummy()
        amount = amount or USDCent(randint(1, 99_99))

        return business_payout_event_manager.create_bp_payout_event(
            thl_ledger_manager=thl_ledger_manager,
            product=product,
            amount=amount,
            ext_ref_id=ext_ref_id or uuid4().hex,
            created=created,
        )

    return _inner


@pytest.fixture(scope="session")
def buyer(buyer_manager: BuyerManager) -> Buyer:
    buyer_code = uuid4().hex
    buyer_manager.bulk_get_or_create(source=Source.TESTING, codes=[buyer_code])
    b = Buyer(
        source=Source.TESTING, code=buyer_code, label=f"test-buyer-{buyer_code[:8]}"
    )
    buyer_manager.update(b)
    return b


@pytest.fixture(scope="session")
def buyer_factory(buyer_manager: BuyerManager) -> Callable[..., Buyer]:

    def _inner() -> Buyer:
        return buyer_manager.bulk_get_or_create(
            source=Source.TESTING, codes=[uuid4().hex]
        )[0]

    return _inner


@pytest.fixture(scope="session")
def survey(survey_manager: SurveyManager, buyer: Buyer) -> Survey:
    s = Survey(source=Source.TESTING, survey_id=uuid4().hex, buyer_code=buyer.code)
    survey_manager.create_bulk([s])
    return s


@pytest.fixture(scope="session")
def survey_factory(
    survey_manager: SurveyManager, buyer_factory: Callable[..., Buyer]
) -> Callable[..., Survey]:

    def _inner(buyer: Buyer | None = None) -> Survey:
        buyer = buyer or buyer_factory()
        s = Survey(
            source=Source.TESTING,
            survey_id=uuid4().hex,
            buyer_code=buyer.code,
            buyer_id=buyer.id,
        )
        survey_manager.create_bulk([s])
        return s

    return _inner