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
|
from __future__ import annotations
from collections.abc import Callable
from random import randint
from typing import TYPE_CHECKING
from uuid import UUID, uuid4
import pytest
from generalresearch.currency import USDCent
from generalresearch.models.thl.definitions import PayoutStatus
from generalresearch.models.thl.payout import (
BrokerageProductPayoutEvent,
)
from generalresearch.models.thl.wallet.cashout_method import (
CashoutRequestInfo,
)
if TYPE_CHECKING:
from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
from generalresearch.managers.thl.ledger_manager.thl_ledger import (
ThlLedgerManager,
)
from generalresearch.managers.thl.payout import (
BrokerageProductPayoutEventManager,
UserPayoutEventManager,
)
from generalresearch.models.thl.payout import UserPayoutEvent
from generalresearch.models.thl.product import Product
class TestThlPayoutEventManager:
def test_get_by_uuid(
self, brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager
):
"""This validates that the method raises an exception if it
fails. There are plenty of other tests that use this method so
it seems silly to duplicate it here again
"""
with pytest.raises(expected_exception=AssertionError) as excinfo:
brokerage_product_payout_event_manager.get_by_uuid(pe_uuid=uuid4().hex)
assert "expected 1 result, got 0" in str(excinfo.value)
def test_filter_by(
self,
product_factory: Callable[..., Product],
usd_cent: USDCent,
bp_payout_event_factory: Callable[..., BrokerageProductPayoutEvent],
thl_ledger_manager: ThlLedgerManager,
brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
):
N_PRODUCTS = randint(3, 10)
N_PAYOUT_EVENTS = randint(3, 10)
amounts = []
products = []
for _ in range(N_PRODUCTS):
product: Product = product_factory()
thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
products.append(product)
brokerage_product_payout_event_manager.set_account_lookup_table(
thl_lm=thl_ledger_manager
)
for _ in range(N_PAYOUT_EVENTS):
pe = bp_payout_event_factory(product=product, usd_cent=usd_cent)
amounts.append(int(usd_cent))
assert isinstance(pe, BrokerageProductPayoutEvent)
# We just added Payout Events for Products, now go ahead and
# query for them
accounts = thl_ledger_manager.get_accounts_bp_wallet_for_products(
product_uuids=[i.uuid for i in products]
)
res = brokerage_product_payout_event_manager.filter_by(
debit_account_uuids=[i.uuid for i in accounts]
)
assert len(res) == (N_PRODUCTS * N_PAYOUT_EVENTS)
assert sum([i.amount for i in res]) == sum(amounts)
def test_get_bp_payout_events_for_product(
self,
product_factory: Callable[..., Product],
usd_cent: USDCent,
bp_payout_event_factory: Callable[..., BrokerageProductPayoutEvent],
brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
thl_ledger_manager: ThlLedgerManager,
):
N_PRODUCTS = randint(3, 10)
N_PAYOUT_EVENTS = randint(3, 10)
amounts = []
products = []
for _ in range(N_PRODUCTS):
product: Product = product_factory()
products.append(product)
thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
brokerage_product_payout_event_manager.set_account_lookup_table(
thl_lm=thl_ledger_manager
)
for _ in range(N_PAYOUT_EVENTS):
pe = bp_payout_event_factory(product=product, usd_cent=usd_cent)
amounts.append(usd_cent)
assert isinstance(pe, BrokerageProductPayoutEvent)
# We just added 5 Payouts for a specific product: Product, now go
# ahead and query for them
res = brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
product_uuids=[product.id]
)
assert len(res) == N_PAYOUT_EVENTS
# Now that all the Payouts for all the Products have been added, go
# ahead and query for them
res = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
product_uuids=[i.uuid for i in products],
)
)
assert len(res) == (N_PRODUCTS * N_PAYOUT_EVENTS)
assert sum([i.amount for i in res]) == sum(amounts)
@pytest.mark.skip
def test_get_payout_detail(self, user_payout_event_manager: UserPayoutEventManager):
"""This fails because the description coming back is None, but then
it tries to return a PayoutEvent which validates that the
description can't be None
"""
from generalresearch.models.thl.payout import (
PayoutType,
)
rand_amount = randint(a=99, b=999)
pe = user_payout_event_manager.create(
debit_account_uuid=uuid4().hex,
account_reference_type="str-type-random",
account_reference_uuid=uuid4().hex,
cashout_method_uuid=uuid4().hex,
description="Best payout !",
amount=rand_amount,
status=PayoutStatus.PENDING,
ext_ref_id="123",
payout_type=PayoutType.CASH_IN_MAIL,
request_data={"foo": 123},
order_data={},
)
res = user_payout_event_manager.get_payout_detail(pe_uuid=pe.uuid)
assert isinstance(res, CashoutRequestInfo)
# def test_filter_by(self):
# raise NotImplementedError
def test_create(
self,
user_payout_event_factory: Callable[..., UserPayoutEvent],
user_payout_event_manager: UserPayoutEventManager,
):
from generalresearch.models.thl.payout import UserPayoutEvent
# Confirm the creation method returns back an instance.
pe = user_payout_event_factory()
assert isinstance(pe, UserPayoutEvent)
# Now query the DB for that PayoutEvent to confirm it was actually
# saved.
res = user_payout_event_manager.get_by_uuid(pe_uuid=pe.uuid)
assert isinstance(res, UserPayoutEvent)
assert UUID(res.uuid)
# Confirm they're the same
# assert pe.model_dump_json() == res2.model_dump_json()
assert res.description is None
# def test_update(self):
# raise NotImplementedError
def test_create_bp_payout(
self,
product: Product,
delete_ledger_db: Callable[..., None],
create_main_accounts: Callable[..., None],
thl_ledger_manager: ThlLedgerManager,
brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
ledger_manager: LedgerManager,
):
delete_ledger_db()
create_main_accounts()
account_bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet(
product=product
)
brokerage_product_payout_event_manager.set_account_lookup_table(
thl_lm=thl_ledger_manager
)
rand_amount = randint(a=99, b=999)
# Save a Brokerage Product Payout, so we have something in the
# Payout Event table and the respective ledger TX and Entry rows for it
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
thl_ledger_manager=thl_ledger_manager,
product=product,
amount=USDCent(rand_amount),
skip_wallet_balance_check=True,
skip_one_per_day_check=True,
)
assert isinstance(pe, BrokerageProductPayoutEvent)
# Now try to query for it!
res = thl_ledger_manager.get_tx_bp_payouts(
account_uuids=[account_bp_wallet.uuid]
)
assert len(res) == 1
res = thl_ledger_manager.get_tx_bp_payouts(account_uuids=[uuid4().hex])
assert len(res) == 0
# Confirm it added to the users balance. The amount is negative because
# money was sent to the Brokerage product: Product, but they didn't have
# any activity that earned them money
bal = ledger_manager.get_account_balance(account=account_bp_wallet)
assert rand_amount == bal * -1
class TestBPPayoutEvent:
def test_get_bp_bp_payout_events_for_products(
self,
product_factory: Callable[..., Product],
bp_payout_event_factory: Callable[..., BrokerageProductPayoutEvent],
usd_cent: USDCent,
delete_ledger_db: Callable[..., None],
create_main_accounts: Callable[..., None],
brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
thl_ledger_manager: ThlLedgerManager,
):
delete_ledger_db()
create_main_accounts()
N_PAYOUT_EVENTS = randint(3, 10)
amounts = []
product: Product = product_factory()
thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
brokerage_product_payout_event_manager.set_account_lookup_table(
thl_lm=thl_ledger_manager
)
for _ in range(N_PAYOUT_EVENTS):
bp_payout_event_factory(product=product, usd_cent=usd_cent)
amounts.append(usd_cent)
# Fetch using the _bp_bp_ approach, so we have an
# array of BPPayoutEvents
bp_bp_res = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
product_uuids=[product.uuid]
)
)
assert isinstance(bp_bp_res, list)
assert sum(amounts) == sum([i.amount for i in bp_bp_res])
for i in bp_bp_res:
assert isinstance(i, BrokerageProductPayoutEvent)
assert isinstance(i.amount, int)
assert isinstance(i.amount_usd, USDCent)
assert isinstance(i.amount_usd_str, str)
assert i.amount_usd_str[0] == "$"
|