aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_ledger
diff options
context:
space:
mode:
authorMax Nanis2026-08-25 11:09:52 -0700
committerMax Nanis2026-08-25 11:09:52 -0700
commit3b4059135be47f7752a08e4277a85f9e57ceaa9d (patch)
tree122561f7cc99aeb2933a4fc37d07b3eecd514701 /tests/managers/thl/test_ledger
parent5682f48a96b0713929a4bb52e72eec5907d5dd32 (diff)
downloadgeneralresearch-3b4059135be47f7752a08e4277a85f9e57ceaa9d.tar.gz
generalresearch-3b4059135be47f7752a08e4277a85f9e57ceaa9d.zip
Ruff typing from this morning. WIP
Diffstat (limited to 'tests/managers/thl/test_ledger')
-rw-r--r--tests/managers/thl/test_ledger/test_lm_accounts.py23
-rw-r--r--tests/managers/thl/test_ledger/test_lm_tx.py142
-rw-r--r--tests/managers/thl/test_ledger/test_lm_tx_entries.py24
-rw-r--r--tests/managers/thl/test_ledger/test_lm_tx_locks.py208
-rw-r--r--tests/managers/thl/test_ledger/test_lm_tx_metadata.py40
-rw-r--r--tests/managers/thl/test_ledger/test_thl_lm_accounts.py310
-rw-r--r--tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py234
-rw-r--r--tests/managers/thl/test_ledger/test_thl_lm_tx.py1004
-rw-r--r--tests/managers/thl/test_ledger/test_thl_lm_tx__user_payouts.py376
-rw-r--r--tests/managers/thl/test_ledger/test_thl_pem.py124
-rw-r--r--tests/managers/thl/test_ledger/test_user_txs.py72
-rw-r--r--tests/managers/thl/test_ledger/test_wallet.py36
12 files changed, 1523 insertions, 1070 deletions
diff --git a/tests/managers/thl/test_ledger/test_lm_accounts.py b/tests/managers/thl/test_ledger/test_lm_accounts.py
index 11b2835..540bea8 100644
--- a/tests/managers/thl/test_ledger/test_lm_accounts.py
+++ b/tests/managers/thl/test_ledger/test_lm_accounts.py
@@ -1,9 +1,11 @@
+from __future__ import annotations
+
from itertools import product as iproduct
from random import randint
-from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
+from pydantic import PositiveInt
from generalresearch.currency import LedgerCurrency
from generalresearch.managers.base import Permission
@@ -11,26 +13,13 @@ from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerAccountDoesntExistError,
)
from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.models.custom_types import AccountType, Direction, UUIDStr
from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
LedgerAccount,
LedgerEntry,
+ LedgerTransaction,
)
-if TYPE_CHECKING:
- from pydantic import PositiveInt
-
- from generalresearch.currency import LedgerCurrency
- from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
- from generalresearch.models.custom_types import AccountType, Direction, UUIDStr
- from generalresearch.models.thl import Direction
- from generalresearch.models.thl.ledger import (
- AccountType,
- LedgerAccount,
- LedgerTransaction,
- )
-
@pytest.mark.parametrize(
argnames="currency, kind, acct_id",
@@ -55,7 +44,7 @@ class TestLedgerAccountManagerNoResults:
we either get the expected None result or it raises the correct
exception
"""
- qn = ":".join([currency, kind, acct_id])
+ qn = f"{currency}:{kind}:{acct_id}"
# (1) .get_account is just a wrapper for .get_account_many_ but
# call it either way
diff --git a/tests/managers/thl/test_ledger/test_lm_tx.py b/tests/managers/thl/test_ledger/test_lm_tx.py
index 37b7ba3..13495a7 100644
--- a/tests/managers/thl/test_ledger/test_lm_tx.py
+++ b/tests/managers/thl/test_ledger/test_lm_tx.py
@@ -1,3 +1,5 @@
+from __future__ import annotations
+
from decimal import Decimal
from random import randint
from uuid import uuid4
@@ -5,9 +7,12 @@ from uuid import uuid4
import pytest
from generalresearch.currency import LedgerCurrency
-from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.managers.thl.ledger_manager.ledger import (
+ LedgerManager,
+)
from generalresearch.models.thl.ledger import (
Direction,
+ LedgerAccount,
LedgerEntry,
LedgerTransaction,
)
@@ -15,7 +20,7 @@ from generalresearch.models.thl.ledger import (
class TestLedgerManagerCreateTx:
- def test_create_account_error_permission(self, lm):
+ def test_create_account_error_permission(self, ledger_manager: LedgerManager):
"""Confirm that the Permission values that are set on the Ledger Manger
allow the Creation action to occur.
"""
@@ -23,11 +28,11 @@ class TestLedgerManagerCreateTx:
# (1) With no Permissions defined
test_lm = LedgerManager(
- pg_config=lm.pg_config,
+ pg_config=ledger_manager.pg_config,
permissions=[],
- redis_config=lm.redis_config,
- cache_prefix=lm.cache_prefix,
- testing=lm.testing,
+ redis_config=ledger_manager.redis_config,
+ cache_prefix=ledger_manager.cache_prefix,
+ testing=ledger_manager.testing,
)
with pytest.raises(expected_exception=AssertionError) as excinfo:
@@ -37,9 +42,14 @@ class TestLedgerManagerCreateTx:
== "LedgerTransactionManager has insufficient Permissions"
)
- def test_create_assertions(self, ledger_account_debit, ledger_account_credit, lm):
+ def test_create_assertions(
+ self,
+ ledger_account_debit: LedgerAccount,
+ ledger_account_credit: LedgerAccount,
+ ledger_manager: LedgerManager,
+ ):
with pytest.raises(expected_exception=ValueError) as excinfo:
- lm.create_tx(
+ ledger_manager.create_tx(
entries=[
{
"direction": Direction.CREDIT,
@@ -53,7 +63,12 @@ class TestLedgerManagerCreateTx:
in str(excinfo.value)
)
- def test_create(self, ledger_account_credit, ledger_account_debit, lm):
+ def test_create(
+ self,
+ ledger_account_credit: LedgerAccount,
+ ledger_account_debit: LedgerAccount,
+ ledger_manager: LedgerManager,
+ ):
amount = int(Decimal("1.00") * 100)
entries = [
@@ -70,15 +85,20 @@ class TestLedgerManagerCreateTx:
]
# Create a Transaction and validate the operation was successful
- tx = lm.create_tx(entries=entries)
+ tx = ledger_manager.create_tx(entries=entries)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert isinstance(res, LedgerTransaction)
assert len(res.entries) == 2
assert tx.id == res.id
- def test_create_and_reverse(self, ledger_account_credit, ledger_account_debit, lm):
+ def test_create_and_reverse(
+ self,
+ ledger_account_credit: LedgerAccount,
+ ledger_account_debit: LedgerAccount,
+ ledger_manager: LedgerManager,
+ ):
amount = int(Decimal("1.00") * 100)
entries = [
@@ -94,13 +114,13 @@ class TestLedgerManagerCreateTx:
),
]
- tx = lm.create_tx(entries=entries)
- res = lm.get_tx_by_id(transaction_id=tx.id)
- assert res.id == tx.id
+ tx = ledger_manager.create_tx(entries=entries)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
+ assert ledger_manager.id == tx.id
- assert lm.get_account_balance(account=ledger_account_credit) == 100
- assert lm.get_account_balance(account=ledger_account_debit) == 100
- assert lm.check_ledger_balanced() is True
+ assert ledger_manager.get_account_balance(account=ledger_account_credit) == 100
+ assert ledger_manager.get_account_balance(account=ledger_account_debit) == 100
+ assert ledger_manager.check_ledger_balanced() is True
# Reverse it
entries = [
@@ -116,13 +136,13 @@ class TestLedgerManagerCreateTx:
),
]
- tx = lm.create_tx(entries=entries)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ tx = ledger_manager.create_tx(entries=entries)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.id == tx.id
- assert lm.get_account_balance(ledger_account_credit) == 0
- assert lm.get_account_balance(ledger_account_debit) == 0
- assert lm.check_ledger_balanced()
+ assert ledger_manager.get_account_balance(ledger_account_credit) == 0
+ assert ledger_manager.get_account_balance(ledger_account_debit) == 0
+ assert ledger_manager.check_ledger_balanced()
# subtract again
entries = [
@@ -137,52 +157,60 @@ class TestLedgerManagerCreateTx:
amount=amount,
),
]
- tx = lm.create_tx(entries=entries)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ tx = ledger_manager.create_tx(entries=entries)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.id == tx.id
- assert lm.get_account_balance(ledger_account_credit) == -100
- assert lm.get_account_balance(ledger_account_debit) == -100
- assert lm.check_ledger_balanced()
+ assert ledger_manager.get_account_balance(ledger_account_credit) == -100
+ assert ledger_manager.get_account_balance(ledger_account_debit) == -100
+ assert ledger_manager.check_ledger_balanced()
class TestLedgerManagerGetTx:
# @pytest.mark.parametrize("currency", [LedgerCurrency.TEST], indirect=True)
- def test_get_tx_by_id(self, ledger_tx, lm):
+ def test_get_tx_by_id(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
with pytest.raises(expected_exception=AssertionError):
- lm.get_tx_by_id(transaction_id=ledger_tx)
+ ledger_manager.get_tx_by_id(transaction_id=ledger_tx)
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert res.id == ledger_tx.id
# @pytest.mark.parametrize("currency", [LedgerCurrency.TEST], indirect=True)
- def test_get_tx_by_ids(self, ledger_tx, lm):
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ def test_get_tx_by_ids(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert res.id == ledger_tx.id
@pytest.mark.parametrize(
"tag", [f"{LedgerCurrency.TEST}:{uuid4().hex}"], indirect=True
)
- def test_get_tx_ids_by_tag(self, ledger_tx, tag, lm):
+ def test_get_tx_ids_by_tag(
+ self, ledger_tx: LedgerTransaction, tag: str, ledger_manager: LedgerManager
+ ):
# (1) search for a random tag
- res = lm.get_tx_ids_by_tag(tag="aaa:bbb")
+ res = ledger_manager.get_tx_ids_by_tag(tag="aaa:bbb")
assert isinstance(res, set)
assert len(res) == 0
# (2) search for the tag that was used during ledger_transaction creation
- res = lm.get_tx_ids_by_tag(tag=tag)
+ res = ledger_manager.get_tx_ids_by_tag(tag=tag)
assert isinstance(res, set)
assert len(res) == 1
- def test_get_tx_by_tag(self, ledger_tx, tag, lm):
+ def test_get_tx_by_tag(
+ self, ledger_tx: LedgerTransaction, tag: str, ledger_manager: LedgerManager
+ ):
# (1) search for a random tag
- res = lm.get_tx_by_tag(tag="aaa:bbb")
+ res = ledger_manager.get_tx_by_tag(tag="aaa:bbb")
assert isinstance(res, list)
assert len(res) == 0
# (2) search for the tag that was used during ledger_transaction creation
- res = lm.get_tx_by_tag(tag=tag)
+ res = ledger_manager.get_tx_by_tag(tag=tag)
assert isinstance(res, list)
assert len(res) == 1
@@ -190,42 +218,60 @@ class TestLedgerManagerGetTx:
assert ledger_tx.id == res[0].id
def test_get_tx_filtered_by_account(
- self, ledger_tx, ledger_account, ledger_account_debit, ledger_account_credit, lm
+ self,
+ ledger_tx: LedgerTransaction,
+ ledger_account: LedgerAccount,
+ ledger_account_debit: LedgerAccount,
+ ledger_account_credit: LedgerAccount,
+ ledger_manager: LedgerManager,
):
# (1) Do basic assertion checks first
with pytest.raises(expected_exception=AssertionError) as excinfo:
- lm.get_tx_filtered_by_account(account_uuid=ledger_account)
+ ledger_manager.get_tx_filtered_by_account(account_uuid=ledger_account)
assert str(excinfo.value) == "account_uuid must be a str"
# (2) This search doesn't return anything because this ledger account
# wasn't actually used in the entries for the ledger_transaction
- res = lm.get_tx_filtered_by_account(account_uuid=ledger_account.uuid)
+ res = ledger_manager.get_tx_filtered_by_account(
+ account_uuid=ledger_account.uuid
+ )
assert len(res) == 0
# (3) Either the credit or the debit example ledger_accounts wll work
# to find this transaction because they're both used in the entries
- res = lm.get_tx_filtered_by_account(account_uuid=ledger_account_debit.uuid)
+ res = ledger_manager.get_tx_filtered_by_account(
+ account_uuid=ledger_account_debit.uuid
+ )
assert len(res) == 1
assert res[0].id == ledger_tx.id
- res = lm.get_tx_filtered_by_account(account_uuid=ledger_account_credit.uuid)
+ res = ledger_manager.get_tx_filtered_by_account(
+ account_uuid=ledger_account_credit.uuid
+ )
assert len(res) == 1
assert ledger_tx.id == res[0].id
- res2 = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res2 = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert res2.model_dump_json() == res[0].model_dump_json()
- def test_filter_metadata(self, ledger_tx, tx_metadata, lm):
+ def test_filter_metadata(
+ self,
+ ledger_tx: LedgerTransaction,
+ tx_metadata: dict[str, str] | None,
+ ledger_manager: LedgerManager,
+ ):
key, value = next(iter(tx_metadata.items()))
# (1) Confirm a random key,value pair returns nothing
- res = lm.get_tx_filtered_by_metadata(
+ res = ledger_manager.get_tx_filtered_by_metadata(
metadata_key=f"key-{uuid4().hex[:10]}", metadata_value=uuid4().hex[:12]
)
assert len(res) == 0
# (2) confirm a key,value pair return the correct results
- res = lm.get_tx_filtered_by_metadata(metadata_key=key, metadata_value=value)
+ res = ledger_manager.get_tx_filtered_by_metadata(
+ metadata_key=key, metadata_value=value
+ )
assert len(res) == 1
# assert 0 == THL_lm.get_filtered_account_balance(account2, "thl_wall", "ccc")
diff --git a/tests/managers/thl/test_ledger/test_lm_tx_entries.py b/tests/managers/thl/test_ledger/test_lm_tx_entries.py
index 5bf1c48..9925b87 100644
--- a/tests/managers/thl/test_ledger/test_lm_tx_entries.py
+++ b/tests/managers/thl/test_ledger/test_lm_tx_entries.py
@@ -1,25 +1,35 @@
-from generalresearch.models.thl.ledger import LedgerEntry
+from __future__ import annotations
+
+from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.models.thl.ledger import (
+ LedgerEntry,
+ LedgerTransaction,
+)
class TestLedgerEntryManager:
- def test_get_tx_entries_by_tx(self, ledger_tx, lm):
+ def test_get_tx_entries_by_tx(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
# First confirm the Ledger TX exists with 2 Entries
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert len(res.entries) == 2
- tx_entries = lm.get_tx_entries_by_tx(transaction=ledger_tx)
+ tx_entries = ledger_manager.get_tx_entries_by_tx(transaction=ledger_tx)
assert len(tx_entries) == 2
assert res.entries == tx_entries
assert isinstance(tx_entries[0], LedgerEntry)
- def test_get_tx_entries_by_txs(self, ledger_tx, lm):
+ def test_get_tx_entries_by_txs(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
# First confirm the Ledger TX exists with 2 Entries
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert len(res.entries) == 2
- tx_entries = lm.get_tx_entries_by_txs(transactions=[ledger_tx])
+ tx_entries = ledger_manager.get_tx_entries_by_txs(transactions=[ledger_tx])
assert len(tx_entries) == 2
assert res.entries == tx_entries
diff --git a/tests/managers/thl/test_ledger/test_lm_tx_locks.py b/tests/managers/thl/test_ledger/test_lm_tx_locks.py
index 020b74a..9158e15 100644
--- a/tests/managers/thl/test_ledger/test_lm_tx_locks.py
+++ b/tests/managers/thl/test_ledger/test_lm_tx_locks.py
@@ -1,3 +1,5 @@
+from __future__ import annotations
+
import logging
from collections.abc import Callable
from datetime import UTC, datetime, timedelta
@@ -5,6 +7,7 @@ from decimal import Decimal
import pytest
+from generalresearch.currency import LedgerCurrency
from generalresearch.managers.thl.ledger_manager.conditions import (
generate_condition_mp_payment,
)
@@ -13,8 +16,11 @@ from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerTransactionCreateLockError,
LedgerTransactionFlagAlreadyExistsError,
)
+from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
from generalresearch.models import Source
from generalresearch.models.thl.ledger import LedgerTransaction
+from generalresearch.models.thl.product import Product
from generalresearch.models.thl.session import (
Session,
Status,
@@ -31,17 +37,17 @@ class TestLedgerLocks:
def test_a(
self,
- user_factory,
- session_factory,
- product_user_wallet_no,
- create_main_accounts,
+ user_factory: Callable[..., User],
+ session_factory: Callable[..., Session],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
caplog,
- thl_lm,
- lm,
- utc_hour_ago,
- currency,
- wall_factory,
- delete_ledger_db,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ utc_hour_ago: datetime,
+ currency: LedgerCurrency,
+ wall_factory: Callable[..., Wall],
+ delete_ledger_db: Callable[..., None],
):
"""
TODO: This whole test is confusing a I don't really understand.
@@ -61,12 +67,16 @@ class TestLedgerLocks:
# A User does a Wall Completion in Session=1
w1 = s1.wall_events[0]
- tx = thl_lm.create_tx_task_complete(wall=w1, user=user, created=w1.started)
+ tx = thl_ledger_manager.create_tx_task_complete(
+ wall=w1, user=user, created=w1.started
+ )
assert isinstance(tx, LedgerTransaction)
# A User does another Wall Completion in Session=1
w2 = s1.wall_events[1]
- tx = thl_lm.create_tx_task_complete(wall=w2, user=user, created=w2.started)
+ tx = thl_ledger_manager.create_tx_task_complete(
+ wall=w2, user=user, created=w2.started
+ )
assert isinstance(tx, LedgerTransaction)
# That first Wall Complete was "adjusted" to instead be marked
@@ -76,7 +86,7 @@ class TestLedgerLocks:
adjusted_cpi=0,
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
- tx = thl_lm.create_tx_task_adjustment(wall=w1, user=user)
+ tx = thl_ledger_manager.create_tx_task_adjustment(wall=w1, user=user)
assert isinstance(tx, LedgerTransaction)
# A User does another! Wall Completion in Session=1; however, we
@@ -85,55 +95,57 @@ class TestLedgerLocks:
# Make sure we clear any flags/locks first
lock_key = f"{currency.value}:thl_wall:{w3.uuid}"
- lock_name = f"{lm.cache_prefix}:transaction_lock:{lock_key}"
- flag_name = f"{lm.cache_prefix}:transaction_flag:{lock_key}"
- lm.redis_client.delete(lock_name)
- lm.redis_client.delete(flag_name)
+ lock_name = f"{ledger_manager.cache_prefix}:transaction_lock:{lock_key}"
+ flag_name = f"{ledger_manager.cache_prefix}:transaction_flag:{lock_key}"
+ ledger_manager.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(flag_name)
# Despite the
f1 = generate_condition_mp_payment(wall=w1)
f2 = generate_condition_mp_payment(wall=w2)
f3 = generate_condition_mp_payment(wall=w3)
- assert f1(lm=lm) is False
- assert f2(lm=lm) is False
- assert f3(lm=lm) is True
+ assert f1(ledger_manager) is False
+ assert f2(lm=ledger_manager) is False
+ assert f3(lm=ledger_manager) is True
condition = f3
- create_tx_func = lambda: thl_lm.create_tx_task_complete_(wall=w3, user=user)
+ create_tx_func = lambda: thl_ledger_manager.create_tx_task_complete_(
+ wall=w3, user=user
+ )
assert isinstance(create_tx_func, Callable)
- assert f3(lm) is True
+ assert f3(ledger_manager) is True
- lm.redis_client.delete(flag_name)
- lm.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(flag_name)
+ ledger_manager.redis_client.delete(lock_name)
- tx = thl_lm.create_tx_protected(
+ tx = thl_ledger_manager.create_tx_protected(
lock_key=lock_key, condition=condition, create_tx_func=create_tx_func
)
- assert f3(lm) is False
+ assert f3(ledger_manager) is False
# purposely hold the lock open
tx = None
- lm.redis_client.set(lock_name, "1")
+ ledger_manager.redis_client.set(lock_name, "1")
with caplog.at_level(logging.ERROR):
with pytest.raises(expected_exception=LedgerTransactionCreateLockError):
- tx = thl_lm.create_tx_protected(
+ tx = thl_ledger_manager.create_tx_protected(
lock_key=lock_key,
condition=condition,
create_tx_func=create_tx_func,
)
assert tx is None
assert "Unable to acquire lock within the time specified" in caplog.text
- lm.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(lock_name)
def test_locking(
self,
- user_factory,
- product_user_wallet_no,
- create_main_accounts,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
caplog,
- thl_lm,
- lm,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
delete_ledger_db()
create_main_accounts()
@@ -154,7 +166,9 @@ class TestLedgerLocks:
started=now,
finished=now + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall1, user=user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall1, user=user, created=wall1.started
+ )
# A User does a Wall complete on Session.id=1 and the transaction is
# logged to the ledger
@@ -169,7 +183,9 @@ class TestLedgerLocks:
started=now,
finished=now + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall2, user=user, created=wall2.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall2, user=user, created=wall2.started
+ )
# An hour later, the first wall complete is adjusted to a Failure and
# it's tracked in the ledger
@@ -178,7 +194,7 @@ class TestLedgerLocks:
adjusted_cpi=0,
adjusted_timestamp=now + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall=wall1, user=user)
+ thl_ledger_manager.create_tx_task_adjustment(wall=wall1, user=user)
# A User does a Wall complete on Session.id=1 and the transaction
# IS NOT logged to the ledger
@@ -195,52 +211,52 @@ class TestLedgerLocks:
uuid="867a282d8b4d40d2a2093d75b802b629",
)
- revenue_account = thl_lm.get_account_task_complete_revenue()
- assert 0 == thl_lm.get_account_filtered_balance(
+ revenue_account = thl_ledger_manager.get_account_task_complete_revenue()
+ assert 0 == thl_ledger_manager.get_account_filtered_balance(
account=revenue_account,
metadata_key="thl_wall",
metadata_value=wall3.uuid,
)
# Make sure we clear any flags/locks first
lock_key = f"test:thl_wall:{wall3.uuid}"
- lock_name = f"{lm.cache_prefix}:transaction_lock:{lock_key}"
- flag_name = f"{lm.cache_prefix}:transaction_flag:{lock_key}"
- lm.redis_client.delete(lock_name)
- lm.redis_client.delete(flag_name)
+ lock_name = f"{ledger_manager.cache_prefix}:transaction_lock:{lock_key}"
+ flag_name = f"{ledger_manager.cache_prefix}:transaction_flag:{lock_key}"
+ ledger_manager.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(flag_name)
# Purposely hold the lock open
- lm.redis_client.set(name=lock_name, value="1")
+ ledger_manager.redis_client.set(name=lock_name, value="1")
with caplog.at_level(logging.DEBUG):
with pytest.raises(expected_exception=LedgerTransactionCreateLockError):
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall3, user=user, created=wall3.started
)
assert isinstance(tx, LedgerTransaction)
assert "Unable to acquire lock within the time specified" in caplog.text
# Release the lock
- lm.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(lock_name)
# Set the redis flag to indicate it has been run
- lm.redis_client.set(flag_name, "1")
+ ledger_manager.redis_client.set(flag_name, "1")
# with self.assertLogs(logger=logger, level=logging.DEBUG) as cm2:
with pytest.raises(expected_exception=LedgerTransactionFlagAlreadyExistsError):
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall3, user=user, created=wall3.started
)
# self.assertIn("entered_lock: True, flag_set: True", cm2.output[0])
# Unset the flag
- lm.redis_client.delete(flag_name)
+ ledger_manager.redis_client.delete(flag_name)
- assert 0 == lm.get_account_filtered_balance(
+ assert 0 == ledger_manager.get_account_filtered_balance(
account=revenue_account,
metadata_key="thl_wall",
metadata_value=wall3.uuid,
)
# Now actually run it
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall3, user=user, created=wall3.started
)
assert tx is not None
@@ -249,29 +265,34 @@ class TestLedgerLocks:
# Confirm the Exception inheritance works
tx = None
with pytest.raises(expected_exception=LedgerTransactionCreateError):
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall3, user=user, created=wall3.started
)
assert tx is None
# clear the redis flag, it should query the db
- assert lm.redis_client.get(flag_name) is not None
- lm.redis_client.delete(flag_name)
- assert lm.redis_client.get(flag_name) is None
+ assert ledger_manager.redis_client.get(flag_name) is not None
+ ledger_manager.redis_client.delete(flag_name)
+ assert ledger_manager.redis_client.get(flag_name) is None
with pytest.raises(expected_exception=LedgerTransactionCreateError):
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall3, user=user, created=wall3.started
)
- assert 400 == thl_lm.get_account_filtered_balance(
+ assert 400 == thl_ledger_manager.get_account_filtered_balance(
account=revenue_account,
metadata_key="thl_wall",
metadata_value=wall3.uuid,
)
def test_bp_payment_without_locks(
- self, user_factory, product_user_wallet_no, create_main_accounts, thl_lm, lm
+ self,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
user: User = user_factory(product=product_user_wallet_no)
wall1 = Wall(
@@ -286,29 +307,42 @@ class TestLedgerLocks:
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall1, user=user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall1, user=user, created=wall1.started
+ )
session = Session(started=wall1.started, user=user, wall_events=[wall1])
status, status_code_1 = session.determine_session_status()
thl_net, commission_amount, bp_pay, user_pay = session.determine_payments()
session.update(
- status=status, status_code_1=status_code_1, finished=session.started + timedelta(minutes=10), payout=bp_pay, user_payout=user_pay
+ status=status,
+ status_code_1=status_code_1,
+ finished=session.started + timedelta(minutes=10),
+ payout=bp_pay,
+ user_payout=user_pay,
)
print(thl_net, commission_amount, bp_pay, user_pay)
# Run it 3 times without any checks, and it gets made three times!
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
- thl_lm.create_tx_bp_payment_(session=session, created=wall1.started)
- thl_lm.create_tx_bp_payment_(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment_(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment_(session=session, created=wall1.started)
- bp_wallet = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- assert 48 * 3 == lm.get_account_balance(account=bp_wallet)
- assert 48 * 3 == thl_lm.get_account_filtered_balance(
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ assert 48 * 3 == ledger_manager.get_account_balance(account=bp_wallet)
+ assert 48 * 3 == thl_ledger_manager.get_account_filtered_balance(
account=bp_wallet, metadata_key="thl_session", metadata_value=session.uuid
)
- assert lm.check_ledger_balanced()
+ assert ledger_manager.check_ledger_balanced()
def test_bp_payment_with_locks(
- self, user_factory, product_user_wallet_no, create_main_accounts, thl_lm, lm
+ self,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
user: User = user_factory(product=product_user_wallet_no)
@@ -324,35 +358,45 @@ class TestLedgerLocks:
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall1, user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(wall1, user, created=wall1.started)
session = Session(started=wall1.started, user=user, wall_events=[wall1])
status, status_code_1 = session.determine_session_status()
thl_net, commission_amount, bp_pay, user_pay = session.determine_payments()
session.update(
- status=status, status_code_1=status_code_1, finished=session.started + timedelta(minutes=10), payout=bp_pay, user_payout=user_pay
+ status=status,
+ status_code_1=status_code_1,
+ finished=session.started + timedelta(minutes=10),
+ payout=bp_pay,
+ user_payout=user_pay,
)
print(thl_net, commission_amount, bp_pay, user_pay)
# Make sure we clear any flags/locks first
lock_key = f"test:thl_wall:{wall1.uuid}"
- lock_name = f"{lm.cache_prefix}:transaction_lock:{lock_key}"
- flag_name = f"{lm.cache_prefix}:transaction_flag:{lock_key}"
- lm.redis_client.delete(lock_name)
- lm.redis_client.delete(flag_name)
+ lock_name = f"{ledger_manager.cache_prefix}:transaction_lock:{lock_key}"
+ flag_name = f"{ledger_manager.cache_prefix}:transaction_flag:{lock_key}"
+ ledger_manager.redis_client.delete(lock_name)
+ ledger_manager.redis_client.delete(flag_name)
# Run it 3 times with check, and it gets made once!
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=session, created=wall1.started)
with pytest.raises(expected_exception=LedgerTransactionCreateError):
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(
+ session=session, created=wall1.started
+ )
with pytest.raises(expected_exception=LedgerTransactionCreateError):
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(
+ session=session, created=wall1.started
+ )
- bp_wallet = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- assert 48 == thl_lm.get_account_balance(bp_wallet)
- assert 48 == thl_lm.get_account_filtered_balance(
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ assert 48 == thl_ledger_manager.get_account_balance(bp_wallet)
+ assert 48 == thl_ledger_manager.get_account_filtered_balance(
account=bp_wallet,
metadata_key="thl_session",
metadata_value=session.uuid,
)
- assert lm.check_ledger_balanced()
+ assert ledger_manager.check_ledger_balanced()
diff --git a/tests/managers/thl/test_ledger/test_lm_tx_metadata.py b/tests/managers/thl/test_ledger/test_lm_tx_metadata.py
index 5d12633..f63efa4 100644
--- a/tests/managers/thl/test_ledger/test_lm_tx_metadata.py
+++ b/tests/managers/thl/test_ledger/test_lm_tx_metadata.py
@@ -1,34 +1,52 @@
+from __future__ import annotations
+
+from generalresearch.managers.thl.ledger_manager.ledger import (
+ LedgerManager,
+ LedgerTransaction,
+)
+
+
class TestLedgerMetadataManager:
- def test_get_tx_metadata_by_txs(self, ledger_tx, lm):
+ def test_get_tx_metadata_by_txs(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
# First confirm the Ledger TX exists with 2 Entries
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
assert isinstance(res.metadata, dict)
- tx_metadatas = lm.get_tx_metadata_by_txs(transactions=[ledger_tx])
+ tx_metadatas = ledger_manager.get_tx_metadata_by_txs(transactions=[ledger_tx])
assert isinstance(tx_metadatas, dict)
assert isinstance(tx_metadatas[ledger_tx.id], dict)
assert res.metadata == tx_metadatas[ledger_tx.id]
- def test_get_tx_metadata_ids_by_tx(self, ledger_tx, lm):
+ def test_get_tx_metadata_ids_by_tx(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
# First confirm the Ledger TX exists with 2 Entries
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
tx_metadata_cnt = len(res.metadata.keys())
- tx_metadata_ids = lm.get_tx_metadata_ids_by_tx(transaction=ledger_tx)
+ tx_metadata_ids = ledger_manager.get_tx_metadata_ids_by_tx(
+ transaction=ledger_tx
+ )
assert isinstance(tx_metadata_ids, set)
- assert isinstance(list(tx_metadata_ids)[0], int)
+ assert isinstance(next(iter(tx_metadata_ids)), int)
assert tx_metadata_cnt == len(tx_metadata_ids)
- def test_get_tx_metadata_ids_by_txs(self, ledger_tx, lm):
+ def test_get_tx_metadata_ids_by_txs(
+ self, ledger_tx: LedgerTransaction, ledger_manager: LedgerManager
+ ):
# First confirm the Ledger TX exists with 2 Entries
- res = lm.get_tx_by_id(transaction_id=ledger_tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=ledger_tx.id)
tx_metadata_cnt = len(res.metadata.keys())
- tx_metadata_ids = lm.get_tx_metadata_ids_by_txs(transactions=[ledger_tx])
+ tx_metadata_ids = ledger_manager.get_tx_metadata_ids_by_txs(
+ transactions=[ledger_tx]
+ )
assert isinstance(tx_metadata_ids, set)
- assert isinstance(list(tx_metadata_ids)[0], int)
+ assert isinstance(next(iter(tx_metadata_ids)), int)
assert tx_metadata_cnt == len(tx_metadata_ids)
diff --git a/tests/managers/thl/test_ledger/test_thl_lm_accounts.py b/tests/managers/thl/test_ledger/test_thl_lm_accounts.py
index 8d7d828..dce9116 100644
--- a/tests/managers/thl/test_ledger/test_thl_lm_accounts.py
+++ b/tests/managers/thl/test_ledger/test_thl_lm_accounts.py
@@ -1,19 +1,38 @@
+from __future__ import annotations
+
+from collections.abc import Callable
from uuid import uuid4
import pytest
+from generalresearch.currency import LedgerCurrency
+from generalresearch.managers.thl.ledger_manager.exceptions import (
+ LedgerAccountDoesntExistError,
+)
+from generalresearch.managers.thl.ledger_manager.ledger import (
+ LedgerAccountManager,
+ LedgerManager,
+)
+from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+from generalresearch.models.thl.ledger import (
+ AccountType,
+ Direction,
+ LedgerAccount,
+)
+from generalresearch.models.thl.product import Product
+from generalresearch.models.thl.user import User
+
class TestThlLedgerManagerAccounts:
- def test_get_account_or_create_user_wallet(self, user, thl_lm, lm):
- from generalresearch.currency import LedgerCurrency
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- LedgerAccount,
- )
+ def test_get_account_or_create_user_wallet(
+ self,
+ user: User,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
- account = thl_lm.get_account_or_create_user_wallet(user=user)
+ account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
assert isinstance(account, LedgerAccount)
assert user.uuid in account.qualified_name
@@ -25,18 +44,20 @@ class TestThlLedgerManagerAccounts:
assert account.currency == LedgerCurrency.TEST
# Actually query for it to confirm
- res = lm.get_account(qualified_name=account.qualified_name, raise_on_error=True)
+ res = ledger_manager.get_account(
+ qualified_name=account.qualified_name, raise_on_error=True
+ )
+ assert isinstance(res, LedgerAccount)
assert res.model_dump_json() == account.model_dump_json()
- def test_get_account_or_create_bp_wallet(self, product, thl_lm, lm):
- from generalresearch.currency import LedgerCurrency
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- LedgerAccount,
- )
+ def test_get_account_or_create_bp_wallet(
+ self,
+ product: Product,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
- account = thl_lm.get_account_or_create_bp_wallet(product=product)
+ account = thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
assert isinstance(account, LedgerAccount)
assert product.uuid in account.qualified_name
@@ -48,17 +69,22 @@ class TestThlLedgerManagerAccounts:
assert account.currency == LedgerCurrency.TEST
# Actually query for it to confirm
- res = lm.get_account(qualified_name=account.qualified_name, raise_on_error=True)
+ res = ledger_manager.get_account(
+ qualified_name=account.qualified_name, raise_on_error=True
+ )
+ assert isinstance(res, LedgerAccount)
assert res.model_dump_json() == account.model_dump_json()
- def test_get_account_or_create_bp_commission(self, product, thl_lm, lm):
- from generalresearch.currency import LedgerCurrency
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- )
+ def test_get_account_or_create_bp_commission(
+ self,
+ product: Product,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
- account = thl_lm.get_account_or_create_bp_commission(product=product)
+ account = thl_ledger_manager.get_account_or_create_bp_commission(
+ product=product
+ )
assert product.uuid in account.qualified_name
assert account.display_name == f"Revenue from commission {product.uuid}"
@@ -69,18 +95,21 @@ class TestThlLedgerManagerAccounts:
assert account.currency == LedgerCurrency.TEST
# Actually query for it to confirm
- res = lm.get_account(qualified_name=account.qualified_name, raise_on_error=True)
+ res = ledger_manager.get_account(
+ qualified_name=account.qualified_name, raise_on_error=True
+ )
+ assert isinstance(res, LedgerAccount)
assert res.model_dump_json() == account.model_dump_json()
@pytest.mark.parametrize("expense", ["tango", "paypal", "gift", "tremendous"])
- def test_get_account_or_create_bp_expense(self, product, expense, thl_lm, lm):
- from generalresearch.currency import LedgerCurrency
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- )
-
- account = thl_lm.get_account_or_create_bp_expense(
+ def test_get_account_or_create_bp_expense(
+ self,
+ product: Product,
+ expense,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
+ account = thl_ledger_manager.get_account_or_create_bp_expense(
product=product, expense_name=expense
)
assert product.uuid in account.qualified_name
@@ -92,17 +121,22 @@ class TestThlLedgerManagerAccounts:
assert account.currency == LedgerCurrency.TEST
# Actually query for it to confirm
- res = lm.get_account(qualified_name=account.qualified_name, raise_on_error=True)
+ res = ledger_manager.get_account(
+ qualified_name=account.qualified_name, raise_on_error=True
+ )
+ assert isinstance(res, LedgerAccount)
assert res.model_dump_json() == account.model_dump_json()
- def test_get_or_create_bp_pending_payout_account(self, product, thl_lm, lm):
- from generalresearch.currency import LedgerCurrency
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- )
+ def test_get_or_create_bp_pending_payout_account(
+ self,
+ product: Product,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
- account = thl_lm.get_or_create_bp_pending_payout_account(product=product)
+ account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
+ product=product
+ )
assert product.uuid in account.qualified_name
assert account.display_name == f"BP Wallet Pending {product.uuid}"
@@ -113,11 +147,17 @@ class TestThlLedgerManagerAccounts:
assert account.currency == LedgerCurrency.TEST
# Actually query for it to confirm
- res = lm.get_account(qualified_name=account.qualified_name, raise_on_error=True)
+ res = ledger_manager.get_account(
+ qualified_name=account.qualified_name, raise_on_error=True
+ )
+ assert isinstance(res, LedgerAccount)
assert res.model_dump_json() == account.model_dump_json()
def test_get_account_task_complete_revenue_raises(
- self, delete_ledger_db, thl_lm, lm
+ self,
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerAccountDoesntExistError,
@@ -126,63 +166,75 @@ class TestThlLedgerManagerAccounts:
delete_ledger_db()
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
- thl_lm.get_account_task_complete_revenue()
+ thl_ledger_manager.get_account_task_complete_revenue()
def test_get_account_task_complete_revenue(
- self, account_cash, account_revenue_task_complete, thl_lm, lm
+ self,
+ thl_ledger_manager: ThlLedgerManager,
):
from generalresearch.models.thl.ledger import (
AccountType,
LedgerAccount,
)
- res = thl_lm.get_account_task_complete_revenue()
+ res = thl_ledger_manager.get_account_task_complete_revenue()
assert isinstance(res, LedgerAccount)
assert res.reference_type is None
assert res.reference_uuid is None
assert res.account_type == AccountType.REVENUE
assert res.display_name == "Cash flow task complete"
- def test_get_account_cash_raises(self, delete_ledger_db, thl_lm, lm):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
+ def test_get_account_cash_raises(
+ self,
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ):
delete_ledger_db()
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
- thl_lm.get_account_cash()
+ thl_ledger_manager.get_account_cash()
- def test_get_account_cash(self, account_cash, thl_lm, lm):
+ def test_get_account_cash(
+ self,
+ thl_ledger_manager: ThlLedgerManager,
+ ):
from generalresearch.models.thl.ledger import (
AccountType,
LedgerAccount,
)
- res = thl_lm.get_account_cash()
+ res = thl_ledger_manager.get_account_cash()
assert isinstance(res, LedgerAccount)
assert res.reference_type is None
assert res.reference_uuid is None
assert res.account_type == AccountType.CASH
assert res.display_name == "Operating Cash Account"
- def test_get_accounts(self, setup_accounts, product, user_factory, thl_lm, lm, lam):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
- from generalresearch.models.thl.user import User
+ def test_get_accounts(
+ self,
+ setup_accounts: Callable[..., None],
+ product: Product,
+ user_factory: Callable[..., User],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ledger_account_manager: LedgerAccountManager,
+ ):
+ setup_accounts()
- user1: User = user_factory(product=product)
- user2: User = user_factory(product=product)
+ _: User = user_factory(product=product)
+ _: User = user_factory(product=product)
- account1 = thl_lm.get_account_or_create_bp_wallet(product=product)
+ account1 = thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
# (1) known account and confirm it comes back
- res = lm.get_account(qualified_name=account1.qualified_name)
+ res = ledger_manager.get_account(qualified_name=account1.qualified_name)
assert account1.model_dump_json() == res.model_dump_json()
# (2) known accounts and confirm they both come back
- res = lam.get_accounts(qualified_names=[account1.qualified_name])
+ res = ledger_account_manager.get_accounts(
+ qualified_names=[account1.qualified_name]
+ )
assert isinstance(res, list)
assert len(res) == 1
assert account1 in res
@@ -190,28 +242,34 @@ class TestThlLedgerManagerAccounts:
# Get 2 known and 1 made up qualified names, and confirm it raises
# an error
with pytest.raises(LedgerAccountDoesntExistError):
- lam.get_accounts(
+ ledger_account_manager.get_accounts(
qualified_names=[
account1.qualified_name,
f"test:bp_wall:{uuid4().hex}",
]
)
- def test_get_accounts_if_exists(self, product_factory, currency, thl_lm, lm):
- from generalresearch.models.thl.product import Product
+ def test_get_accounts_if_exists(
+ self,
+ product_factory: Callable[..., Product],
+ currency: LedgerCurrency,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
p1: Product = product_factory()
p2: Product = product_factory()
- account1 = thl_lm.get_account_or_create_bp_wallet(product=p1)
- account2 = thl_lm.get_account_or_create_bp_wallet(product=p2)
+ account1 = thl_ledger_manager.get_account_or_create_bp_wallet(product=p1)
+ account2 = thl_ledger_manager.get_account_or_create_bp_wallet(product=p2)
# (1) known account and confirm it comes back
- res = lm.get_account(qualified_name=account1.qualified_name)
+ res = ledger_manager.get_account(qualified_name=account1.qualified_name)
+ assert isinstance(res, LedgerAccount)
assert account1.model_dump_json() == res.model_dump_json()
# (2) known accounts and confirm they both come back
- res = lm.get_accounts(
+ res = ledger_manager.get_accounts(
qualified_names=[account1.qualified_name, account2.qualified_name]
)
assert isinstance(res, list)
@@ -221,7 +279,7 @@ class TestThlLedgerManagerAccounts:
# Get 2 known and 1 made up qualified names, and confirm only 2
# come back
- lm.get_accounts_if_exists(
+ ledger_manager.get_accounts_if_exists(
qualified_names=[
account1.qualified_name,
account2.qualified_name,
@@ -233,53 +291,49 @@ class TestThlLedgerManagerAccounts:
assert len(res) == 2
# Confirm an empty array comes back for all unknown qualified names
- res = lm.get_accounts_if_exists(
+ res = ledger_manager.get_accounts_if_exists(
qualified_names=[
- f"{lm.currency.value}:bp_wall:{uuid4().hex}" for i in range(5)
+ f"{ledger_manager.currency.value}:bp_wall:{uuid4().hex}"
+ for _ in range(5)
]
)
assert isinstance(res, list)
assert len(res) == 0
- def test_get_accounts_for_products(self, product_factory, thl_lm, lm):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
- from generalresearch.models.thl.ledger import (
- LedgerAccount,
- )
-
+ def test_get_accounts_for_products(
+ self,
+ product_factory: Callable[..., Product],
+ thl_ledger_manager: ThlLedgerManager,
+ ):
# Create 5 Products
product_uuids = []
- for i in range(5):
+ for _ in range(5):
_p = product_factory()
product_uuids.append(_p.uuid)
# Confirm that this fails.. because none of those accounts have been
# created yet
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
- thl_lm.get_accounts_bp_wallet_for_products(product_uuids=product_uuids)
+ thl_ledger_manager.get_accounts_bp_wallet_for_products(
+ product_uuids=product_uuids
+ )
# Create the bp_wallet accounts and then try again
for p_uuid in product_uuids:
- thl_lm.get_account_or_create_bp_wallet_by_uuid(product_uuid=p_uuid)
+ thl_ledger_manager.get_account_or_create_bp_wallet_by_uuid(
+ product_uuid=p_uuid
+ )
- res = thl_lm.get_accounts_bp_wallet_for_products(product_uuids=product_uuids)
+ res = thl_ledger_manager.get_accounts_bp_wallet_for_products(
+ product_uuids=product_uuids
+ )
assert len(res) == len(product_uuids)
assert all([isinstance(i, LedgerAccount) for i in res])
class TestLedgerAccountManager:
- def test_get_or_create(self, thl_lm, lm, lam):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- LedgerAccount,
- )
+ def test_get_or_create(self, ledger_account_manager: LedgerAccountManager):
u = uuid4().hex
name = f"test-{u[:8]}"
@@ -306,39 +360,42 @@ class TestLedgerAccountManager:
assert isinstance(instance, LedgerAccount)
assert instance.reference_uuid == u
- def test_get(self, user, thl_lm, lm, lam):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
- from generalresearch.models.thl.ledger import (
- AccountType,
- LedgerAccount,
- )
+ def test_get(
+ self,
+ user: User,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ledger_account_manager: LedgerAccountManager,
+ ):
with pytest.raises(LedgerAccountDoesntExistError):
- lam.get_account(qualified_name=f"test:bp_wallet:{user.product.id}")
+ ledger_account_manager.get_account(
+ qualified_name=f"test:bp_wallet:{user.product.id}"
+ )
- thl_lm.get_account_or_create_bp_wallet(product=user.product)
- account = lam.get_account(qualified_name=f"test:bp_wallet:{user.product.id}")
+ thl_ledger_manager.get_account_or_create_bp_wallet(product=user.product)
+ account = ledger_account_manager.get_account(
+ qualified_name=f"test:bp_wallet:{user.product.id}"
+ )
assert isinstance(account, LedgerAccount)
assert AccountType.BP_WALLET == account.account_type
assert user.product.uuid == account.reference_uuid
- def test_get_many(self, product_factory, thl_lm, lm, lam, currency):
- from generalresearch.managers.thl.ledger_manager.exceptions import (
- LedgerAccountDoesntExistError,
- )
- from generalresearch.models.thl.product import Product
-
+ def test_get_many(
+ self,
+ product_factory: Callable[..., Product],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_account_manager: LedgerAccountManager,
+ ):
p1: Product = product_factory()
p2: Product = product_factory()
- account1 = thl_lm.get_account_or_create_bp_wallet(product=p1)
- account2 = thl_lm.get_account_or_create_bp_wallet(product=p2)
+ account1 = thl_ledger_manager.get_account_or_create_bp_wallet(product=p1)
+ account2 = thl_ledger_manager.get_account_or_create_bp_wallet(product=p2)
# Get 1 known account and confirm it comes back
- res = lam.get_account_many(
+ res = ledger_account_manager.get_account_many(
qualified_names=[account1.qualified_name, account2.qualified_name]
)
assert isinstance(res, list)
@@ -346,7 +403,7 @@ class TestLedgerAccountManager:
assert account1 in res
# Get 2 known accounts and confirm they both come back
- res = lam.get_account_many(
+ res = ledger_account_manager.get_account_many(
qualified_names=[account1.qualified_name, account2.qualified_name]
)
assert isinstance(res, list)
@@ -356,7 +413,7 @@ class TestLedgerAccountManager:
# Get 2 known and 1 made up qualified names, and confirm only 2 come
# back. Don't raise on error, so we can confirm the array is "short"
- res = lam.get_account_many(
+ res = ledger_account_manager.get_account_many(
qualified_names=[
account1.qualified_name,
account2.qualified_name,
@@ -369,7 +426,7 @@ class TestLedgerAccountManager:
# Same as above, but confirm the raise works on checking res length
with pytest.raises(LedgerAccountDoesntExistError):
- lam.get_account_many(
+ ledger_account_manager.get_account_many(
qualified_names=[
account1.qualified_name,
account2.qualified_name,
@@ -379,19 +436,14 @@ class TestLedgerAccountManager:
)
# Confirm an empty array comes back for all unknown qualified names
- res = lam.get_account_many(
- qualified_names=[f"test:bp_wall:{uuid4().hex}" for i in range(5)],
+ res = ledger_account_manager.get_account_many(
+ qualified_names=[f"test:bp_wall:{uuid4().hex}" for _ in range(5)],
raise_on_error=False,
)
assert isinstance(res, list)
assert len(res) == 0
- def test_create_account(self, thl_lm, lm, lam):
- from generalresearch.models.thl.ledger import (
- AccountType,
- Direction,
- LedgerAccount,
- )
+ def test_create_account(self, ledger_account_manager: LedgerAccountManager):
u = uuid4().hex
name = f"test-{u[:8]}"
@@ -406,6 +458,6 @@ class TestLedgerAccountManager:
reference_uuid=u,
)
- lam.create_account(account=account)
- assert lam.get_account(f"test:bp_wallet:{u}") == account
- assert lam.get_account_or_create(account) == account
+ ledger_account_manager.create_account(account=account)
+ assert ledger_account_manager.get_account(f"test:bp_wallet:{u}") == account
+ assert ledger_account_manager.get_account_or_create(account) == account
diff --git a/tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py b/tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py
index cfb8f8f..e4a25a3 100644
--- a/tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py
+++ b/tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py
@@ -1,4 +1,7 @@
+from __future__ import annotations
+
import logging
+from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from decimal import Decimal
from random import randint
@@ -9,7 +12,7 @@ import redis
from pydantic import RedisDsn
from redis.lock import Lock
-from generalresearch.currency import USDCent
+from generalresearch.currency import LedgerCurrency, USDCent
from generalresearch.managers.base import Permission
from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerTransactionConditionFailedError,
@@ -19,9 +22,13 @@ from generalresearch.managers.thl.ledger_manager.exceptions import (
)
from generalresearch.managers.thl.ledger_manager.ledger import LedgerTransaction
from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+from generalresearch.managers.thl.payout import (
+ BrokerageProductPayoutEventManager,
+)
from generalresearch.models import Source
from generalresearch.models.thl.definitions import PayoutStatus
from generalresearch.models.thl.ledger import Direction, TransactionType
+from generalresearch.models.thl.product import Product
from generalresearch.models.thl.session import (
Session,
Status,
@@ -30,6 +37,7 @@ from generalresearch.models.thl.session import (
)
from generalresearch.models.thl.user import User
from generalresearch.models.thl.wallet import PayoutType
+from generalresearch.pg_helper import PostgresConfig
from generalresearch.redis_helper import RedisConfig
@@ -45,12 +53,12 @@ class TestThlLedgerManagerBPPayout:
def test_create_tx_with_bp_payment(
self,
- user_factory,
- product_user_wallet_no,
- create_main_accounts,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
caplog,
- thl_lm,
- delete_ledger_db,
+ thl_ledger_manager: ThlLedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -69,25 +77,29 @@ class TestThlLedgerManagerBPPayout:
started=now,
finished=now + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
assert isinstance(tx, LedgerTransaction)
session = Session(started=wall1.started, user=user, wall_events=[wall1])
status, status_code_1 = session.determine_session_status()
- thl_net, commission_amount, bp_pay, user_pay = session.determine_payments()
+ _, _, bp_pay, user_pay = session.determine_payments()
session.update(
- status=status, status_code_1=status_code_1, finished=now + timedelta(minutes=10), payout=bp_pay, user_payout=user_pay
+ status=status,
+ status_code_1=status_code_1,
+ finished=now + timedelta(minutes=10),
+ payout=bp_pay,
+ user_payout=user_pay,
)
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=session, created=wall1.started)
lock_key = f"test:bp_payout:{user.product.id}"
- flag_name = f"{thl_lm.cache_prefix}:transaction_flag:{lock_key}"
- thl_lm.redis_client.delete(flag_name)
+ flag_name = f"{thl_ledger_manager.cache_prefix}:transaction_flag:{lock_key}"
+ thl_ledger_manager.redis_client.delete(flag_name)
payoutevent_uuid = uuid4().hex
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=user.product,
amount=USDCent(200),
created=now,
@@ -95,7 +107,7 @@ class TestThlLedgerManagerBPPayout:
)
payoutevent_uuid = uuid4().hex
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=user.product,
amount=USDCent(200),
created=now + timedelta(minutes=2),
@@ -103,13 +115,15 @@ class TestThlLedgerManagerBPPayout:
payoutevent_uuid=payoutevent_uuid,
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- assert 170 == thl_lm.get_account_balance(bp_wallet_account)
- assert 200 == thl_lm.get_account_balance(cash)
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ assert 170 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 200 == thl_ledger_manager.get_account_balance(cash)
with pytest.raises(expected_exception=LedgerTransactionFlagAlreadyExistsError):
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
user.product,
amount=USDCent(200),
created=now + timedelta(minutes=2),
@@ -121,7 +135,7 @@ class TestThlLedgerManagerBPPayout:
payoutevent_uuid = uuid4().hex
with caplog.at_level(logging.INFO):
with pytest.raises(LedgerTransactionConditionFailedError):
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
user.product,
amount=USDCent(10_000),
created=now + timedelta(minutes=2),
@@ -131,7 +145,7 @@ class TestThlLedgerManagerBPPayout:
)
assert "failed condition check balance:" in caplog.text
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=user.product,
amount=USDCent(10_00),
created=now + timedelta(minutes=2),
@@ -139,16 +153,22 @@ class TestThlLedgerManagerBPPayout:
skip_wallet_balance_check=True,
payoutevent_uuid=payoutevent_uuid,
)
- assert 170 - 1000 == thl_lm.get_account_balance(bp_wallet_account)
+ assert 170 - 1000 == thl_ledger_manager.get_account_balance(bp_wallet_account)
- def test_create_tx(self, product, caplog, thl_lm, currency):
+ def test_create_tx(
+ self,
+ product: Product,
+ caplog,
+ thl_ledger_manager: ThlLedgerManager,
+ currency: LedgerCurrency,
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
# Create a BP Payout for a Product without any activity. By issuing,
# the skip_* checks, we should be able to force it to work, and will
# then ultimately result in a negative balance
- tx = thl_lm.create_tx_bp_payout(
+ tx = thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -171,15 +191,15 @@ class TestThlLedgerManagerBPPayout:
# Check the Product's balance, it should be negative the amount that was
# paid out. That's because the Product earned nothing.. and then was
# sent something.
- balance = thl_lm.get_account_balance(
- account=thl_lm.get_account_or_create_bp_wallet(product=product)
+ balance = thl_ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
)
assert balance == int(rand_amount) * -1
# Test some basic assertions
with caplog.at_level(logging.INFO):
with pytest.raises(expected_exception=Exception):
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=uuid4().hex,
@@ -190,12 +210,17 @@ class TestThlLedgerManagerBPPayout:
)
assert "failed condition check >1 tx per day" in caplog.text
- def test_create_tx_redis_failure(self, product, thl_web_rw, thl_lm):
+ def test_create_tx_redis_failure(
+ self,
+ product: Product,
+ thl_web_rw: PostgresConfig,
+ thl_ledger_manager: ThlLedgerManager,
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
now = datetime.now(tz=UTC)
- thl_lm.create_tx_plug_bp_wallet(
+ thl_ledger_manager.create_tx_plug_bp_wallet(
product, rand_amount, now, direction=Direction.CREDIT
)
@@ -216,7 +241,7 @@ class TestThlLedgerManagerBPPayout:
)
with pytest.raises(expected_exception=Exception) as e:
- tx = thl_lm_redis_0.create_tx_bp_payout(
+ thl_lm_redis_0.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -224,21 +249,27 @@ class TestThlLedgerManagerBPPayout:
)
assert e.type is redis.exceptions.TimeoutError
# No txs were created
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=product)
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=product
+ )
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 0
- def test_create_tx_multiple_per_day(self, product, thl_lm):
+ def test_create_tx_multiple_per_day(
+ self, product: Product, thl_ledger_manager: ThlLedgerManager
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
now = datetime.now(tz=UTC)
- thl_lm.create_tx_plug_bp_wallet(
+ thl_ledger_manager.create_tx_plug_bp_wallet(
product, rand_amount * USDCent(2), now, direction=Direction.CREDIT
)
- tx = thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -248,7 +279,7 @@ class TestThlLedgerManagerBPPayout:
# Try to create another
# Will fail b/c it has the same payout event uuid
with pytest.raises(expected_exception=Exception) as e:
- tx = thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -260,7 +291,7 @@ class TestThlLedgerManagerBPPayout:
# Will fail due to multiple per day
payoutevent_uuid2 = uuid4().hex
with pytest.raises(expected_exception=Exception) as e:
- tx = thl_lm.create_tx_bp_payout(
+ tx = thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid2,
@@ -270,7 +301,7 @@ class TestThlLedgerManagerBPPayout:
assert str(e.value) == ">1 tx per day"
# Make it run by skipping one per day check
- tx = thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid2,
@@ -278,13 +309,17 @@ class TestThlLedgerManagerBPPayout:
skip_one_per_day_check=True,
)
- def test_create_tx_redis_lock_release_error(self, product, thl_lm):
+ def test_create_tx_redis_lock_release_error(
+ self, product: Product, thl_ledger_manager: ThlLedgerManager
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
now = datetime.now(tz=UTC)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=product)
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=product
+ )
- thl_lm.create_tx_plug_bp_wallet(
+ thl_ledger_manager.create_tx_plug_bp_wallet(
product, rand_amount * USDCent(2), now, direction=Direction.CREDIT
)
@@ -294,7 +329,7 @@ class TestThlLedgerManagerBPPayout:
# Create TX will fail on lock enter, no tx will actually get created
with pytest.raises(expected_exception=Exception) as e:
- tx = thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -302,7 +337,9 @@ class TestThlLedgerManagerBPPayout:
)
assert e.type is LedgerTransactionCreateError
assert str(e.value) == "Redis error: Simulated timeout during acquire"
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 0
@@ -311,7 +348,7 @@ class TestThlLedgerManagerBPPayout:
# Create TX will fail on lock exit, after the tx was created!
with pytest.raises(expected_exception=Exception) as e:
- tx = thl_lm.create_tx_bp_payout(
+ tx = thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -321,7 +358,9 @@ class TestThlLedgerManagerBPPayout:
assert str(e.value) == "Redis error: Simulated timeout during release"
# Transaction was still created!
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 1
Lock.release = original_release
@@ -329,34 +368,45 @@ class TestThlLedgerManagerBPPayout:
class TestPayoutEventManagerBPPayout:
- def test_create(self, product, thl_lm, brokerage_product_payout_event_manager):
+ def test_create(
+ self,
+ product: Product,
+ thl_ledger_manager: ThlLedgerManager,
+ brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
now = datetime.now(tz=UTC)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=product)
- assert thl_lm.get_account_balance(bp_wallet_account) == 0
- thl_lm.create_tx_plug_bp_wallet(
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=product
+ )
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == 0
+ thl_ledger_manager.create_tx_plug_bp_wallet(
product, rand_amount, now, direction=Direction.CREDIT
)
- assert thl_lm.get_account_balance(bp_wallet_account) == rand_amount
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == rand_amount
brokerage_product_payout_event_manager.set_account_lookup_table(thl_lm=thl_lm)
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
payout_type=PayoutType.ACH,
)
assert brokerage_product_payout_event_manager.check_for_ledger_tx(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product_id=product.id,
amount=rand_amount,
payout_event=pe,
)
- assert thl_lm.get_account_balance(bp_wallet_account) == 0
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == 0
def test_create_with_redis_error(
- self, product, caplog, thl_lm, brokerage_product_payout_event_manager
+ self,
+ product: Product,
+ caplog,
+ thl_ledger_manager: ThlLedgerManager,
+ brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
):
caplog.set_level("WARNING")
original_acquire = Lock.acquire
@@ -364,19 +414,23 @@ class TestPayoutEventManagerBPPayout:
rand_amount: USDCent = USDCent(randint(100, 1_000))
now = datetime.now(tz=UTC)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=product)
- assert thl_lm.get_account_balance(bp_wallet_account) == 0
- thl_lm.create_tx_plug_bp_wallet(
- product, rand_amount, now, direction=Direction.CREDIT
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=product
+ )
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == 0
+ thl_ledger_manager.create_tx_plug_bp_wallet(
+ product=product, amount=rand_amount, now=now, direction=Direction.CREDIT
+ )
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == rand_amount
+ brokerage_product_payout_event_manager.set_account_lookup_table(
+ thl_lm=thl_ledger_manager
)
- assert thl_lm.get_account_balance(bp_wallet_account) == rand_amount
- brokerage_product_payout_event_manager.set_account_lookup_table(thl_lm=thl_lm)
# Will fail on lock enter, no tx will actually get created
Lock.acquire = broken_acquire
with pytest.raises(expected_exception=Exception) as e:
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
@@ -389,13 +443,15 @@ class TestPayoutEventManagerBPPayout:
for m in caplog.messages
)
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
# One payout event is created, status is failed, and no ledger txs exist
assert len(txs) == 0
pes = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
- thl_ledger_manager=thl_lm, product_uuids=[product.id]
+ thl_ledger_manager=thl_ledger_manager, product_uuids=[product.id]
)
)
assert len(pes) == 1
@@ -407,17 +463,21 @@ class TestPayoutEventManagerBPPayout:
# Try to fix the failed payout, by trying ledger tx again
brokerage_product_payout_event_manager.retry_create_bp_payout_event_tx(
- product=product, thl_ledger_manager=thl_lm, payout_event_uuid=pe.uuid
+ product=product,
+ thl_ledger_manager=thl_ledger_manager,
+ payout_event_uuid=pe.uuid,
+ )
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
)
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 1
- assert thl_lm.get_account_balance(bp_wallet_account) == 0
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == 0
# And then try to run it again, it'll fail because a payout event with the same info exists
with pytest.raises(expected_exception=Exception) as e:
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
@@ -432,7 +492,7 @@ class TestPayoutEventManagerBPPayout:
now = datetime.now(tz=UTC)
with pytest.raises(LedgerTransactionConditionFailedError) as e:
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
@@ -446,7 +506,7 @@ class TestPayoutEventManagerBPPayout:
# And if we really want to, we can make it again
now = datetime.now(tz=UTC)
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
@@ -455,17 +515,25 @@ class TestPayoutEventManagerBPPayout:
skip_wallet_balance_check=True,
)
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 2
# since they were paid twice
- assert thl_lm.get_account_balance(bp_wallet_account) == 0 - rand_amount
+ assert (
+ thl_ledger_manager.get_account_balance(bp_wallet_account) == 0 - rand_amount
+ )
Lock.release = original_release
Lock.acquire = original_acquire
def test_create_with_redis_error_release(
- self, product, caplog, thl_lm, brokerage_product_payout_event_manager
+ self,
+ product: Product,
+ caplog,
+ thl_ledger_manager: ThlLedgerManager,
+ brokerage_product_payout_event_manager: BrokerageProductPayoutEventManager,
):
caplog.set_level("WARNING")
@@ -473,20 +541,24 @@ class TestPayoutEventManagerBPPayout:
rand_amount: USDCent = USDCent(randint(100, 1_000))
now = datetime.now(tz=UTC)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=product)
- brokerage_product_payout_event_manager.set_account_lookup_table(thl_lm=thl_lm)
+ bp_wallet_account = 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
+ )
- assert thl_lm.get_account_balance(bp_wallet_account) == 0
- thl_lm.create_tx_plug_bp_wallet(
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == 0
+ thl_ledger_manager.create_tx_plug_bp_wallet(
product, rand_amount, now, direction=Direction.CREDIT
)
- assert thl_lm.get_account_balance(bp_wallet_account) == rand_amount
+ assert thl_ledger_manager.get_account_balance(bp_wallet_account) == rand_amount
# Will fail on lock exit, after the tx was created!
# But it'll see that the tx was created and so everything will be fine
Lock.release = broken_release
pe = brokerage_product_payout_event_manager.create_bp_payout_event(
- thl_ledger_manager=thl_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
created=now,
amount=rand_amount,
@@ -497,12 +569,14 @@ class TestPayoutEventManagerBPPayout:
for m in caplog.messages
)
- txs = thl_lm.get_tx_filtered_by_account(account_uuid=bp_wallet_account.uuid)
+ txs = thl_ledger_manager.get_tx_filtered_by_account(
+ account_uuid=bp_wallet_account.uuid
+ )
txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"]
assert len(txs) == 1
pes = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
- thl_ledger_manager=thl_lm, product_uuids=[product.uuid]
+ thl_ledger_manager=thl_ledger_manager, product_uuids=[product.uuid]
)
)
assert len(pes) == 1
diff --git a/tests/managers/thl/test_ledger/test_thl_lm_tx.py b/tests/managers/thl/test_ledger/test_thl_lm_tx.py
index 6fb0a0f..89adb0b 100644
--- a/tests/managers/thl/test_ledger/test_thl_lm_tx.py
+++ b/tests/managers/thl/test_ledger/test_thl_lm_tx.py
@@ -1,4 +1,7 @@
+from __future__ import annotations
+
import logging
+from collections.abc import Callable
from datetime import UTC, datetime, timedelta
from decimal import Decimal
from random import randint
@@ -6,19 +9,30 @@ from uuid import uuid4
import pytest
-from generalresearch.currency import USDCent
+from generalresearch.currency import LedgerCurrency, USDCent
from generalresearch.managers.thl.ledger_manager.ledger import (
+ LedgerManager,
LedgerTransaction,
)
+from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+from generalresearch.managers.thl.product import ProductManager
+from generalresearch.managers.thl.session import SessionManager
+from generalresearch.managers.thl.wall import WallManager
from generalresearch.models import Source
from generalresearch.models.thl.definitions import (
WALL_ALLOWED_STATUS_STATUS_CODE,
)
-from generalresearch.models.thl.ledger import Direction, TransactionType
+from generalresearch.models.thl.ledger import (
+ AccountType,
+ Direction,
+ LedgerAccount,
+ TransactionType,
+)
from generalresearch.models.thl.payout import UserPayoutEvent
from generalresearch.models.thl.product import (
PayoutConfig,
PayoutTransformation,
+ Product,
UserWalletConfig,
)
from generalresearch.models.thl.session import (
@@ -38,45 +52,50 @@ class TestThlLedgerTxManager:
def test_create_tx_task_complete(
self,
- wall,
- user,
- account_revenue_task_complete,
- create_main_accounts,
- thl_lm,
- lm,
+ wall: Wall,
+ user: User,
+ account_revenue_task_complete: LedgerAccount,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
create_main_accounts()
- tx = thl_lm.create_tx_task_complete(wall=wall, user=user)
+ tx = thl_ledger_manager.create_tx_task_complete(wall=wall, user=user)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.created == tx.created
def test_create_tx_task_complete_(
- self, wall, user, account_revenue_task_complete, thl_lm, lm
+ self,
+ wall: Wall,
+ user: User,
+ account_revenue_task_complete: LedgerAccount,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
- tx = thl_lm.create_tx_task_complete_(wall=wall, user=user)
+ tx = thl_ledger_manager.create_tx_task_complete_(wall=wall, user=user)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.created == tx.created
def test_create_tx_bp_payment(
self,
- session_factory,
- user,
- create_main_accounts,
- delete_ledger_db,
- thl_lm,
- lm,
- session_manager,
+ session_factory: Callable[..., Session],
+ user: User,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ session_manager: SessionManager,
):
delete_ledger_db()
create_main_accounts()
s1 = session_factory(user=user)
- status, status_code_1 = s1.determine_session_status()
- thl_net, commission_amount, bp_pay, user_pay = s1.determine_payments()
+ _, status_code_1 = s1.determine_session_status()
+ _, _, bp_pay, user_pay = s1.determine_payments()
session_manager.finish_with_status(
session=s1,
status=Status.COMPLETE,
@@ -86,22 +105,22 @@ class TestThlLedgerTxManager:
user_payout=user_pay,
)
- tx = thl_lm.create_tx_bp_payment(session=s1)
+ tx = thl_ledger_manager.create_tx_bp_payment(session=s1)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.created == tx.created
def test_create_tx_bp_payment_amt(
self,
- session_factory,
- user_factory,
- product_manager,
- create_main_accounts,
- delete_ledger_db,
- thl_lm,
- lm,
- session_manager,
+ session_factory: Callable[..., Session],
+ user_factory: Callable[..., User],
+ product_manager: ProductManager,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ session_manager: SessionManager,
):
delete_ledger_db()
create_main_accounts()
@@ -119,7 +138,7 @@ class TestThlLedgerTxManager:
status, status_code_1 = s1.determine_session_status()
assert status == Status.COMPLETE
thl_net, commission_amount, bp_pay, user_pay = s1.determine_payments(
- thl_ledger_manager=thl_lm
+ thl_ledger_manager=thl_ledger_manager
)
print(thl_net, commission_amount, bp_pay, user_pay)
session_manager.finish_with_status(
@@ -131,25 +150,25 @@ class TestThlLedgerTxManager:
user_payout=user_pay,
)
- tx = thl_lm.create_tx_bp_payment(session=s1)
+ tx = thl_ledger_manager.create_tx_bp_payment(session=s1)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.created == tx.created
def test_create_tx_bp_payment_(
self,
- session_factory,
- user,
- create_main_accounts,
- thl_lm,
- lm,
- session_manager,
- utc_hour_ago,
+ session_factory: Callable[..., Session],
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ session_manager: SessionManager,
+ utc_hour_ago: datetime,
):
s1 = session_factory(user=user)
status, status_code_1 = s1.determine_session_status()
- thl_net, commission_amount, bp_pay, user_pay = s1.determine_payments()
+ _, _, bp_pay, user_pay = s1.determine_payments()
session_manager.finish_with_status(
session=s1,
status=status,
@@ -160,14 +179,20 @@ class TestThlLedgerTxManager:
)
s1.determine_payments()
- tx = thl_lm.create_tx_bp_payment_(session=s1)
+ tx = thl_ledger_manager.create_tx_bp_payment_(session=s1)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.created == tx.created
def test_create_tx_task_adjustment(
- self, wall_factory, session, user, create_main_accounts, thl_lm, lm
+ self,
+ wall_factory: Callable[..., Wall],
+ session: Session,
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
"""Create Wall event Complete, and Create a Tx Task Adjustment
@@ -179,16 +204,23 @@ class TestThlLedgerTxManager:
wall_status = Status.COMPLETE
wall: Wall = wall_factory(session=session, wall_status=wall_status)
- tx = thl_lm.create_tx_task_adjustment(wall=wall, user=user)
+ tx = thl_ledger_manager.create_tx_task_adjustment(wall=wall, user=user)
assert isinstance(tx, LedgerTransaction)
- res = lm.get_tx_by_id(transaction_id=tx.id)
+ res = ledger_manager.get_tx_by_id(transaction_id=tx.id)
assert res.entries[0].amount == int(wall.cpi * 100)
assert res.entries[1].amount == int(wall.cpi * 100)
assert wall.source.name in res.ext_description
assert res.created == tx.created
- def test_create_tx_bp_adjustment(self, session, user, caplog, thl_lm, lm):
+ def test_create_tx_bp_adjustment(
+ self,
+ session: Session,
+ user: User,
+ caplog,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ ):
status, status_code_1 = session.determine_session_status()
thl_net, commission_amount, bp_pay, user_pay = session.determine_payments()
@@ -209,18 +241,24 @@ class TestThlLedgerTxManager:
session.update(finished=datetime.now(tz=UTC) + timedelta(minutes=10))
assert session.finished
with caplog.at_level(logging.INFO):
- tx = thl_lm.create_tx_bp_adjustment(session=session)
+ tx = thl_ledger_manager.create_tx_bp_adjustment(session=session)
assert tx is None
assert "No transactions needed." in caplog.text
- def test_create_tx_bp_payout(self, product, caplog, thl_lm, currency):
+ def test_create_tx_bp_payout(
+ self,
+ product: Product,
+ caplog,
+ thl_ledger_manager: ThlLedgerManager,
+ currency: LedgerCurrency,
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
# Create a BP Payout for a Product without any activity. By issuing,
# the skip_* checks, we should be able to force it to work, and will
# then ultimately result in a negative balance
- tx = thl_lm.create_tx_bp_payout(
+ tx = thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=payoutevent_uuid,
@@ -235,7 +273,7 @@ class TestThlLedgerTxManager:
assert tx.ext_description == "BP Payout"
assert (
tx.tag
- == f"{thl_lm.currency.value}:{TransactionType.BP_PAYOUT.value}:{payoutevent_uuid}"
+ == f"{thl_ledger_manager.currency.value}:{TransactionType.BP_PAYOUT.value}:{payoutevent_uuid}"
)
assert tx.entries[0].amount == rand_amount
assert tx.entries[1].amount == rand_amount
@@ -243,15 +281,15 @@ class TestThlLedgerTxManager:
# Check the Product's balance, it should be negative the amount that was
# paid out. That's because the Product earned nothing.. and then was
# sent something.
- balance = thl_lm.get_account_balance(
- account=thl_lm.get_account_or_create_bp_wallet(product=product)
+ balance = thl_ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
)
assert balance == int(rand_amount) * -1
# Test some basic assertions
with caplog.at_level(logging.INFO):
with pytest.raises(expected_exception=Exception):
- thl_lm.create_tx_bp_payout(
+ thl_ledger_manager.create_tx_bp_payout(
product=product,
amount=rand_amount,
payoutevent_uuid=uuid4().hex,
@@ -262,7 +300,13 @@ class TestThlLedgerTxManager:
)
assert "failed condition check >1 tx per day" in caplog.text
- def test_create_tx_bp_payout_(self, product, thl_lm, lm, currency):
+ def test_create_tx_bp_payout_(
+ self,
+ product: Product,
+ thl_lm: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
+ ):
rand_amount: USDCent = USDCent(randint(100, 1_000))
payoutevent_uuid = uuid4().hex
@@ -285,14 +329,19 @@ class TestThlLedgerTxManager:
assert tx.entries[1].amount == rand_amount
def test_create_tx_plug_bp_wallet(
- self, product, create_main_accounts, thl_lm, lm, currency
+ self,
+ product: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
"""A BP Wallet "plug" is a way to makeup discrepancies and simply
add or remove money
"""
rand_amount: USDCent = USDCent(randint(100, 1_000))
- tx = thl_lm.create_tx_plug_bp_wallet(
+ tx = thl_ledger_manager.create_tx_plug_bp_wallet(
product=product,
amount=rand_amount,
created=datetime.now(tz=UTC),
@@ -304,13 +353,18 @@ class TestThlLedgerTxManager:
# We issued the BP money they didn't earn, so now they have a
# negative balance
- balance = thl_lm.get_account_balance(
- account=thl_lm.get_account_or_create_bp_wallet(product=product)
+ balance = thl_ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
)
assert balance == int(rand_amount) * -1
def test_create_tx_plug_bp_wallet_(
- self, product, create_main_accounts, thl_lm, lm, currency
+ self,
+ product: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
"""A BP Wallet "plug" is a way to fix discrepancies and simply
add or remove money.
@@ -320,7 +374,7 @@ class TestThlLedgerTxManager:
"""
rand_amount: USDCent = USDCent(randint(100, 1_000))
- tx = thl_lm.create_tx_plug_bp_wallet_(
+ tx = thl_ledger_manager.create_tx_plug_bp_wallet_(
product=product,
amount=rand_amount,
created=datetime.now(tz=UTC),
@@ -331,32 +385,32 @@ class TestThlLedgerTxManager:
# We issued the BP money they didn't earn, so now they have a
# negative balance
- balance = thl_lm.get_account_balance(
- account=thl_lm.get_account_or_create_bp_wallet(product=product)
+ balance = thl_ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
)
assert balance == int(rand_amount) * -1
# Issue a positive one now, and confirm the balance goes positive
- thl_lm.create_tx_plug_bp_wallet_(
+ thl_ledger_manager.create_tx_plug_bp_wallet_(
product=product,
amount=rand_amount + rand_amount,
created=datetime.now(tz=UTC),
direction=Direction.CREDIT,
)
- balance = thl_lm.get_account_balance(
- account=thl_lm.get_account_or_create_bp_wallet(product=product)
+ balance = thl_ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
)
assert balance == int(rand_amount)
def test_create_tx_user_payout_request(
self,
- user,
- product_user_wallet_yes,
- user_factory,
- delete_df_collection,
- thl_lm,
- lm,
- currency,
+ user: User,
+ product_user_wallet_yes: Product,
+ user_factory: Callable[..., User],
+ delete_df_collection: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
pe = UserPayoutEvent(
uuid=uuid4().hex,
@@ -369,7 +423,7 @@ class TestThlLedgerTxManager:
# The default user fixture uses a product that doesn't have wallet
# mode enabled
with pytest.raises(expected_exception=AssertionError):
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
skip_flag_check=True,
@@ -380,12 +434,12 @@ class TestThlLedgerTxManager:
u2 = user_factory(product=product_user_wallet_yes)
# User's pre-balance is 0 because no activity has occurred yet
- pre_balance = lm.get_account_balance(
- account=thl_lm.get_account_or_create_user_wallet(user=u2)
+ pre_balance = ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_user_wallet(user=u2)
)
assert pre_balance == 0
- tx = thl_lm.create_tx_user_payout_request(
+ tx = thl_ledger_manager.create_tx_user_payout_request(
user=u2,
payout_event=pe,
skip_flag_check=True,
@@ -406,19 +460,19 @@ class TestThlLedgerTxManager:
# Post balance is -$5.00 because it comes out of the wallet before
# it's Approved or Completed
- post_balance = lm.get_account_balance(
- account=thl_lm.get_account_or_create_user_wallet(user=u2)
+ post_balance = ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_user_wallet(user=u2)
)
assert post_balance == -500
def test_create_tx_user_payout_request_(
self,
- user,
- product_user_wallet_yes,
- user_factory,
- delete_ledger_db,
- thl_lm,
- lm,
+ user: User,
+ product_user_wallet_yes: Product,
+ user_factory: Callable[..., User],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
delete_ledger_db()
@@ -431,36 +485,36 @@ class TestThlLedgerTxManager:
)
rand_description = uuid4().hex
- tx = thl_lm.create_tx_user_payout_request_(
+ tx = thl_ledger_manager.create_tx_user_payout_request_(
user=user, payout_event=pe, description=rand_description
)
assert tx.ext_description == rand_description
- post_balance = lm.get_account_balance(
- account=thl_lm.get_account_or_create_user_wallet(user=user)
+ post_balance = ledger_manager.get_account_balance(
+ account=thl_ledger_manager.get_account_or_create_user_wallet(user=user)
)
assert post_balance == -500
def test_create_tx_user_payout_complete(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- delete_ledger_db,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
# Ensure the user starts out with nothing...
- assert lm.get_account_balance(account=user_account) == 0
+ assert ledger_manager.get_account_balance(account=user_account) == 0
pe = UserPayoutEvent(
uuid=uuid4().hex,
@@ -472,7 +526,7 @@ class TestThlLedgerTxManager:
# Confirm it's not possible unless a request occurred happen
with pytest.raises(expected_exception=ValueError):
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user=user,
payout_event=pe,
fee_amount=None,
@@ -480,17 +534,19 @@ class TestThlLedgerTxManager:
)
# (1) Make a request first
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
skip_flag_check=True,
skip_wallet_balance_check=True,
)
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == rand_amount * -1
+ assert (
+ ledger_manager.get_account_balance(account=user_account) == rand_amount * -1
+ )
# (2) Complete the request
- tx = thl_lm.create_tx_user_payout_complete(
+ tx = thl_ledger_manager.create_tx_user_payout_complete(
user=user,
payout_event=pe,
fee_amount=Decimal(0),
@@ -503,18 +559,20 @@ class TestThlLedgerTxManager:
# The amount that comes out of the user wallet doesn't change after
# it's approved becuase it's already been withdrawn
- assert lm.get_account_balance(account=user_account) == rand_amount * -1
+ assert (
+ ledger_manager.get_account_balance(account=user_account) == rand_amount * -1
+ )
def test_create_tx_user_payout_complete_(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
):
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
pe = UserPayoutEvent(
@@ -526,7 +584,7 @@ class TestThlLedgerTxManager:
)
# (1) Make a request first
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
skip_flag_check=True,
@@ -536,12 +594,14 @@ class TestThlLedgerTxManager:
# (2) Complete the request
rand_desc = uuid4().hex
- bp_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
product=user.product, expense_name="paypal"
)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=user.product)
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
- tx = thl_lm.create_tx_user_payout_complete_(
+ tx = thl_ledger_manager.create_tx_user_payout_complete_(
user=user,
payout_event=pe,
fee_amount=Decimal("0.00"),
@@ -550,19 +610,21 @@ class TestThlLedgerTxManager:
description=rand_desc,
)
assert tx.ext_description == rand_desc
- assert lm.get_account_balance(account=user_account) == rand_amount * -1
+ assert (
+ ledger_manager.get_account_balance(account=user_account) == rand_amount * -1
+ )
def test_create_tx_user_payout_cancelled(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
pe = UserPayoutEvent(
@@ -574,17 +636,19 @@ class TestThlLedgerTxManager:
)
# (1) Make a request first
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
skip_flag_check=True,
skip_wallet_balance_check=True,
)
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == rand_amount * -1
+ assert (
+ ledger_manager.get_account_balance(account=user_account) == rand_amount * -1
+ )
# (2) Cancel the request
- tx = thl_lm.create_tx_user_payout_cancelled(
+ tx = thl_ledger_manager.create_tx_user_payout_cancelled(
user=user,
payout_event=pe,
skip_flag_check=False,
@@ -595,19 +659,19 @@ class TestThlLedgerTxManager:
assert isinstance(tx, LedgerTransaction)
# Assert the balance comes back to 0 after it was cancelled
- assert lm.get_account_balance(account=user_account) == 0
+ assert ledger_manager.get_account_balance(account=user_account) == 0
def test_create_tx_user_payout_cancelled_(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
pe = UserPayoutEvent(
@@ -619,43 +683,45 @@ class TestThlLedgerTxManager:
)
# (1) Make a request first
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
skip_flag_check=True,
skip_wallet_balance_check=True,
)
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == rand_amount * -1
+ assert (
+ ledger_manager.get_account_balance(account=user_account) == rand_amount * -1
+ )
# (2) Cancel the request
rand_desc = uuid4().hex
- tx = thl_lm.create_tx_user_payout_cancelled_(
+ tx = thl_ledger_manager.create_tx_user_payout_cancelled_(
user=user, payout_event=pe, description=rand_desc
)
assert isinstance(tx, LedgerTransaction)
assert tx.ext_description == rand_desc
- assert lm.get_account_balance(account=user_account) == 0
+ assert ledger_manager.get_account_balance(account=user_account) == 0
def test_create_tx_user_bonus(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
rand_ref_uuid = uuid4().hex
rand_desc = uuid4().hex
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == 0
+ assert ledger_manager.get_account_balance(account=user_account) == 0
- tx = thl_lm.create_tx_user_bonus(
+ tx = thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(rand_amount / 100),
ref_uuid=rand_ref_uuid,
@@ -663,44 +729,48 @@ class TestThlLedgerTxManager:
skip_flag_check=True,
)
assert tx.ext_description == rand_desc
- assert tx.tag == f"{thl_lm.currency.value}:user_bonus:{rand_ref_uuid}"
+ assert (
+ tx.tag == f"{thl_ledger_manager.currency.value}:user_bonus:{rand_ref_uuid}"
+ )
assert tx.entries[0].amount == rand_amount
assert tx.entries[1].amount == rand_amount
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == rand_amount
+ assert ledger_manager.get_account_balance(account=user_account) == rand_amount
def test_create_tx_user_bonus_(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
user: User = user_factory(product=product_user_wallet_yes)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
rand_amount = randint(100, 1_000)
rand_ref_uuid = uuid4().hex
rand_desc = uuid4().hex
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == 0
+ assert ledger_manager.get_account_balance(account=user_account) == 0
- tx = thl_lm.create_tx_user_bonus_(
+ tx = thl_ledger_manager.create_tx_user_bonus_(
user=user,
amount=Decimal(rand_amount / 100),
ref_uuid=rand_ref_uuid,
description=rand_desc,
)
assert tx.ext_description == rand_desc
- assert tx.tag == f"{thl_lm.currency.value}:user_bonus:{rand_ref_uuid}"
+ assert (
+ tx.tag == f"{thl_ledger_manager.currency.value}:user_bonus:{rand_ref_uuid}"
+ )
assert tx.entries[0].amount == rand_amount
assert tx.entries[1].amount == rand_amount
# Assert the balance came out of their user wallet
- assert lm.get_account_balance(account=user_account) == rand_amount
+ assert ledger_manager.get_account_balance(account=user_account) == rand_amount
class TestThlLedgerTxManagerFlows:
@@ -709,7 +779,13 @@ class TestThlLedgerTxManagerFlows:
"""
def test_create_tx_task_complete(
- self, user, create_main_accounts, thl_lm, lm, currency, delete_ledger_db
+ self,
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -725,7 +801,9 @@ class TestThlLedgerTxManagerFlows:
started=datetime.now(UTC),
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall1, user=user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall1, user=user, created=wall1.started
+ )
wall2 = Wall(
user_id=1,
@@ -738,38 +816,40 @@ class TestThlLedgerTxManagerFlows:
started=datetime.now(UTC),
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall2, user=user, created=wall2.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall2, user=user, created=wall2.started
+ )
- cash = thl_lm.get_account_cash()
- revenue = thl_lm.get_account_task_complete_revenue()
+ cash = thl_ledger_manager.get_account_cash()
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
- assert lm.get_account_balance(cash) == 123 + 321
- assert lm.get_account_balance(revenue) == 123 + 321
- assert lm.check_ledger_balanced()
+ assert ledger_manager.get_account_balance(cash) == 123 + 321
+ assert ledger_manager.get_account_balance(revenue) == 123 + 321
+ assert ledger_manager.check_ledger_balanced()
assert (
- lm.get_account_filtered_balance(
+ ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="d"
)
== 123
)
assert (
- lm.get_account_filtered_balance(
+ ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="f"
)
== 321
)
assert (
- lm.get_account_filtered_balance(
+ ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="x"
)
== 0
)
assert (
- thl_lm.get_account_filtered_balance(
+ thl_ledger_manager.get_account_filtered_balance(
account=revenue,
metadata_key="thl_wall",
metadata_value=wall1.uuid,
@@ -778,7 +858,12 @@ class TestThlLedgerTxManagerFlows:
)
def test_create_transaction_task_complete_1_cent(
- self, user, create_main_accounts, thl_lm, lm, currency
+ self,
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
wall1 = Wall(
user_id=1,
@@ -791,7 +876,7 @@ class TestThlLedgerTxManagerFlows:
started=datetime.now(UTC),
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
@@ -799,14 +884,14 @@ class TestThlLedgerTxManagerFlows:
def test_create_transaction_bp_payment(
self,
- user,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
- delete_ledger_db,
- session_factory,
- utc_hour_ago,
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
+ delete_ledger_db: Callable[..., None],
+ session_factory: Callable[..., Session],
+ utc_hour_ago: datetime,
):
delete_ledger_db()
create_main_accounts()
@@ -819,7 +904,9 @@ class TestThlLedgerTxManagerFlows:
)
w1: Wall = s1.wall_events[0]
- tx = thl_lm.create_tx_task_complete(wall=w1, user=user, created=w1.started)
+ tx = thl_ledger_manager.create_tx_task_complete(
+ wall=w1, user=user, created=w1.started
+ )
assert isinstance(tx, LedgerTransaction)
status, status_code_1 = s1.determine_session_status()
@@ -832,35 +919,39 @@ class TestThlLedgerTxManagerFlows:
user_payout=user_pay,
)
print(thl_net, commission_amount, bp_pay, user_pay)
- thl_lm.create_tx_bp_payment(session=s1, created=w1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=s1, created=w1.started)
- revenue = thl_lm.get_account_task_complete_revenue()
- bp_wallet = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- bp_commission = thl_lm.get_account_or_create_bp_commission(product=user.product)
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ bp_commission = thl_ledger_manager.get_account_or_create_bp_commission(
+ product=user.product
+ )
- assert 0 == lm.get_account_balance(account=revenue)
- assert 50 == lm.get_account_filtered_balance(
+ assert 0 == ledger_manager.get_account_balance(account=revenue)
+ assert 50 == ledger_manager.get_account_filtered_balance(
account=revenue,
metadata_key="source",
metadata_value=Source.TESTING,
)
- assert 48 == lm.get_account_balance(account=bp_wallet)
- assert 48 == lm.get_account_filtered_balance(
+ assert 48 == ledger_manager.get_account_balance(account=bp_wallet)
+ assert 48 == ledger_manager.get_account_filtered_balance(
account=bp_wallet,
metadata_key="thl_session",
metadata_value=s1.uuid,
)
- assert 2 == thl_lm.get_account_balance(account=bp_commission)
- assert thl_lm.check_ledger_balanced()
+ assert 2 == thl_ledger_manager.get_account_balance(account=bp_commission)
+ assert thl_ledger_manager.check_ledger_balanced()
def test_create_transaction_bp_payment_round(
self,
- user_factory,
- product_user_wallet_no,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
product_user_wallet_no.commission_pct = Decimal("0.085")
user: User = user_factory(product=product_user_wallet_no)
@@ -877,7 +968,7 @@ class TestThlLedgerTxManagerFlows:
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
assert isinstance(tx, LedgerTransaction)
@@ -894,11 +985,19 @@ class TestThlLedgerTxManagerFlows:
)
print(thl_net, commission_amount, bp_pay, user_pay)
- tx = thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ tx = thl_ledger_manager.create_tx_bp_payment(
+ session=session, created=wall1.started
+ )
assert isinstance(tx, LedgerTransaction)
def test_create_transaction_bp_payment_round2(
- self, delete_ledger_db, user, create_main_accounts, thl_lm, lm, currency
+ self,
+ delete_ledger_db: Callable[..., None],
+ user: User,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
@@ -917,7 +1016,9 @@ class TestThlLedgerTxManagerFlows:
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall1, user=user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall1, user=user, created=wall1.started
+ )
session = Session(started=wall1.started, user=user, wall_events=[wall1])
status, status_code_1 = session.determine_session_status()
# thl_net, commission_amount, bp_pay, user_pay = session.determine_payments()
@@ -929,16 +1030,16 @@ class TestThlLedgerTxManagerFlows:
user_payout=Decimal("1.53"),
)
- thl_lm.create_tx_bp_payment(session=session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=session, created=wall1.started)
def test_create_transaction_bp_payment_round3(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
# e.g. session ___ fails b/c we rounded incorrectly
# before, and now we are off by a penny...
@@ -955,7 +1056,9 @@ class TestThlLedgerTxManagerFlows:
started=datetime.now(UTC),
finished=datetime.now(UTC) + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall=wall1, user=user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=wall1, user=user, created=wall1.started
+ )
session = Session(started=wall1.started, user=user, wall_events=[wall1])
status, status_code_1 = session.determine_session_status()
@@ -973,22 +1076,23 @@ class TestThlLedgerTxManagerFlows:
def test_create_transaction_bp_payment_user_wallet(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- delete_ledger_db,
- thl_lm,
- session_manager,
- wall_manager,
- lm,
- session_factory,
- currency,
- utc_hour_ago,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ session_manager: SessionManager,
+ wall_manager: WallManager,
+ ledger_manager: LedgerManager,
+ session_factory: Callable[..., Session],
+ currency: LedgerCurrency,
+ utc_hour_ago: datetime,
):
delete_ledger_db()
create_main_accounts()
user: User = user_factory(product=product_user_wallet_yes)
+ assert isinstance(user.product, Product)
assert user.product.user_wallet_enabled
s1: Session = session_factory(
@@ -1000,10 +1104,12 @@ class TestThlLedgerTxManagerFlows:
)
w1: Wall = s1.wall_events[0]
- thl_lm.create_tx_task_complete(wall=w1, user=user, created=w1.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=w1, user=user, created=w1.started
+ )
status, status_code_1 = s1.determine_session_status()
- thl_net, commission_amount, bp_pay, user_pay = s1.determine_payments()
+ _, _, bp_pay, user_pay = s1.determine_payments()
session_manager.finish_with_status(
session=s1,
status=status,
@@ -1012,52 +1118,56 @@ class TestThlLedgerTxManagerFlows:
payout=bp_pay,
user_payout=user_pay,
)
- thl_lm.create_tx_bp_payment(session=s1, created=w1.started)
+ thl_ledger_manager.create_tx_bp_payment(session=s1, created=w1.started)
- revenue = thl_lm.get_account_task_complete_revenue()
- bp_wallet = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- bp_commission = thl_lm.get_account_or_create_bp_commission(product=user.product)
- user_wallet = thl_lm.get_account_or_create_user_wallet(user=user)
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ bp_commission = thl_ledger_manager.get_account_or_create_bp_commission(
+ product=user.product
+ )
+ user_wallet = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
- assert 0 == thl_lm.get_account_balance(account=revenue)
- assert 50 == thl_lm.get_account_filtered_balance(
+ assert 0 == thl_ledger_manager.get_account_balance(account=revenue)
+ assert 50 == thl_ledger_manager.get_account_filtered_balance(
account=revenue,
metadata_key="source",
metadata_value=Source.TESTING,
)
- assert 48 - 19 == thl_lm.get_account_balance(account=bp_wallet)
- assert 48 - 19 == thl_lm.get_account_filtered_balance(
+ assert 48 - 19 == thl_ledger_manager.get_account_balance(account=bp_wallet)
+ assert 48 - 19 == thl_ledger_manager.get_account_filtered_balance(
account=bp_wallet,
metadata_key="thl_session",
metadata_value=s1.uuid,
)
- assert 2 == thl_lm.get_account_balance(bp_commission)
- assert 19 == thl_lm.get_account_balance(user_wallet)
- assert 19 == thl_lm.get_account_filtered_balance(
+ assert 2 == thl_ledger_manager.get_account_balance(bp_commission)
+ assert 19 == thl_ledger_manager.get_account_balance(user_wallet)
+ assert 19 == thl_ledger_manager.get_account_filtered_balance(
account=user_wallet,
metadata_key="thl_session",
metadata_value=s1.uuid,
)
- assert 0 == thl_lm.get_account_filtered_balance(
+ assert 0 == thl_ledger_manager.get_account_filtered_balance(
account=user_wallet, metadata_key="thl_session", metadata_value="x"
)
- assert thl_lm.check_ledger_balanced()
+ assert thl_ledger_manager.check_ledger_balanced()
class TestThlLedgerManagerAdj:
def test_create_tx_task_adjustment(
self,
- user_factory,
- product_user_wallet_no,
- create_main_accounts,
- delete_ledger_db,
- thl_lm,
- lm,
- utc_hour_ago,
- currency,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ utc_hour_ago: datetime,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
@@ -1076,7 +1186,7 @@ class TestThlLedgerManagerAdj:
finished=utc_hour_ago + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall1, user, created=wall1.started)
+ thl_ledger_manager.create_tx_task_complete(wall1, user, created=wall1.started)
wall2 = Wall(
user_id=1,
@@ -1089,7 +1199,7 @@ class TestThlLedgerManagerAdj:
started=utc_hour_ago,
finished=utc_hour_ago + timedelta(seconds=1),
)
- thl_lm.create_tx_task_complete(wall2, user, created=wall2.started)
+ thl_ledger_manager.create_tx_task_complete(wall2, user, created=wall2.started)
wall1.update(
adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
@@ -1097,24 +1207,26 @@ class TestThlLedgerManagerAdj:
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
print(wall1.get_cpi_after_adjustment())
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
- cash = thl_lm.get_account_cash()
- revenue = thl_lm.get_account_task_complete_revenue()
+ cash = thl_ledger_manager.get_account_cash()
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
- assert 123 + 321 - 123 == thl_lm.get_account_balance(account=cash)
- assert 123 + 321 - 123 == thl_lm.get_account_balance(account=revenue)
- assert thl_lm.check_ledger_balanced()
- assert 0 == thl_lm.get_account_filtered_balance(
+ assert 123 + 321 - 123 == thl_ledger_manager.get_account_balance(account=cash)
+ assert 123 + 321 - 123 == thl_ledger_manager.get_account_balance(
+ account=revenue
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
+ assert 0 == thl_ledger_manager.get_account_filtered_balance(
revenue, metadata_key="source", metadata_value="d"
)
- assert 321 == thl_lm.get_account_filtered_balance(
+ assert 321 == thl_ledger_manager.get_account_filtered_balance(
revenue, metadata_key="source", metadata_value="f"
)
- assert 0 == thl_lm.get_account_filtered_balance(
+ assert 0 == thl_ledger_manager.get_account_filtered_balance(
revenue, metadata_key="source", metadata_value="x"
)
- assert 123 - 123 == thl_lm.get_account_filtered_balance(
+ assert 123 - 123 == thl_ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="thl_wall", metadata_value=wall1.uuid
)
@@ -1125,43 +1237,43 @@ class TestThlLedgerManagerAdj:
adjusted_timestamp=utc_hour_ago + timedelta(minutes=45),
)
print(wall1.get_cpi_after_adjustment())
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
# and then run it again to make sure it does nothing
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
- cash = thl_lm.get_account_cash()
- revenue = thl_lm.get_account_task_complete_revenue()
+ cash = thl_ledger_manager.get_account_cash()
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
- assert 123 + 321 - 123 + 123 == thl_lm.get_account_balance(cash)
- assert 123 + 321 - 123 + 123 == thl_lm.get_account_balance(revenue)
- assert thl_lm.check_ledger_balanced()
- assert 123 == thl_lm.get_account_filtered_balance(
+ assert 123 + 321 - 123 + 123 == thl_ledger_manager.get_account_balance(cash)
+ assert 123 + 321 - 123 + 123 == thl_ledger_manager.get_account_balance(revenue)
+ assert thl_ledger_manager.check_ledger_balanced()
+ assert 123 == thl_ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="d"
)
- assert 321 == thl_lm.get_account_filtered_balance(
+ assert 321 == thl_ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="f"
)
- assert 0 == thl_lm.get_account_filtered_balance(
+ assert 0 == thl_ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="source", metadata_value="x"
)
- assert 123 - 123 + 123 == thl_lm.get_account_filtered_balance(
+ assert 123 - 123 + 123 == thl_ledger_manager.get_account_filtered_balance(
account=revenue, metadata_key="thl_wall", metadata_value=wall1.uuid
)
def test_create_tx_bp_adjustment(
self,
- user,
- product_user_wallet_no,
- create_main_accounts,
+ user: User,
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
caplog,
- thl_lm,
- lm,
- currency,
- session_manager,
- wall_manager,
- session_factory,
- utc_hour_ago,
- delete_ledger_db,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
+ session_manager: SessionManager,
+ wall_manager: WallManager,
+ session_factory: Callable[..., Session],
+ utc_hour_ago: datetime,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -1177,8 +1289,12 @@ class TestThlLedgerManagerAdj:
w1: Wall = s1.wall_events[0]
w2: Wall = s1.wall_events[1]
- thl_lm.create_tx_task_complete(wall=w1, user=user, created=w1.started)
- thl_lm.create_tx_task_complete(wall=w2, user=user, created=w2.started)
+ thl_ledger_manager.create_tx_task_complete(
+ wall=w1, user=user, created=w1.started
+ )
+ thl_ledger_manager.create_tx_task_complete(
+ wall=w2, user=user, created=w2.started
+ )
status, status_code_1 = s1.determine_session_status()
_, _, bp_pay, user_pay = s1.determine_payments()
@@ -1190,21 +1306,25 @@ class TestThlLedgerManagerAdj:
payout=bp_pay,
user_payout=user_pay,
)
- thl_lm.create_tx_bp_payment(session=s1, created=w1.started)
- revenue = thl_lm.get_account_task_complete_revenue()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- bp_commission_account = thl_lm.get_account_or_create_bp_commission(
+ thl_ledger_manager.create_tx_bp_payment(session=s1, created=w1.started)
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
product=user.product
)
- assert 380 == thl_lm.get_account_balance(account=bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(account=revenue)
- assert 20 == thl_lm.get_account_balance(account=bp_commission_account)
- thl_lm.check_ledger_balanced()
+ bp_commission_account = thl_ledger_manager.get_account_or_create_bp_commission(
+ product=user.product
+ )
+ assert 380 == thl_ledger_manager.get_account_balance(account=bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(account=revenue)
+ assert 20 == thl_ledger_manager.get_account_balance(
+ account=bp_commission_account
+ )
+ thl_ledger_manager.check_ledger_balanced()
# This should do nothing (since we haven't adjusted any wall events)
s1.adjust_status()
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
assert (
"create_transaction_bp_adjustment. No transactions needed." in caplog.text
@@ -1222,22 +1342,22 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=Decimal(0),
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall=w1, user=user)
+ thl_ledger_manager.create_tx_task_adjustment(wall=w1, user=user)
# -$1.00 b/c the MP took the $1 back, but we haven't yet taken the BP payment back
- assert -100 == thl_lm.get_account_balance(revenue)
+ assert -100 == thl_ledger_manager.get_account_balance(revenue)
s1.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
assert (
"create_transaction_bp_adjustment. No transactions needed." in caplog.text
)
- assert 380 - 95 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 - 5 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 380 - 95 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 - 5 == thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
# unrecon the $1 survey
wall_manager.adjust_status(
@@ -1246,29 +1366,29 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=None,
adjusted_timestamp=utc_hour_ago + timedelta(minutes=45),
)
- thl_lm.create_tx_task_adjustment(
+ thl_ledger_manager.create_tx_task_adjustment(
wall=w1,
user=user,
created=utc_hour_ago + timedelta(minutes=45),
)
_, _, _ = s1.determine_new_status_and_payouts()
s1.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=s1)
- assert 380 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20, thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
+ assert 380 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20, thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
def test_create_tx_bp_adjustment_small(
self,
user_factory: Callable[..., User],
- product_user_wallet_no,
- create_main_accounts,
- delete_ledger_db,
- thl_ledger_manager,
- ledger_manager,
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
utc_hour_ago: datetime,
- currency,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
@@ -1289,7 +1409,7 @@ class TestThlLedgerManagerAdj:
finished=utc_hour_ago + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
assert isinstance(tx, LedgerTransaction)
@@ -1304,31 +1424,31 @@ class TestThlLedgerManagerAdj:
payout=bp_pay,
user_payout=user_pay,
)
- thl_lm.create_tx_bp_payment(session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session, created=wall1.started)
wall1.update(
adjusted_status=WallAdjustedStatus.ADJUSTED_TO_FAIL,
adjusted_cpi=0,
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
session.adjust_status()
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
def test_create_tx_bp_adjustment_abandon(
self,
- user_factory,
- product_user_wallet_no,
- delete_ledger_db,
- session_factory,
- create_main_accounts,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ delete_ledger_db: Callable[..., None],
+ session_factory: Callable[..., Session],
+ create_main_accounts: Callable[..., None],
caplog,
- thl_lm,
- lm,
- currency,
- utc_hour_ago,
- session_manager,
- wall_manager,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
+ utc_hour_ago: datetime,
+ session_manager: SessionManager,
+ wall_manager: WallManager,
):
delete_ledger_db()
create_main_accounts()
@@ -1345,9 +1465,9 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=w1.cpi,
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall=w1, user=user)
+ thl_ledger_manager.create_tx_task_adjustment(wall=w1, user=user)
s1.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
# And then adjust it back (it was abandon before, but now it should be
# fail (?) or back to abandon?)
wall_manager.adjust_status(
@@ -1356,24 +1476,26 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=None,
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall=w1, user=user)
+ thl_ledger_manager.create_tx_task_adjustment(wall=w1, user=user)
s1.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
- revenue = thl_lm.get_account_task_complete_revenue()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- bp_commission_account = thl_lm.get_account_or_create_bp_commission(
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ bp_commission_account = thl_ledger_manager.get_account_or_create_bp_commission(
product=user.product
)
- assert 0 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 0 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 0 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 0 == thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
# This should do nothing
s1.adjust_status()
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session=s1)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
assert "No transactions needed" in caplog.text
# Now back to complete again
@@ -1384,19 +1506,19 @@ class TestThlLedgerManagerAdj:
adjusted_timestamp=utc_hour_ago + timedelta(hours=1),
)
s1.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=s1)
- assert 95 == thl_lm.get_account_balance(bp_wallet_account)
+ thl_ledger_manager.create_tx_bp_adjustment(session=s1)
+ assert 95 == thl_ledger_manager.get_account_balance(bp_wallet_account)
def test_create_tx_bp_adjustment_user_wallet(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
caplog,
- thl_lm,
- lm,
- currency,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
@@ -1432,7 +1554,7 @@ class TestThlLedgerManagerAdj:
started=now_w1,
finished=now_w1 + timedelta(minutes=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
assert isinstance(tx, LedgerTransaction)
@@ -1449,7 +1571,7 @@ class TestThlLedgerManagerAdj:
started=now_w2,
finished=now_w2 + timedelta(minutes=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall2, user=user, created=wall2.started
)
assert isinstance(tx, LedgerTransaction)
@@ -1477,25 +1599,31 @@ class TestThlLedgerManagerAdj:
user_payout=user_pay,
)
- tx = thl_lm.create_tx_bp_adjustment(session=session, created=wall1.started)
+ tx = thl_ledger_manager.create_tx_bp_adjustment(
+ session=session, created=wall1.started
+ )
assert isinstance(tx, LedgerTransaction)
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(product=user.product)
- assert 228 == thl_lm.get_account_balance(account=bp_wallet_account)
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ product=user.product
+ )
+ assert 228 == thl_ledger_manager.get_account_balance(account=bp_wallet_account)
- user_account = thl_lm.get_account_or_create_user_wallet(user=user)
- assert 152 == thl_lm.get_account_balance(account=user_account)
+ user_account = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
+ assert 152 == thl_ledger_manager.get_account_balance(account=user_account)
- revenue = thl_lm.get_account_task_complete_revenue()
- assert 0 == thl_lm.get_account_balance(account=revenue)
+ revenue = thl_ledger_manager.get_account_task_complete_revenue()
+ assert 0 == thl_ledger_manager.get_account_balance(account=revenue)
- bp_commission_account = thl_lm.get_account_or_create_bp_commission(
+ bp_commission_account = thl_ledger_manager.get_account_or_create_bp_commission(
product=user.product
)
- assert 20 == thl_lm.get_account_balance(account=bp_commission_account)
+ assert 20 == thl_ledger_manager.get_account_balance(
+ account=bp_commission_account
+ )
# the total (4.00) = 2.28 + 1.52 + .20
- assert thl_lm.check_ledger_balanced()
+ assert thl_ledger_manager.check_ledger_balanced()
# This should do nothing (since we haven't adjusted any wall events)
session.adjust_status()
@@ -1505,7 +1633,7 @@ class TestThlLedgerManagerAdj:
session.get_user_payout_after_adjustment(),
)
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
assert (
"create_transaction_bp_adjustment. No transactions needed." in caplog.text
)
@@ -1516,16 +1644,16 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=0,
adjusted_timestamp=now + timedelta(hours=1),
)
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
# -$1.00 b/c the MP took the $1 back, but we haven't yet taken the BP payment back
- assert -100 == thl_lm.get_account_balance(revenue)
+ assert -100 == thl_ledger_manager.get_account_balance(revenue)
session.adjust_status()
print(
session.get_status_after_adjustment(),
session.get_payout_after_adjustment(),
session.get_user_payout_after_adjustment(),
)
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
# running this twice b/c it should do nothing the 2nd time
print(
@@ -1534,16 +1662,16 @@ class TestThlLedgerManagerAdj:
session.get_user_payout_after_adjustment(),
)
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
assert (
"create_transaction_bp_adjustment. No transactions needed." in caplog.text
)
- assert 228 - 57 == thl_lm.get_account_balance(bp_wallet_account)
- assert 152 - 38 == thl_lm.get_account_balance(user_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 - 5 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 228 - 57 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 152 - 38 == thl_ledger_manager.get_account_balance(user_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 - 5 == thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
# unrecon the $1 survey
wall1.update(
@@ -1551,7 +1679,7 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=None,
adjusted_timestamp=now + timedelta(hours=2),
)
- tx = thl_lm.create_tx_task_adjustment(wall=wall1, user=user)
+ tx = thl_ledger_manager.create_tx_task_adjustment(wall=wall1, user=user)
assert isinstance(tx, LedgerTransaction)
new_status, new_payout, new_user_payout = (
@@ -1564,13 +1692,17 @@ class TestThlLedgerManagerAdj:
session.get_payout_after_adjustment(),
session.get_user_payout_after_adjustment(),
)
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
- assert 228 - 57 + 57 == thl_lm.get_account_balance(bp_wallet_account)
- assert 152 - 38 + 38 == thl_lm.get_account_balance(user_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 - 5 + 5 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 228 - 57 + 57 == thl_ledger_manager.get_account_balance(
+ bp_wallet_account
+ )
+ assert 152 - 38 + 38 == thl_ledger_manager.get_account_balance(user_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 - 5 + 5 == thl_ledger_manager.get_account_balance(
+ bp_commission_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
# make the $2 failure into a complete also
wall3.update(
@@ -1578,7 +1710,7 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=wall3.cpi,
adjusted_timestamp=now + timedelta(hours=2),
)
- thl_lm.create_tx_task_adjustment(wall3, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall3, user)
new_status, new_payout, new_user_payout = (
session.determine_new_status_and_payouts()
)
@@ -1589,24 +1721,30 @@ class TestThlLedgerManagerAdj:
session.get_payout_after_adjustment(),
session.get_user_payout_after_adjustment(),
)
- thl_lm.create_tx_bp_adjustment(session)
- assert 228 - 57 + 57 + 114 == thl_lm.get_account_balance(bp_wallet_account)
- assert 152 - 38 + 38 + 76 == thl_lm.get_account_balance(user_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 - 5 + 5 + 10 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ thl_ledger_manager.create_tx_bp_adjustment(session)
+ assert 228 - 57 + 57 + 114 == thl_ledger_manager.get_account_balance(
+ bp_wallet_account
+ )
+ assert 152 - 38 + 38 + 76 == thl_ledger_manager.get_account_balance(
+ user_account
+ )
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 - 5 + 5 + 10 == thl_ledger_manager.get_account_balance(
+ bp_commission_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
def test_create_transaction_bp_adjustment_cpi_adjustment(
self,
- user_factory,
- product_user_wallet_no,
- create_main_accounts,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_user_wallet_no: Product,
+ create_main_accounts: Callable[..., None],
+ delete_ledger_db: Callable[..., None],
caplog,
- thl_lm,
- lm,
- utc_hour_ago,
- currency,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ utc_hour_ago: datetime,
+ currency: LedgerCurrency,
):
delete_ledger_db()
create_main_accounts()
@@ -1623,7 +1761,7 @@ class TestThlLedgerManagerAdj:
started=utc_hour_ago,
finished=utc_hour_ago + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall1, user=user, created=wall1.started
)
assert isinstance(tx, LedgerTransaction)
@@ -1639,7 +1777,7 @@ class TestThlLedgerManagerAdj:
started=utc_hour_ago,
finished=utc_hour_ago + timedelta(seconds=1),
)
- tx = thl_lm.create_tx_task_complete(
+ tx = thl_ledger_manager.create_tx_task_complete(
wall=wall2, user=user, created=wall2.started
)
assert isinstance(tx, LedgerTransaction)
@@ -1654,15 +1792,19 @@ class TestThlLedgerManagerAdj:
payout=bp_pay,
user_payout=user_pay,
)
- thl_lm.create_tx_bp_payment(session, created=wall1.started)
+ thl_ledger_manager.create_tx_bp_payment(session, created=wall1.started)
- revenue = thl_lm.get_account_task_complete_revenue()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_commission_account = thl_lm.get_account_or_create_bp_commission(user.product)
- assert 380 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ revenue = ththl_ledger_managerl_lm.get_account_task_complete_revenue()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_commission_account = thl_ledger_manager.get_account_or_create_bp_commission(
+ user.product
+ )
+ assert 380 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 == thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
# cpi adjustment $1 -> $.60.
wall1.update(
@@ -1670,17 +1812,17 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=Decimal("0.60"),
adjusted_timestamp=utc_hour_ago + timedelta(minutes=30),
)
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
# -$0.40 b/c the MP took $0.40 back, but we haven't yet taken the BP payment back
- assert -40 == thl_lm.get_account_balance(revenue)
+ assert -40 == thl_ledger_manager.get_account_balance(revenue)
session.adjust_status()
print(
session.get_status_after_adjustment(),
session.get_payout_after_adjustment(),
session.get_user_payout_after_adjustment(),
)
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
# running this twice b/c it should do nothing the 2nd time
print(
@@ -1689,14 +1831,14 @@ class TestThlLedgerManagerAdj:
session.get_user_payout_after_adjustment(),
)
with caplog.at_level(logging.INFO):
- thl_lm.create_tx_bp_adjustment(session)
+ thl_ledger_manager.create_tx_bp_adjustment(session)
assert "create_transaction_bp_adjustment." in caplog.text
assert "No transactions needed." in caplog.text
- assert 380 - 38 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 20 - 2 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 380 - 38 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 20 - 2 == thl_ledger_manager.get_account_balance(bp_commission_account)
+ assert thl_ledger_manager.check_ledger_balanced()
# adjust it to failure
wall1.update(
@@ -1704,13 +1846,17 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=0,
adjusted_timestamp=utc_hour_ago + timedelta(minutes=45),
)
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
session.adjust_status()
- thl_lm.create_tx_bp_adjustment(session)
- assert 300 - (300 * 0.05) == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 300 * 0.05 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ thl_ledger_manager.create_tx_bp_adjustment(session)
+ assert 300 - (300 * 0.05) == thl_ledger_manager.get_account_balance(
+ bp_wallet_account
+ )
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 300 * 0.05 == thl_ledger_manager.get_account_balance(
+ bp_commission_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
# and then back to cpi adj again, but this time for more than the orig amount
wall1.update(
@@ -1718,13 +1864,17 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=Decimal("2.00"),
adjusted_timestamp=utc_hour_ago + timedelta(minutes=45),
)
- thl_lm.create_tx_task_adjustment(wall1, user)
+ thl_ledger_manager.create_tx_task_adjustment(wall1, user)
session.adjust_status()
- thl_lm.create_tx_bp_adjustment(session)
- assert 500 - (500 * 0.05) == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(revenue)
- assert 500 * 0.05 == thl_lm.get_account_balance(bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ thl_ledger_manager.create_tx_bp_adjustment(session)
+ assert 500 - (500 * 0.05) == thl_ledger_manager.get_account_balance(
+ bp_wallet_account
+ )
+ assert 0 == thl_ledger_manager.get_account_balance(revenue)
+ assert 500 * 0.05 == thl_ledger_manager.get_account_balance(
+ bp_commission_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
# And adjust again
wall1.update(
@@ -1732,12 +1882,14 @@ class TestThlLedgerManagerAdj:
adjusted_cpi=Decimal("3.00"),
adjusted_timestamp=utc_hour_ago + timedelta(minutes=45),
)
- thl_lm.create_tx_task_adjustment(wall=wall1, user=user)
+ thl_ledger_manager.create_tx_task_adjustment(wall=wall1, user=user)
session.adjust_status()
- thl_lm.create_tx_bp_adjustment(session=session)
- assert 600 - (600 * 0.05) == thl_lm.get_account_balance(
+ thl_ledger_manager.create_tx_bp_adjustment(session=session)
+ assert 600 - (600 * 0.05) == thl_ledger_manager.get_account_balance(
account=bp_wallet_account
)
- assert 0 == thl_lm.get_account_balance(account=revenue)
- assert 600 * 0.05 == thl_lm.get_account_balance(account=bp_commission_account)
- assert thl_lm.check_ledger_balanced()
+ assert 0 == thl_ledger_manager.get_account_balance(account=revenue)
+ assert 600 * 0.05 == thl_ledger_manager.get_account_balance(
+ account=bp_commission_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
diff --git a/tests/managers/thl/test_ledger/test_thl_lm_tx__user_payouts.py b/tests/managers/thl/test_ledger/test_thl_lm_tx__user_payouts.py
index cd6ea79..5fb6935 100644
--- a/tests/managers/thl/test_ledger/test_thl_lm_tx__user_payouts.py
+++ b/tests/managers/thl/test_ledger/test_thl_lm_tx__user_payouts.py
@@ -1,5 +1,7 @@
+from __future__ import annotations
+
import logging
-from datetime import UTC, datetime, timedelta
+from collections.abc import Callable
from decimal import Decimal
from uuid import uuid4
@@ -9,7 +11,10 @@ from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerTransactionConditionFailedError,
LedgerTransactionFlagAlreadyExistsError,
)
+from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
from generalresearch.models.thl.payout import UserPayoutEvent
+from generalresearch.models.thl.product import Product
from generalresearch.models.thl.user import User
from generalresearch.models.thl.wallet import PayoutType
@@ -18,12 +23,12 @@ class TestLedgerManagerAMT:
def test_create_transaction_amt_ass_request(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -39,16 +44,16 @@ class TestLedgerManagerAMT:
)
flag_key = f"test:user_payout:{pe.uuid}:request"
flag_name = f"ledger-manager:transaction_flag:{flag_key}"
- lm.redis_client.delete(flag_name)
+ ledger_manager.redis_client.delete(flag_name)
# User has $0 in their wallet. They are allowed amt_assignment payouts until -$1.00
- thl_lm.create_tx_user_payout_request(user=user, payout_event=pe)
+ thl_ledger_manager.create_tx_user_payout_request(user=user, payout_event=pe)
with pytest.raises(expected_exception=LedgerTransactionFlagAlreadyExistsError):
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user, payout_event=pe, skip_flag_check=False
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user, payout_event=pe, skip_flag_check=True
)
pe2 = UserPayoutEvent(
@@ -61,36 +66,40 @@ class TestLedgerManagerAMT:
flag_key = f"test:user_payout:{pe2.uuid}:request"
flag_name = f"ledger-manager:transaction_flag:{flag_key}"
- lm.redis_client.delete(flag_name)
+ ledger_manager.redis_client.delete(flag_name)
# 96 cents would put them over the -$1.00 limit
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
- thl_lm.create_tx_user_payout_request(user, payout_event=pe2)
+ thl_ledger_manager.create_tx_user_payout_request(user, payout_event=pe2)
# But they could do 0.95 cents
pe2.amount = 95
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe2, skip_flag_check=True
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
product=user.product
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(
+ user=user
+ )
- assert 0 == lm.get_account_balance(account=bp_wallet_account)
- assert 0 == lm.get_account_balance(account=cash)
- assert 100 == lm.get_account_balance(account=bp_pending_account)
- assert -100 == lm.get_account_balance(account=user_wallet_account)
- assert thl_lm.check_ledger_balanced()
- assert -5 == thl_lm.get_account_filtered_balance(
+ assert 0 == ledger_manager.get_account_balance(account=bp_wallet_account)
+ assert 0 == ledger_manager.get_account_balance(account=cash)
+ assert 100 == ledger_manager.get_account_balance(account=bp_pending_account)
+ assert -100 == ledger_manager.get_account_balance(account=user_wallet_account)
+ assert thl_ledger_manager.check_ledger_balanced()
+ assert -5 == thl_ledger_manager.get_account_filtered_balance(
account=user_wallet_account,
metadata_key="payoutevent",
metadata_value=pe.uuid,
)
- assert -95 == thl_lm.get_account_filtered_balance(
+ assert -95 == thl_ledger_manager.get_account_filtered_balance(
account=user_wallet_account,
metadata_key="payoutevent",
metadata_value=pe2.uuid,
@@ -98,12 +107,12 @@ class TestLedgerManagerAMT:
def test_create_transaction_amt_ass_complete(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -117,40 +126,42 @@ class TestLedgerManagerAMT:
debit_account_uuid=uuid4().hex,
)
flag = f"ledger-manager:transaction_flag:test:user_payout:{pe.uuid}:request"
- lm.redis_client.delete(flag)
+ ledger_manager.redis_client.delete(flag)
flag = f"ledger-manager:transaction_flag:test:user_payout:{pe.uuid}:complete"
- lm.redis_client.delete(flag)
+ ledger_manager.redis_client.delete(flag)
# User has $0 in their wallet. They are allowed amt_assignment payouts until -$1.00
- thl_lm.create_tx_user_payout_request(user, payout_event=pe)
- thl_lm.create_tx_user_payout_complete(user, payout_event=pe)
+ thl_ledger_manager.create_tx_user_payout_request(user, payout_event=pe)
+ thl_ledger_manager.create_tx_user_payout_complete(user, payout_event=pe)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
user.product
)
- bp_amt_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_amt_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
user.product, expense_name="amt"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user)
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(user)
# BP wallet pays the 1cent fee
- assert -1 == thl_lm.get_account_balance(bp_wallet_account)
- assert -5 == thl_lm.get_account_balance(cash)
- assert -1 == thl_lm.get_account_balance(bp_amt_expense_account)
- assert 0 == thl_lm.get_account_balance(bp_pending_account)
- assert -5 == lm.get_account_balance(user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ assert -1 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert -5 == thl_ledger_manager.get_account_balance(cash)
+ assert -1 == thl_ledger_manager.get_account_balance(bp_amt_expense_account)
+ assert 0 == thl_ledger_manager.get_account_balance(bp_pending_account)
+ assert -5 == ledger_manager.get_account_balance(user_wallet_account)
+ assert thl_ledger_manager.check_ledger_balanced()
def test_create_transaction_amt_bonus(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -165,15 +176,15 @@ class TestLedgerManagerAMT:
debit_account_uuid=uuid4().hex,
)
flag = f"ledger-manager:transaction_flag:test:user_payout:{pe.uuid}:request"
- lm.redis_client.delete(flag)
+ ledger_manager.redis_client.delete(flag)
flag = f"ledger-manager:transaction_flag:test:user_payout:{pe.uuid}:complete"
- lm.redis_client.delete(flag)
+ ledger_manager.redis_client.delete(flag)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
# User has $0 in their wallet. No amt bonus allowed
- thl_lm.create_tx_user_payout_request(user, payout_event=pe)
+ thl_ledger_manager.create_tx_user_payout_request(user, payout_event=pe)
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user,
amount=Decimal(5),
ref_uuid="e703830dec124f17abed2d697d8d7701",
@@ -181,68 +192,68 @@ class TestLedgerManagerAMT:
skip_flag_check=True,
)
pe.amount = 101
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe, skip_flag_check=False
)
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=False
)
with pytest.raises(expected_exception=LedgerTransactionFlagAlreadyExistsError):
# duplicate, even if amount changed
pe.amount = 200
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=False
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
# duplicate
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=True
)
pe.uuid = "533364150de4451198e5774e221a2acb"
pe.amount = 9900
with pytest.raises(expected_exception=ValueError):
# Trying to complete payout with no pending tx
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=True
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
# trying to payout $99 with only a $5 balance
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe, skip_flag_check=True
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
user.product
)
- bp_amt_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
+ user.product
+ )
+ bp_amt_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
user.product, expense_name="amt"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user)
- assert -500 + round(-101 * 0.20) == thl_lm.get_account_balance(
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(user)
+ assert -500 + round(-101 * 0.20) == thl_ledger_manager.get_account_balance(
bp_wallet_account
)
- assert -101 == lm.get_account_balance(cash)
- assert -20 == lm.get_account_balance(bp_amt_expense_account)
- assert 0 == lm.get_account_balance(bp_pending_account)
- assert 500 - 101 == lm.get_account_balance(user_wallet_account)
- assert lm.check_ledger_balanced() is True
+ assert -101 == ledger_manager.get_account_balance(cash)
+ assert -20 == ledger_manager.get_account_balance(bp_amt_expense_account)
+ assert 0 == ledger_manager.get_account_balance(bp_pending_account)
+ assert 500 - 101 == ledger_manager.get_account_balance(user_wallet_account)
+ assert ledger_manager.check_ledger_balanced() is True
def test_create_transaction_amt_bonus_cancel(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
caplog,
- thl_lm,
- lm,
- delete_ledger_db,
+ thl_ledger_manager: ThlLedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
- now = datetime.now(UTC) - timedelta(hours=1)
user: User = user_factory(product=product_amt_true)
pe = UserPayoutEvent(
@@ -253,41 +264,47 @@ class TestLedgerManagerAMT:
debit_account_uuid=uuid4().hex,
)
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user,
amount=Decimal(5),
ref_uuid="c44f4da2db1d421ebc6a5e5241ca4ce6",
description="Bribe",
skip_flag_check=True,
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe, skip_flag_check=True
)
- thl_lm.create_tx_user_payout_cancelled(
+ thl_ledger_manager.create_tx_user_payout_cancelled(
user, payout_event=pe, skip_flag_check=True
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
with caplog.at_level(logging.WARNING):
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=True
)
assert "trying to complete payout that was already cancelled" in caplog.text
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
user.product
)
- bp_amt_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
+ user.product
+ )
+ bp_amt_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
user.product, expense_name="amt"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user)
- assert -500 == thl_lm.get_account_balance(account=bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(account=cash)
- assert 0 == thl_lm.get_account_balance(account=bp_amt_expense_account)
- assert 0 == thl_lm.get_account_balance(account=bp_pending_account)
- assert 500 == thl_lm.get_account_balance(account=user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(user)
+ assert -500 == thl_ledger_manager.get_account_balance(account=bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(account=cash)
+ assert 0 == thl_ledger_manager.get_account_balance(
+ account=bp_amt_expense_account
+ )
+ assert 0 == thl_ledger_manager.get_account_balance(account=bp_pending_account)
+ assert 500 == thl_ledger_manager.get_account_balance(
+ account=user_wallet_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
pe2 = UserPayoutEvent(
uuid=uuid4().hex,
@@ -296,15 +313,15 @@ class TestLedgerManagerAMT:
cashout_method_uuid=uuid4().hex,
debit_account_uuid=uuid4().hex,
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe2, skip_flag_check=True
)
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe2, skip_flag_check=True
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
with caplog.at_level(logging.WARNING):
- thl_lm.create_tx_user_payout_cancelled(
+ thl_ledger_manager.create_tx_user_payout_cancelled(
user, payout_event=pe2, skip_flag_check=True
)
assert "trying to cancel payout that was already completed" in caplog.text
@@ -314,12 +331,12 @@ class TestLedgerManagerTango:
def test_create_transaction_tango_request(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
@@ -336,64 +353,65 @@ class TestLedgerManagerTango:
)
flag_key = f"test:user_payout:{pe.uuid}:request"
flag_name = f"ledger-manager:transaction_flag:{flag_key}"
- lm.redis_client.delete(flag_name)
- thl_lm.create_tx_user_bonus(
+ ledger_manager.redis_client.delete(flag_name)
+ thl_ledger_manager.create_tx_user_bonus(
user,
amount=Decimal(6),
ref_uuid="e703830dec124f17abed2d697d8d7701",
description="Bribe",
skip_flag_check=True,
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe, skip_flag_check=True
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
user.product
)
- bp_tango_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_tango_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
user.product, expense_name="tango"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user)
- assert -600 == thl_lm.get_account_balance(bp_wallet_account)
- assert 0 == thl_lm.get_account_balance(cash)
- assert 0 == thl_lm.get_account_balance(bp_tango_expense_account)
- assert 500 == thl_lm.get_account_balance(bp_pending_account)
- assert 600 - 500 == thl_lm.get_account_balance(user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(user)
+ assert -600 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert 0 == thl_ledger_manager.get_account_balance(cash)
+ assert 0 == thl_ledger_manager.get_account_balance(bp_tango_expense_account)
+ assert 500 == thl_ledger_manager.get_account_balance(bp_pending_account)
+ assert 600 - 500 == thl_ledger_manager.get_account_balance(user_wallet_account)
+ assert thl_ledger_manager.check_ledger_balanced()
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user, payout_event=pe, skip_flag_check=True
)
- assert -600 - round(500 * 0.035) == thl_lm.get_account_balance(
+ assert -600 - round(500 * 0.035) == thl_ledger_manager.get_account_balance(
bp_wallet_account
)
- assert -500, thl_lm.get_account_balance(cash)
- assert round(-500 * 0.035) == thl_lm.get_account_balance(
+ assert -500, thl_ledger_manager.get_account_balance(cash)
+ assert round(-500 * 0.035) == thl_ledger_manager.get_account_balance(
bp_tango_expense_account
)
- assert 0 == lm.get_account_balance(bp_pending_account)
- assert 100 == lm.get_account_balance(user_wallet_account)
- assert lm.check_ledger_balanced()
+ assert 0 == ledger_manager.get_account_balance(bp_pending_account)
+ assert 100 == ledger_manager.get_account_balance(user_wallet_account)
+ assert ledger_manager.check_ledger_balanced()
class TestLedgerManagerPaypal:
def test_create_transaction_paypal_request(
self,
- user_factory,
- product_amt_true,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_amt_true: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
- now = datetime.now(tz=UTC) - timedelta(hours=1)
user: User = user_factory(product=product_amt_true)
# debit_account_uuid nothing checks they match the ledger ... todo?
@@ -406,8 +424,8 @@ class TestLedgerManagerPaypal:
)
flag_key = f"test:user_payout:{pe.uuid}:request"
flag_name = f"ledger-manager:transaction_flag:{flag_key}"
- lm.redis_client.delete(flag_name)
- thl_lm.create_tx_user_bonus(
+ ledger_manager.redis_client.delete(flag_name)
+ thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(6),
ref_uuid="e703830dec124f17abed2d697d8d7701",
@@ -415,79 +433,91 @@ class TestLedgerManagerPaypal:
skip_flag_check=True,
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user, payout_event=pe, skip_flag_check=True
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
product=user.product
)
- bp_paypal_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_paypal_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
product=user.product, expense_name="paypal"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user=user)
- assert -600 == lm.get_account_balance(account=bp_wallet_account)
- assert 0 == lm.get_account_balance(account=cash)
- assert 0 == lm.get_account_balance(account=bp_paypal_expense_account)
- assert 500 == lm.get_account_balance(account=bp_pending_account)
- assert 600 - 500 == lm.get_account_balance(account=user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(
+ user=user
+ )
+ assert -600 == ledger_manager.get_account_balance(account=bp_wallet_account)
+ assert 0 == ledger_manager.get_account_balance(account=cash)
+ assert 0 == ledger_manager.get_account_balance(
+ account=bp_paypal_expense_account
+ )
+ assert 500 == ledger_manager.get_account_balance(account=bp_pending_account)
+ assert 600 - 500 == ledger_manager.get_account_balance(
+ account=user_wallet_account
+ )
+ assert thl_ledger_manager.check_ledger_balanced()
- thl_lm.create_tx_user_payout_complete(
+ thl_ledger_manager.create_tx_user_payout_complete(
user=user, payout_event=pe, skip_flag_check=True, fee_amount=Decimal("0.50")
)
- assert -600 - 50 == thl_lm.get_account_balance(bp_wallet_account)
- assert -500 == thl_lm.get_account_balance(cash)
- assert -50 == thl_lm.get_account_balance(bp_paypal_expense_account)
- assert 0 == thl_lm.get_account_balance(bp_pending_account)
- assert 100 == thl_lm.get_account_balance(user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ assert -600 - 50 == thl_ledger_manager.get_account_balance(bp_wallet_account)
+ assert -500 == thl_ledger_manager.get_account_balance(cash)
+ assert -50 == thl_ledger_manager.get_account_balance(bp_paypal_expense_account)
+ assert 0 == thl_ledger_manager.get_account_balance(bp_pending_account)
+ assert 100 == thl_ledger_manager.get_account_balance(user_wallet_account)
+ assert thl_ledger_manager.check_ledger_balanced()
class TestLedgerManagerBonus:
def test_create_transaction_bonus(
self,
- user_factory,
- product_user_wallet_yes,
- create_main_accounts,
- thl_lm,
- lm,
- delete_ledger_db,
+ user_factory: Callable[..., User],
+ product_user_wallet_yes: Product,
+ create_main_accounts: Callable[..., None],
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
user: User = user_factory(product=product_user_wallet_yes)
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(5),
ref_uuid="8d0aaf612462448a9ebdd57fab0fc660",
description="Bribe",
skip_flag_check=True,
)
- cash = thl_lm.get_account_cash()
- bp_wallet_account = thl_lm.get_account_or_create_bp_wallet(user.product)
- bp_pending_account = thl_lm.get_or_create_bp_pending_payout_account(
+ cash = thl_ledger_manager.get_account_cash()
+ bp_wallet_account = thl_ledger_manager.get_account_or_create_bp_wallet(
+ user.product
+ )
+ bp_pending_account = thl_ledger_manager.get_or_create_bp_pending_payout_account(
product=user.product
)
- bp_amt_expense_account = thl_lm.get_account_or_create_bp_expense(
+ bp_amt_expense_account = thl_ledger_manager.get_account_or_create_bp_expense(
user.product, expense_name="amt"
)
- user_wallet_account = thl_lm.get_account_or_create_user_wallet(user=user)
+ user_wallet_account = thl_ledger_manager.get_account_or_create_user_wallet(
+ user=user
+ )
- assert -500 == lm.get_account_balance(account=bp_wallet_account)
- assert 0 == lm.get_account_balance(account=cash)
- assert 0 == lm.get_account_balance(account=bp_amt_expense_account)
- assert 0 == lm.get_account_balance(account=bp_pending_account)
- assert 500 == lm.get_account_balance(account=user_wallet_account)
- assert thl_lm.check_ledger_balanced()
+ assert -500 == ledger_manager.get_account_balance(account=bp_wallet_account)
+ assert 0 == ledger_manager.get_account_balance(account=cash)
+ assert 0 == ledger_manager.get_account_balance(account=bp_amt_expense_account)
+ assert 0 == ledger_manager.get_account_balance(account=bp_pending_account)
+ assert 500 == ledger_manager.get_account_balance(account=user_wallet_account)
+ assert thl_ledger_manager.check_ledger_balanced()
with pytest.raises(expected_exception=LedgerTransactionFlagAlreadyExistsError):
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(5),
ref_uuid="8d0aaf612462448a9ebdd57fab0fc660",
@@ -495,7 +525,7 @@ class TestLedgerManagerBonus:
skip_flag_check=False,
)
with pytest.raises(expected_exception=LedgerTransactionConditionFailedError):
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(5),
ref_uuid="8d0aaf612462448a9ebdd57fab0fc660",
diff --git a/tests/managers/thl/test_ledger/test_thl_pem.py b/tests/managers/thl/test_ledger/test_thl_pem.py
index 7a4f0c9..29341cf 100644
--- a/tests/managers/thl/test_ledger/test_thl_pem.py
+++ b/tests/managers/thl/test_ledger/test_thl_pem.py
@@ -1,11 +1,24 @@
+from __future__ import annotations
+
+from collections.abc import Callable
from random import randint
from uuid import UUID, uuid4
import pytest
from generalresearch.currency import USDCent
+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.definitions import PayoutStatus
-from generalresearch.models.thl.payout import BrokerageProductPayoutEvent
+from generalresearch.models.thl.payout import (
+ BrokerageProductPayoutEvent,
+)
from generalresearch.models.thl.product import Product
from generalresearch.models.thl.wallet.cashout_method import (
CashoutRequestInfo,
@@ -14,7 +27,9 @@ from generalresearch.models.thl.wallet.cashout_method import (
class TestThlPayoutEventManager:
- def test_get_by_uuid(self, brokerage_product_payout_event_manager, thl_lm):
+ 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
@@ -26,11 +41,11 @@ class TestThlPayoutEventManager:
def test_filter_by(
self,
- product_factory,
- usd_cent,
- bp_payout_event_factory,
- thl_lm,
- brokerage_product_payout_event_manager,
+ 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)
@@ -38,22 +53,22 @@ class TestThlPayoutEventManager:
amounts = []
products = []
- for x_idx in range(N_PRODUCTS):
+ for _ in range(N_PRODUCTS):
product: Product = product_factory()
- thl_lm.get_account_or_create_bp_wallet(product=product)
+ 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_lm
+ thl_lm=thl_ledger_manager
)
- for y_idx in range(N_PAYOUT_EVENTS):
+ 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_lm.get_accounts_bp_wallet_for_products(
+ 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(
@@ -65,11 +80,11 @@ class TestThlPayoutEventManager:
def test_get_bp_payout_events_for_product(
self,
- product_factory,
- usd_cent,
- bp_payout_event_factory,
- brokerage_product_payout_event_manager,
- thl_lm,
+ 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)
@@ -77,23 +92,23 @@ class TestThlPayoutEventManager:
amounts = []
products = []
- for x_idx in range(N_PRODUCTS):
+ for _ in range(N_PRODUCTS):
product: Product = product_factory()
products.append(product)
- thl_lm.get_account_or_create_bp_wallet(product=product)
+ thl_ledger_manager.get_account_or_create_bp_wallet(product=product)
brokerage_product_payout_event_manager.set_account_lookup_table(
- thl_lm=thl_lm
+ thl_lm=thl_ledger_manager
)
- for y_idx in range(N_PAYOUT_EVENTS):
+ 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, now go
+ # 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(
- thl_ledger_manager=thl_lm, product_uuids=[product.id]
+ thl_ledger_manager=thl_ledger_manager, product_uuids=[product.id]
)
assert len(res) == N_PAYOUT_EVENTS
@@ -102,7 +117,8 @@ class TestThlPayoutEventManager:
# ahead and query for them
res = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
- thl_ledger_manager=thl_lm, product_uuids=[i.uuid for i in products]
+ thl_ledger_manager=thl_ledger_manager,
+ product_uuids=[i.uuid for i in products],
)
)
@@ -110,7 +126,7 @@ class TestThlPayoutEventManager:
assert sum([i.amount for i in res]) == sum(amounts)
@pytest.mark.skip
- def test_get_payout_detail(self, user_payout_event_manager):
+ 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
@@ -141,7 +157,7 @@ class TestThlPayoutEventManager:
# def test_filter_by(self):
# raise NotImplementedError
- def test_create(self, user_payout_event_manager):
+ def test_create(self, user_payout_event_manager: UserPayoutEventManager):
from generalresearch.models.thl.payout import UserPayoutEvent
# Confirm the creation method returns back an instance.
@@ -163,26 +179,30 @@ class TestThlPayoutEventManager:
def test_create_bp_payout(
self,
- product,
- delete_ledger_db,
- create_main_accounts,
- thl_lm,
- brokerage_product_payout_event_manager,
- lm,
+ 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_lm.get_account_or_create_bp_wallet(product=product)
- brokerage_product_payout_event_manager.set_account_lookup_table(thl_lm=thl_lm)
+ 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_lm,
+ thl_ledger_manager=thl_ledger_manager,
product=product,
amount=USDCent(rand_amount),
skip_wallet_balance_check=True,
@@ -191,15 +211,17 @@ class TestThlPayoutEventManager:
assert isinstance(pe, BrokerageProductPayoutEvent)
# Now try to query for it!
- res = thl_lm.get_tx_bp_payouts(account_uuids=[account_bp_wallet.uuid])
+ res = thl_ledger_manager.get_tx_bp_payouts(
+ account_uuids=[account_bp_wallet.uuid]
+ )
assert len(res) == 1
- res = thl_lm.get_tx_bp_payouts(account_uuids=[uuid4().hex])
+ 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, but they didn't have
+ # money was sent to the Brokerage product: Product, but they didn't have
# any activity that earned them money
- bal = lm.get_account_balance(account=account_bp_wallet)
+ bal = ledger_manager.get_account_balance(account=account_bp_wallet)
assert rand_amount == bal * -1
@@ -207,13 +229,13 @@ class TestBPPayoutEvent:
def test_get_bp_bp_payout_events_for_products(
self,
- product_factory,
- bp_payout_event_factory,
- usd_cent,
- delete_ledger_db,
- create_main_accounts,
- brokerage_product_payout_event_manager,
- thl_lm,
+ 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()
@@ -222,10 +244,12 @@ class TestBPPayoutEvent:
amounts = []
product: Product = product_factory()
- thl_lm.get_account_or_create_bp_wallet(product=product)
- brokerage_product_payout_event_manager.set_account_lookup_table(thl_lm=thl_lm)
+ 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 y_idx in range(N_PAYOUT_EVENTS):
+ for _ in range(N_PAYOUT_EVENTS):
bp_payout_event_factory(product=product, usd_cent=usd_cent)
amounts.append(usd_cent)
@@ -233,7 +257,7 @@ class TestBPPayoutEvent:
# array of BPPayoutEvents
bp_bp_res = (
brokerage_product_payout_event_manager.get_bp_bp_payout_events_for_products(
- thl_ledger_manager=thl_lm, product_uuids=[product.uuid]
+ thl_ledger_manager=thl_ledger_manager, product_uuids=[product.uuid]
)
)
assert isinstance(bp_bp_res, list)
diff --git a/tests/managers/thl/test_ledger/test_user_txs.py b/tests/managers/thl/test_ledger/test_user_txs.py
index a6bfa79..56dc485 100644
--- a/tests/managers/thl/test_ledger/test_user_txs.py
+++ b/tests/managers/thl/test_ledger/test_user_txs.py
@@ -1,11 +1,14 @@
+from __future__ import annotations
+
from collections.abc import Callable
from datetime import UTC, datetime
from decimal import Decimal
-from typing import TYPE_CHECKING
from uuid import uuid4
from generalresearch.config import GRLBaseSettings
+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 UserPayoutEventManager
from generalresearch.managers.thl.user_compensate import user_compensate
from generalresearch.models.thl.definitions import (
Status,
@@ -15,41 +18,38 @@ from generalresearch.models.thl.ledger import (
UserLedgerTransactionTypesSummary,
UserLedgerTransactionTypeSummary,
)
-
-if TYPE_CHECKING:
- from generalresearch.config import GRLSettings
- from generalresearch.models.thl.product import Product
- from generalresearch.models.thl.session import Session
- from generalresearch.models.thl.user import User
- from generalresearch.models.thl.wallet import PayoutType
+from generalresearch.models.thl.product import Product
+from generalresearch.models.thl.session import Session
+from generalresearch.models.thl.user import User
+from generalresearch.models.thl.wallet import PayoutType
def test_user_txs(
user_factory: Callable[..., User],
product_amt_true: Product,
create_main_accounts: Callable[..., None],
- thl_lm: ThlLedgerManager,
- lm,
+ thl_ledger_manager: ThlLedgerManager,
+ ledger_manager: LedgerManager,
delete_ledger_db: Callable[..., None],
- session_with_tx_factory,
- adj_to_fail_with_tx_factory,
- adj_to_complete_with_tx_factory,
- session_factory,
- user_payout_event_manager,
+ session_with_tx_factory: Callable[..., Session],
+ adj_to_fail_with_tx_factory: Callable[..., None],
+ adj_to_complete_with_tx_factory: Callable[..., None],
+ session_factory: Callable[..., Session],
+ user_payout_event_manager: UserPayoutEventManager,
utc_now: datetime,
- settings: GRLSettings,
+ settings: GRLBaseSettings,
):
delete_ledger_db()
create_main_accounts()
user: User = user_factory(product=product_amt_true)
- account = thl_lm.get_account_or_create_user_wallet(user)
+ account = thl_ledger_manager.get_account_or_create_user_wallet(user)
print(f"{account.uuid=}")
s: Session = session_with_tx_factory(user=user, wall_req_cpi=Decimal("1.00"))
user_compensate(
- ledger_manager=thl_lm,
+ ledger_manager=ledger_manager,
user=user,
amount_int=100,
)
@@ -63,7 +63,7 @@ def test_user_txs(
payout_type=PayoutType.AMT_HIT,
request_data={},
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
)
@@ -76,7 +76,7 @@ def test_user_txs(
payout_type=PayoutType.AMT_BONUS,
request_data={},
)
- thl_lm.create_tx_user_payout_request(
+ thl_ledger_manager.create_tx_user_payout_request(
user=user,
payout_event=pe,
)
@@ -93,16 +93,16 @@ def test_user_txs(
)
adj_to_complete_with_tx_factory(session=s_fail, created=utc_now)
- # txs = thl_lm.get_tx_filtered_by_account(account.uuid)
+ # txs = thl_ledger_manager.get_tx_filtered_by_account(account.uuid)
# print(len(txs), txs)
- txs = thl_lm.get_user_txs(user)
+ txs = thl_ledger_manager.get_user_txs(user)
assert len(txs.transactions) == 6
assert txs.total == 6
assert txs.page == 1
assert txs.size == 50
# print(len(txs.transactions), txs)
- d = txs.model_dump_json()
+ # d = txs.model_dump_json()
# print(d)
descriptions = {x.description for x in txs.transactions}
@@ -140,25 +140,26 @@ def test_user_txs_pagination(
user_factory: Callable[..., User],
product_amt_true: Product,
create_main_accounts: Callable[..., None],
- thl_lm: ThlLedgerManager,
+ ledger_manager: LedgerManager,
+ thl_ledger_manager: ThlLedgerManager,
delete_ledger_db: Callable[..., None],
):
delete_ledger_db()
create_main_accounts()
user: User = user_factory(product=product_amt_true)
- account = thl_lm.get_account_or_create_user_wallet(user)
+ account = thl_ledger_manager.get_account_or_create_user_wallet(user)
print(f"{account.uuid=}")
for _ in range(12):
user_compensate(
- ledger_manager=thl_lm,
+ ledger_manager=ledger_manager,
user=user,
amount_int=100,
skip_flag_check=True,
)
- txs = thl_lm.get_user_txs(user, page=1, size=5)
+ txs = thl_ledger_manager.get_user_txs(user, page=1, size=5)
assert len(txs.transactions) == 5
assert txs.total == 12
assert txs.page == 1
@@ -167,7 +168,7 @@ def test_user_txs_pagination(
assert txs.summary.user_bonus.entry_count == 12
# Skip to the 3rd page. We made 12, so there are 2 left
- txs = thl_lm.get_user_txs(user, page=3, size=5)
+ txs = thl_ledger_manager.get_user_txs(user, page=3, size=5)
assert len(txs.transactions) == 2
assert txs.total == 12
assert txs.page == 3
@@ -175,7 +176,7 @@ def test_user_txs_pagination(
assert txs.summary.user_bonus.entry_count == 12
# Should be empty, not fail
- txs = thl_lm.get_user_txs(user, page=4, size=5)
+ txs = thl_ledger_manager.get_user_txs(user, page=4, size=5)
assert len(txs.transactions) == 0
assert txs.total == 12
assert txs.page == 4
@@ -185,12 +186,12 @@ def test_user_txs_pagination(
# Test filtering. We should pull back only this one
now = datetime.now(tz=UTC)
user_compensate(
- ledger_manager=thl_lm,
+ ledger_manager=ledger_manager,
user=user,
amount_int=100,
skip_flag_check=True,
)
- txs = thl_lm.get_user_txs(user, page=1, size=5, time_start=now)
+ txs = thl_ledger_manager.get_user_txs(user, page=1, size=5, time_start=now)
assert len(txs.transactions) == 1
assert txs.total == 1
assert txs.page == 1
@@ -200,7 +201,7 @@ def test_user_txs_pagination(
# And filtering with 0 results
now = datetime.now(tz=UTC)
- txs = thl_lm.get_user_txs(user, page=1, size=5, time_start=now)
+ txs = thl_ledger_manager.get_user_txs(user, page=1, size=5, time_start=now)
assert len(txs.transactions) == 0
assert txs.total == 0
assert txs.page == 1
@@ -213,13 +214,10 @@ def test_user_txs_pagination(
def test_user_txs_rolling_balance(
user_factory: Callable[..., User],
product_amt_true: Product,
- create_main_accounts,
+ create_main_accounts: Callable[..., None],
thl_ledger_manager: ThlLedgerManager,
- ledger_manager: LedgerManager,
delete_ledger_db: Callable[..., None],
- session_with_tx_factory,
- adj_to_fail_with_tx_factory,
- user_payout_event_manager,
+ user_payout_event_manager: UserPayoutEventManager,
settings: GRLBaseSettings,
):
"""
diff --git a/tests/managers/thl/test_ledger/test_wallet.py b/tests/managers/thl/test_ledger/test_wallet.py
index bb49cd8..9e886db 100644
--- a/tests/managers/thl/test_ledger/test_wallet.py
+++ b/tests/managers/thl/test_ledger/test_wallet.py
@@ -1,19 +1,26 @@
+from __future__ import annotations
+
+from collections.abc import Callable
from decimal import Decimal
from uuid import uuid4
import pytest
+from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
+from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+from generalresearch.managers.thl.product import ProductManager
from generalresearch.models.thl.product import (
PayoutConfig,
PayoutTransformation,
PayoutTransformationPercentArgs,
+ Product,
UserWalletConfig,
)
from generalresearch.models.thl.user import User
@pytest.fixture()
-def schrute_product(product_manager):
+def schrute_product(product_manager: ProductManager) -> Product:
return product_manager.create_dummy(
user_wallet_config=UserWalletConfig(enabled=True, amt=False),
payout_config=PayoutConfig(
@@ -27,25 +34,30 @@ def schrute_product(product_manager):
class TestGetUserWalletBalance:
- def test_get_user_wallet_balance_non_managed(self, user, thl_lm):
+ def test_get_user_wallet_balance_non_managed(
+ self, user: User, thl_ledger_manager: ThlLedgerManager
+ ):
with pytest.raises(
AssertionError,
match="Can't get wallet balance on non-managed account.",
):
- thl_lm.get_user_wallet_balance(user=user)
+ thl_ledger_manager.get_user_wallet_balance(user=user)
def test_get_user_wallet_balance_managed_0(
- self, schrute_product, user_factory, thl_lm
+ self,
+ schrute_product: Product,
+ user_factory: Callable[..., User],
+ thl_ledger_manager: ThlLedgerManager,
):
assert (
schrute_product.payout_config.payout_format == "{payout:,.0f} Schrute Bucks"
)
user: User = user_factory(schrute_product)
- balance = thl_lm.get_user_wallet_balance(user=user)
+ balance = thl_ledger_manager.get_user_wallet_balance(user=user)
assert balance == 0
balance_string = user.product.format_payout_format(Decimal(balance) / 100)
assert balance_string == "0 Schrute Bucks"
- redeemable_balance = thl_lm.get_user_redeemable_wallet_balance(
+ redeemable_balance = thl_ledger_manager.get_user_redeemable_wallet_balance(
user=user, user_wallet_balance=balance
)
assert redeemable_balance == 0
@@ -55,10 +67,14 @@ class TestGetUserWalletBalance:
assert redeemable_balance_string == "0 Schrute Bucks"
def test_get_user_wallet_balance_managed(
- self, schrute_product, user_factory, thl_lm, session_with_tx_factory
+ self,
+ schrute_product: Product,
+ user_factory: Callable[..., User],
+ thl_ledger_manager: ThlLedgerManager,
+ session_with_tx_factory: Callable[..., None],
):
user: User = user_factory(schrute_product)
- thl_lm.create_tx_user_bonus(
+ thl_ledger_manager.create_tx_user_bonus(
user=user,
amount=Decimal(1),
ref_uuid=uuid4().hex,
@@ -69,10 +85,10 @@ class TestGetUserWalletBalance:
# This product has a payout xform of 40% and commission of 5%
# 1.23 * 0.05 = 0.06 of commission
# 1.17 of payout * 0.40 = 0.47 of user pay and (1.17-0.47) 0.70 bp pay
- balance = thl_lm.get_user_wallet_balance(user=user)
+ balance = thl_ledger_manager.get_user_wallet_balance(user=user)
assert balance == 47 + 100 # plus the $1 bribe
- redeemable_balance = thl_lm.get_user_redeemable_wallet_balance(
+ redeemable_balance = thl_ledger_manager.get_user_redeemable_wallet_balance(
user=user, user_wallet_balance=balance
)
assert redeemable_balance == 20 + 100