aboutsummaryrefslogtreecommitdiff
path: root/tests/managers/thl/test_contest
diff options
context:
space:
mode:
authorstuppie2026-09-07 11:47:43 -0600
committerstuppie2026-09-07 11:47:43 -0600
commit092960233652cce1f4dc7841856034a6635e9cd9 (patch)
tree46e5fcd4d1e1b7ed0b987980c6c67ffa6e6b45c7 /tests/managers/thl/test_contest
parent80fd8aab4c7271ddb619b0de18741d7ac77b490b (diff)
parent242579a44855873d5e054e375440e9d3492cd682 (diff)
downloadgeneralresearch-092960233652cce1f4dc7841856034a6635e9cd9.tar.gz
generalresearch-092960233652cce1f4dc7841856034a6635e9cd9.zip
Merge branch 'master' into dev-greg
Diffstat (limited to 'tests/managers/thl/test_contest')
-rw-r--r--tests/managers/thl/test_contest/test_leaderboard.py89
-rw-r--r--tests/managers/thl/test_contest/test_milestone.py136
-rw-r--r--tests/managers/thl/test_contest/test_raffle.py232
3 files changed, 273 insertions, 184 deletions
diff --git a/tests/managers/thl/test_contest/test_leaderboard.py b/tests/managers/thl/test_contest/test_leaderboard.py
index 80a88a5..8aa0780 100644
--- a/tests/managers/thl/test_contest/test_leaderboard.py
+++ b/tests/managers/thl/test_contest/test_leaderboard.py
@@ -1,34 +1,41 @@
-from datetime import datetime, timezone, timedelta
+from __future__ import annotations
+
+from datetime import UTC, datetime, timedelta
+from typing import TYPE_CHECKING
from zoneinfo import ZoneInfo
from generalresearch.currency import USDCent
from generalresearch.models.thl.contest.definitions import (
- ContestStatus,
ContestEndReason,
+ ContestStatus,
)
from generalresearch.models.thl.contest.leaderboard import (
LeaderboardContest,
- LeaderboardContestCreate,
-)
-from generalresearch.models.thl.product import Product
-from generalresearch.models.thl.user import User
-from test_utils.managers.contest.conftest import (
- leaderboard_contest_in_db as contest_in_db,
- leaderboard_contest_create as contest_create,
)
+if TYPE_CHECKING:
+ from generalresearch.managers.thl.contest_manager import ContestManager
+ from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+ from generalresearch.managers.thl.user_manager.user_manager import UserManager
+ from generalresearch.models.thl.contest.leaderboard import (
+ LeaderboardContestCreate,
+ )
+ from generalresearch.models.thl.product import Product
+ from generalresearch.models.thl.user import User
+ from generalresearch.redis_helper import RedisConfig
-class TestLeaderboardContestCRUD:
+class TestLeaderboardContestCRUD:
def test_create(
self,
- contest_create: LeaderboardContestCreate,
+ leaderboard_contest_create: LeaderboardContestCreate,
product_user_wallet_yes: Product,
- thl_lm,
- contest_manager,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
c = contest_manager.create(
- product_id=product_user_wallet_yes.uuid, contest_create=contest_create
+ product_id=product_user_wallet_yes.uuid,
+ contest_create=leaderboard_contest_create,
)
c_out = contest_manager.get(c.uuid)
assert c == c_out
@@ -39,18 +46,19 @@ class TestLeaderboardContestCRUD:
# We have it set in the fixture as the daily contest for 2025-01-01
assert c.end_condition.ends_at == datetime(
2025, 1, 1, 23, 59, 59, 999999, tzinfo=ZoneInfo("America/New_York")
- ).astimezone(tz=timezone.utc) + timedelta(minutes=90)
+ ).astimezone(tz=UTC) + timedelta(minutes=90)
def test_enter(
self,
user_with_wallet: User,
- contest_in_db: LeaderboardContest,
- thl_lm,
- contest_manager,
- user_manager,
- thl_redis,
+ leaderboard_contest_in_db: LeaderboardContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
+ user_manager: UserManager,
+ thl_redis_config: RedisConfig,
):
- contest = contest_in_db
+ thl_redis = thl_redis_config.create_redis_client()
+ contest = leaderboard_contest_in_db
user = user_with_wallet
c: LeaderboardContest = contest_manager.get(contest_uuid=contest.uuid)
@@ -77,14 +85,15 @@ class TestLeaderboardContestCRUD:
def test_contest_ends(
self,
user_with_wallet: User,
- contest_in_db: LeaderboardContest,
- thl_lm,
- contest_manager,
- user_manager,
- thl_redis,
+ leaderboard_contest_in_db: LeaderboardContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
+ user_manager: UserManager,
+ thl_redis_config: RedisConfig,
):
+ thl_redis = thl_redis_config.create_redis_client()
# The contest should be over. We need to trigger it.
- contest = contest_in_db
+ contest = leaderboard_contest_in_db
contest._redis_client = thl_redis
contest._user_manager = user_manager
user = user_with_wallet
@@ -100,18 +109,22 @@ class TestLeaderboardContestCRUD:
)
assert c.user_rank == 1
- bp_wallet = thl_lm.get_account_or_create_bp_wallet_by_uuid(user.product_id)
- bp_wallet_balance = thl_lm.get_account_balance(account=bp_wallet)
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet_by_uuid(
+ user.product_id
+ )
+ bp_wallet_balance = thl_ledger_manager.get_account_balance(account=bp_wallet)
assert bp_wallet_balance == 0
- user_wallet = thl_lm.get_account_or_create_user_wallet(user=user)
- user_balance = thl_lm.get_account_balance(user_wallet)
+ user_wallet = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
+ user_balance = thl_ledger_manager.get_account_balance(user_wallet)
assert user_balance == 0
decision, reason = contest.should_end()
assert decision
assert reason == ContestEndReason.ENDS_AT
- contest_manager.end_contest_if_over(contest=contest, ledger_manager=thl_lm)
+ contest_manager.end_contest_if_over(
+ contest=contest, ledger_manager=thl_ledger_manager
+ )
c: LeaderboardContest = contest_manager.get(contest_uuid=contest.uuid)
assert c.status == ContestStatus.COMPLETED
@@ -129,10 +142,12 @@ class TestLeaderboardContestCRUD:
assert w.prize.cash_amount == USDCent(15_00)
# The prize is $15.00, so the user should get $15, paid by the bp
- assert thl_lm.get_account_balance(account=user_wallet) == 15_00
+ assert thl_ledger_manager.get_account_balance(account=user_wallet) == 15_00
# contest wallet is 0, and the BP gets 20c
- contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
- contest_uuid=c.uuid
+ contest_wallet = (
+ thl_ledger_manager.get_account_or_create_contest_wallet_by_uuid(
+ contest_uuid=c.uuid
+ )
)
- assert thl_lm.get_account_balance(account=contest_wallet) == 0
- assert thl_lm.get_account_balance(account=bp_wallet) == -15_00
+ assert thl_ledger_manager.get_account_balance(account=contest_wallet) == 0
+ assert thl_ledger_manager.get_account_balance(account=bp_wallet) == -15_00
diff --git a/tests/managers/thl/test_contest/test_milestone.py b/tests/managers/thl/test_contest/test_milestone.py
index 7312a64..f26819b 100644
--- a/tests/managers/thl/test_contest/test_milestone.py
+++ b/tests/managers/thl/test_contest/test_milestone.py
@@ -1,34 +1,41 @@
-from datetime import datetime, timezone
+from __future__ import annotations
+
+from collections.abc import Callable
+from datetime import UTC, datetime
+from typing import TYPE_CHECKING
from generalresearch.models.thl.contest.definitions import (
- ContestStatus,
ContestEndReason,
+ ContestEntryTrigger,
+ ContestStatus,
)
from generalresearch.models.thl.contest.milestone import (
MilestoneContest,
- MilestoneContestCreate,
MilestoneUserView,
- ContestEntryTrigger,
-)
-from generalresearch.models.thl.product import Product
-from generalresearch.models.thl.user import User
-from test_utils.managers.contest.conftest import (
- milestone_contest as contest,
- milestone_contest_in_db as contest_in_db,
- milestone_contest_create as contest_create,
- milestone_contest_factory as contest_factory,
)
+if TYPE_CHECKING:
+ from generalresearch.managers.thl.contest_manager import ContestManager
+ from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+ from generalresearch.models.thl.contest.milestone import (
+ MilestoneContestCreate,
+ )
+ from generalresearch.models.thl.product import Product
+ from generalresearch.models.thl.user import User
-class TestMilestoneContest:
- def test_should_end(self, contest: MilestoneContest, thl_lm, contest_manager):
+class TestMilestoneContest:
+ def test_should_end(
+ self,
+ milestone_contest: MilestoneContest,
+ ):
+ contest = milestone_contest
# contest is active and has no entries
should, msg = contest.should_end()
assert not should, msg
# Change so that the contest ends now
- contest.end_condition.ends_at = datetime.now(tz=timezone.utc)
+ contest.end_condition.ends_at = datetime.now(tz=UTC)
should, msg = contest.should_end()
assert should
assert msg == ContestEndReason.ENDS_AT
@@ -43,16 +50,15 @@ class TestMilestoneContest:
class TestMilestoneContestCRUD:
-
def test_create(
self,
- contest_create: MilestoneContestCreate,
+ milestone_contest_create: MilestoneContestCreate,
product_user_wallet_yes: Product,
- thl_lm,
- contest_manager,
+ contest_manager: ContestManager,
):
c = contest_manager.create(
- product_id=product_user_wallet_yes.uuid, contest_create=contest_create
+ product_id=product_user_wallet_yes.uuid,
+ contest_create=milestone_contest_create,
)
c_out = contest_manager.get(c.uuid)
assert c == c_out
@@ -68,20 +74,20 @@ class TestMilestoneContestCRUD:
def test_enter(
self,
user_with_wallet: User,
- contest_in_db: MilestoneContest,
- thl_lm,
- contest_manager,
+ milestone_contest_in_db: MilestoneContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Users CANNOT directly enter a milestone contest through the api,
# but we'll call this manager method when a trigger is hit.
- contest = contest_in_db
+ contest = milestone_contest_in_db
user = user_with_wallet
contest_manager.enter_milestone_contest(
contest_uuid=contest.uuid,
user=user,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
incr=1,
)
@@ -96,17 +102,19 @@ class TestMilestoneContestCRUD:
assert c.user_amount == 1
# Contest wallet should have 0 bc there is no ledger
- contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
- contest_uuid=contest.uuid
+ contest_wallet = (
+ thl_ledger_manager.get_account_or_create_contest_wallet_by_uuid(
+ contest_uuid=contest.uuid
+ )
)
- assert thl_lm.get_account_balance(contest_wallet) == 0
+ assert thl_ledger_manager.get_account_balance(contest_wallet) == 0
# Enter again!
contest_manager.enter_milestone_contest(
contest_uuid=contest.uuid,
user=user,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
incr=1,
)
c: MilestoneUserView = contest_manager.get_milestone_user_view(
@@ -122,21 +130,21 @@ class TestMilestoneContestCRUD:
def test_enter_win(
self,
user_with_wallet: User,
- contest_in_db: MilestoneContest,
- thl_lm,
- contest_manager,
+ milestone_contest_in_db: MilestoneContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# User enters contest, which brings the USER'S total amount above the limit,
# and the user reaches the milestone
- contest = contest_in_db
+ contest = milestone_contest_in_db
user = user_with_wallet
- user_wallet = thl_lm.get_account_or_create_user_wallet(user=user)
- user_balance = thl_lm.get_account_balance(account=user_wallet)
- bp_wallet = thl_lm.get_account_or_create_bp_wallet_by_uuid(
+ user_wallet = thl_ledger_manager.get_account_or_create_user_wallet(user=user)
+ user_balance = thl_ledger_manager.get_account_balance(account=user_wallet)
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet_by_uuid(
product_uuid=user.product_id
)
- bp_wallet_balance = thl_lm.get_account_balance(account=bp_wallet)
+ bp_wallet_balance = thl_ledger_manager.get_account_balance(account=bp_wallet)
c: MilestoneUserView = contest_manager.get_milestone_user_view(
contest_uuid=contest.uuid, user=user_with_wallet
@@ -151,7 +159,7 @@ class TestMilestoneContestCRUD:
contest_uuid=contest.uuid,
user=user,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
incr=1,
)
@@ -171,9 +179,12 @@ class TestMilestoneContestCRUD:
assert c.win_count == 1
# The prize was awarded! User should have won $1.00
- assert thl_lm.get_account_balance(user_wallet) - user_balance == 100
+ assert thl_ledger_manager.get_account_balance(user_wallet) - user_balance == 100
# Which was paid from the BP's balance
- assert thl_lm.get_account_balance(bp_wallet) - bp_wallet_balance == -100
+ assert (
+ thl_ledger_manager.get_account_balance(bp_wallet) - bp_wallet_balance
+ == -100
+ )
# winnings = cm.get_winnings_by_user(user=user)
# assert len(winnings) == 1
@@ -182,22 +193,22 @@ class TestMilestoneContestCRUD:
def test_enter_ends(
self,
- user_factory,
+ user_factory: Callable[..., User],
product_user_wallet_yes: Product,
- contest_in_db: MilestoneContest,
- thl_lm,
- contest_manager,
+ milestone_contest_in_db: MilestoneContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Multiple users reach the milestone. Contest ends after 5 wins.
users = [user_factory(product=product_user_wallet_yes) for _ in range(5)]
- contest = contest_in_db
+ contest = milestone_contest_in_db
for u in users:
contest_manager.enter_milestone_contest(
contest_uuid=contest.uuid,
user=u,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
incr=3,
)
@@ -208,29 +219,33 @@ class TestMilestoneContestCRUD:
def test_trigger(
self,
user_with_wallet: User,
- contest_in_db: MilestoneContest,
- thl_lm,
- contest_manager,
+ milestone_contest_in_db: MilestoneContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Pretend user just got a complete
cnt = contest_manager.hit_milestone_triggers(
country_iso="us",
user=user_with_wallet,
event=ContestEntryTrigger.TASK_COMPLETE,
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert cnt == 1
# Assert this contest got entered
c: MilestoneUserView = contest_manager.get_milestone_user_view(
- contest_uuid=contest_in_db.uuid, user=user_with_wallet
+ contest_uuid=milestone_contest_in_db.uuid, user=user_with_wallet
)
assert c.user_amount == 1
class TestMilestoneContestUserViews:
def test_list_user_eligible_country(
- self, user_with_wallet: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_wallet: User,
+ milestone_contest_factory: Callable[..., MilestoneContest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# No contests exists
cs = contest_manager.get_many_by_user_eligible(
@@ -239,7 +254,7 @@ class TestMilestoneContestUserViews:
assert len(cs) == 0
# Create a contest. It'll be in the US/CA
- contest_factory(country_isos={"us", "ca"})
+ milestone_contest_factory(country_isos={"us", "ca"})
# Not eligible in mexico
cs = contest_manager.get_many_by_user_eligible(
@@ -252,7 +267,7 @@ class TestMilestoneContestUserViews:
assert len(cs) == 1
# Create another, any country
- contest_factory(country_isos=None)
+ milestone_contest_factory(country_isos=None)
cs = contest_manager.get_many_by_user_eligible(
user=user_with_wallet, country_iso="mx"
)
@@ -263,10 +278,14 @@ class TestMilestoneContestUserViews:
assert len(cs) == 2
def test_list_user_eligible(
- self, user_with_money: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_money: User,
+ milestone_contest_factory: Callable[..., MilestoneContest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# User reaches milestone after 1 complete
- c = contest_factory(target_amount=1)
+ c = milestone_contest_factory(target_amount=1)
user = user_with_money
cs = contest_manager.get_many_by_user_eligible(
@@ -275,7 +294,10 @@ class TestMilestoneContestUserViews:
assert len(cs) == 1
contest_manager.enter_milestone_contest(
- contest_uuid=c.uuid, user=user, country_iso="us", ledger_manager=thl_lm
+ contest_uuid=c.uuid,
+ user=user,
+ country_iso="us",
+ ledger_manager=thl_ledger_manager,
)
# User isn't eligible anymore
diff --git a/tests/managers/thl/test_contest/test_raffle.py b/tests/managers/thl/test_contest/test_raffle.py
index 060055a..7388991 100644
--- a/tests/managers/thl/test_contest/test_raffle.py
+++ b/tests/managers/thl/test_contest/test_raffle.py
@@ -1,4 +1,8 @@
-from datetime import datetime, timezone
+from __future__ import annotations
+
+from collections.abc import Callable
+from datetime import UTC, datetime
+from typing import TYPE_CHECKING
import pytest
from pydantic import ValidationError
@@ -9,44 +13,49 @@ from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerTransactionConditionFailedError,
)
from generalresearch.models.thl.contest import (
- ContestPrize,
- ContestEntryRule,
ContestEndCondition,
+ ContestEntryRule,
+ ContestPrize,
)
-from generalresearch.models.thl.contest.definitions import (
- ContestStatus,
- ContestPrizeKind,
- ContestEndReason,
-)
-from generalresearch.models.thl.contest.exceptions import ContestError
-from generalresearch.models.thl.contest.raffle import (
+from generalresearch.models.thl.contest.contest_entry import (
ContestEntry,
ContestEntryType,
)
-from generalresearch.models.thl.contest.raffle import (
- RaffleContest,
- RaffleContestCreate,
- RaffleUserView,
-)
-from generalresearch.models.thl.product import Product
-from generalresearch.models.thl.user import User
-from test_utils.managers.contest.conftest import (
- raffle_contest as contest,
- raffle_contest_in_db as contest_in_db,
- raffle_contest_create as contest_create,
- raffle_contest_factory as contest_factory,
+from generalresearch.models.thl.contest.definitions import (
+ ContestEndReason,
+ ContestPrizeKind,
+ ContestStatus,
)
+from generalresearch.models.thl.contest.exceptions import ContestError
+from generalresearch.models.thl.contest.raffle import RaffleContest
+
+if TYPE_CHECKING:
+ from generalresearch.managers.thl.contest_manager import ContestManager
+ from generalresearch.managers.thl.ledger_manager.thl_ledger import ThlLedgerManager
+ from generalresearch.models.thl.contest import (
+ Contest,
+ )
+ from generalresearch.models.thl.contest.raffle import (
+ RaffleContestCreate,
+ RaffleUserView,
+ )
+ from generalresearch.models.thl.product import Product
+ from generalresearch.models.thl.user import User
class TestRaffleContest:
- def test_should_end(self, contest: RaffleContest, thl_lm, contest_manager):
+ def test_should_end(
+ self,
+ raffle_contest: RaffleContest,
+ ):
+ contest = raffle_contest
# contest is active and has no entries
should, msg = contest.should_end()
assert not should, msg
# Change so that the contest ends now
- contest.end_condition.ends_at = datetime.now(tz=timezone.utc)
+ contest.end_condition.ends_at = datetime.now(tz=UTC)
should, msg = contest.should_end()
assert should
assert msg == ContestEndReason.ENDS_AT
@@ -63,13 +72,12 @@ class TestRaffleContestCRUD:
def test_create(
self,
- contest_create: RaffleContestCreate,
+ raffle_contest_create: RaffleContestCreate,
product_user_wallet_yes: Product,
- thl_lm,
- contest_manager,
+ contest_manager: ContestManager,
):
c = contest_manager.create(
- product_id=product_user_wallet_yes.uuid, contest_create=contest_create
+ product_id=product_user_wallet_yes.uuid, contest_create=raffle_contest_create
)
c_out = contest_manager.get(c.uuid)
assert c == c_out
@@ -85,18 +93,20 @@ class TestRaffleContestCRUD:
def test_enter(
self,
user_with_money: User,
- contest_in_db: RaffleContest,
- thl_lm,
- contest_manager,
+ raffle_contest_in_db: RaffleContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Raffle ends at $1.00. User enters for $0.60
print(user_with_money.product_id)
- print(contest_in_db.product_id)
- print(contest_in_db.uuid)
- contest = contest_in_db
+ print(raffle_contest_in_db.product_id)
+ print(raffle_contest_in_db.uuid)
+ contest = raffle_contest_in_db
- user_wallet = thl_lm.get_account_or_create_user_wallet(user=user_with_money)
- user_balance = thl_lm.get_account_balance(account=user_wallet)
+ user_wallet = thl_ledger_manager.get_account_or_create_user_wallet(
+ user=user_with_money
+ )
+ user_balance = thl_ledger_manager.get_account_balance(account=user_wallet)
entry = ContestEntry(
entry_type=ContestEntryType.CASH, user=user_with_money, amount=USDCent(60)
@@ -105,7 +115,7 @@ class TestRaffleContestCRUD:
contest_uuid=contest.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
c: RaffleContest = contest_manager.get(contest_uuid=contest.uuid)
assert c.current_amount == USDCent(60)
@@ -120,30 +130,35 @@ class TestRaffleContestCRUD:
assert c.projected_win_probability == approx(60 / 100, rel=0.01)
# Contest wallet should have $0.60
- contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
- contest_uuid=contest.uuid
+ contest_wallet = (
+ thl_ledger_manager.get_account_or_create_contest_wallet_by_uuid(
+ contest_uuid=contest.uuid
+ )
)
- assert thl_lm.get_account_balance(account=contest_wallet) == 60
+ assert thl_ledger_manager.get_account_balance(account=contest_wallet) == 60
# User spent 60c
- assert user_balance - thl_lm.get_account_balance(account=user_wallet) == 60
+ assert (
+ user_balance - thl_ledger_manager.get_account_balance(account=user_wallet)
+ == 60
+ )
@pytest.mark.parametrize("user_with_money", [{"min_balance": 120}], indirect=True)
def test_enter_ends(
self,
user_with_money: User,
- contest_in_db: RaffleContest,
- thl_lm,
- contest_manager,
+ raffle_contest_in_db: RaffleContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# User enters contest, which brings the total amount above the limit,
# and the contest should end, with a winner selected
- contest = contest_in_db
+ contest = raffle_contest_in_db
- bp_wallet = thl_lm.get_account_or_create_bp_wallet_by_uuid(
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet_by_uuid(
user_with_money.product_id
)
# I bribed the user, so the balance is not 0
- bp_wallet_balance = thl_lm.get_account_balance(account=bp_wallet)
+ bp_wallet_balance = thl_ledger_manager.get_account_balance(account=bp_wallet)
for _ in range(2):
entry = ContestEntry(
@@ -155,7 +170,7 @@ class TestRaffleContestCRUD:
contest_uuid=contest.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
c: RaffleContest = contest_manager.get(contest_uuid=contest.uuid)
assert c.status == ContestStatus.COMPLETED
@@ -175,25 +190,33 @@ class TestRaffleContestCRUD:
assert win.product_user_id == user_with_money.product_user_id
# Contest wallet should have gotten zeroed out
- contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
- contest_uuid=contest.uuid
+ contest_wallet = (
+ thl_ledger_manager.get_account_or_create_contest_wallet_by_uuid(
+ contest_uuid=contest.uuid
+ )
)
- assert thl_lm.get_account_balance(contest_wallet) == 0
+ assert thl_ledger_manager.get_account_balance(contest_wallet) == 0
# Expense wallet gets the $1.00 expense
- expense_wallet = thl_lm.get_account_or_create_bp_expense_by_uuid(
+ expense_wallet = thl_ledger_manager.get_account_or_create_bp_expense_by_uuid(
product_uuid=user_with_money.product_id, expense_name="Prize"
)
- assert thl_lm.get_account_balance(expense_wallet) == -100
+ assert thl_ledger_manager.get_account_balance(expense_wallet) == -100
# And the BP gets 20c
- assert thl_lm.get_account_balance(bp_wallet) - bp_wallet_balance == 20
+ assert (
+ thl_ledger_manager.get_account_balance(bp_wallet) - bp_wallet_balance == 20
+ )
@pytest.mark.parametrize("user_with_money", [{"min_balance": 120}], indirect=True)
def test_enter_ends_cash_prize(
- self, user_with_money: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_money: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Same as test_enter_ends, but the prize is cash. Just
# testing the ledger methods
- c = contest_factory(
+ c = raffle_contest_factory(
prizes=[
ContestPrize(
name="$1.00 bonus",
@@ -205,12 +228,14 @@ class TestRaffleContestCRUD:
)
assert c.prizes[0].kind == ContestPrizeKind.CASH
- user_wallet = thl_lm.get_account_or_create_user_wallet(user=user_with_money)
- user_balance = thl_lm.get_account_balance(user_wallet)
- bp_wallet = thl_lm.get_account_or_create_bp_wallet_by_uuid(
+ user_wallet = thl_ledger_manager.get_account_or_create_user_wallet(
+ user=user_with_money
+ )
+ user_balance = thl_ledger_manager.get_account_balance(user_wallet)
+ bp_wallet = thl_ledger_manager.get_account_or_create_bp_wallet_by_uuid(
user_with_money.product_id
)
- bp_wallet_balance = thl_lm.get_account_balance(bp_wallet)
+ bp_wallet_balance = thl_ledger_manager.get_account_balance(bp_wallet)
## Enter Contest
entry = ContestEntry(
@@ -220,28 +245,35 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
# The prize is $1.00, so the user spent $1.20 entering, won, then got $1.00 back
assert (
- thl_lm.get_account_balance(account=user_wallet) == user_balance + 100 - 120
+ thl_ledger_manager.get_account_balance(account=user_wallet)
+ == user_balance + 100 - 120
)
# contest wallet is 0, and the BP gets 20c
- contest_wallet = thl_lm.get_account_or_create_contest_wallet_by_uuid(
- contest_uuid=c.uuid
+ contest_wallet = (
+ thl_ledger_manager.get_account_or_create_contest_wallet_by_uuid(
+ contest_uuid=c.uuid
+ )
+ )
+ assert thl_ledger_manager.get_account_balance(account=contest_wallet) == 0
+ assert (
+ thl_ledger_manager.get_account_balance(account=bp_wallet)
+ - bp_wallet_balance
+ == 20
)
- assert thl_lm.get_account_balance(account=contest_wallet) == 0
- assert thl_lm.get_account_balance(account=bp_wallet) - bp_wallet_balance == 20
def test_enter_failure(
self,
user_with_wallet: User,
- contest_in_db: RaffleContest,
- thl_lm,
- contest_manager,
+ raffle_contest_in_db: RaffleContest,
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
- c = contest_in_db
+ c = raffle_contest_in_db
user = user_with_wallet
# Tries to enter $0
@@ -260,7 +292,7 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert e.value.args[0] == "insufficient balance"
@@ -271,16 +303,20 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert "incompatible entry type" in str(e.value)
@pytest.mark.parametrize("user_with_money", [{"min_balance": 100}], indirect=True)
def test_enter_not_eligible(
- self, user_with_money: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_money: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# Max entry amount per user $0.10. Contest still ends at $1.00
- c = contest_factory(
+ c = raffle_contest_factory(
entry_rule=ContestEntryRule(
max_entry_amount_per_user=USDCent(10),
max_daily_entries_per_user=USDCent(8),
@@ -299,7 +335,7 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert "Entry would exceed max amount per user." in str(e.value)
@@ -312,7 +348,7 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert "Entry would exceed max amount per user per day." in str(e.value)
@@ -324,7 +360,7 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
# Then can't anymore
@@ -336,14 +372,18 @@ class TestRaffleContestCRUD:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
assert "Entry would exceed max amount per user per day." in str(e.value)
class TestRaffleContestUserViews:
def test_list_user_eligible_country(
- self, user_with_wallet: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_wallet: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
# No contests exists
cs = contest_manager.get_many_by_user_eligible(
@@ -352,7 +392,7 @@ class TestRaffleContestUserViews:
assert len(cs) == 0
# Create a contest. It'll be in the US/CA
- contest_factory(country_isos={"us", "ca"})
+ raffle_contest_factory(country_isos={"us", "ca"})
# Not eligible in mexico
cs = contest_manager.get_many_by_user_eligible(
@@ -365,7 +405,7 @@ class TestRaffleContestUserViews:
assert len(cs) == 1
# Create another, any country
- contest_factory(country_isos=None)
+ raffle_contest_factory(country_isos=None)
cs = contest_manager.get_many_by_user_eligible(
user=user_with_wallet, country_iso="mx"
)
@@ -376,9 +416,13 @@ class TestRaffleContestUserViews:
assert len(cs) == 2
def test_list_user_eligible(
- self, user_with_money: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_money: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
- c = contest_factory(
+ c = raffle_contest_factory(
end_condition=ContestEndCondition(target_entry_amount=USDCent(10)),
entry_rule=ContestEntryRule(
max_entry_amount_per_user=USDCent(1),
@@ -398,7 +442,7 @@ class TestRaffleContestUserViews:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
# User isn't eligible anymore
@@ -422,9 +466,13 @@ class TestRaffleContestUserViews:
assert len(contest_manager.get_winnings_by_user(user_with_money)) == 0
def test_list_user_winnings(
- self, user_with_money: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_money: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
- c = contest_factory(
+ c = raffle_contest_factory(
end_condition=ContestEndCondition(target_entry_amount=USDCent(100)),
)
entry = ContestEntry(
@@ -436,7 +484,7 @@ class TestRaffleContestUserViews:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)
# Contest ends after 100 entry, user enters 100 entry, user wins!
ws = contest_manager.get_winnings_by_user(user_with_money)
@@ -458,9 +506,13 @@ class TestRaffleContestCRUDCount:
# This is a COUNT contest. No cash moves. Not really fleshed out what we'd do with this.
@pytest.mark.skip
def test_enter(
- self, user_with_wallet: User, contest_factory, thl_lm, contest_manager
+ self,
+ user_with_wallet: User,
+ raffle_contest_factory: Callable[..., Contest],
+ thl_ledger_manager: ThlLedgerManager,
+ contest_manager: ContestManager,
):
- c = contest_factory(entry_type=ContestEntryType.COUNT)
+ c = raffle_contest_factory(entry_type=ContestEntryType.COUNT)
entry = ContestEntry(
entry_type=ContestEntryType.COUNT,
user=user_with_wallet,
@@ -470,5 +522,5 @@ class TestRaffleContestCRUDCount:
contest_uuid=c.uuid,
entry=entry,
country_iso="us",
- ledger_manager=thl_lm,
+ ledger_manager=thl_ledger_manager,
)