diff options
| author | stuppie | 2026-09-04 14:22:53 -0600 |
|---|---|---|
| committer | stuppie | 2026-09-04 14:22:53 -0600 |
| commit | 9ed9d9822d006a0d71bf7559c2ad0968bb243c8c (patch) | |
| tree | 39aaad5392e0a135e370089c704875e3b7c5c54d | |
| parent | 63f9d47b2774993e5e749d9d9c64915ad0d2de50 (diff) | |
| download | generalresearch-9ed9d9822d006a0d71bf7559c2ad0968bb243c8c.tar.gz generalresearch-9ed9d9822d006a0d71bf7559c2ad0968bb243c8c.zip | |
fix more tests
| -rw-r--r-- | tests/managers/gr/test_business.py | 12 | ||||
| -rw-r--r-- | tests/managers/test_events.py | 46 | ||||
| -rw-r--r-- | tests/managers/thl/test_ledger/test_thl_lm_bp_payout.py | 64 | ||||
| -rw-r--r-- | tests/managers/thl/test_wall_manager.py | 12 |
4 files changed, 67 insertions, 67 deletions
diff --git a/tests/managers/gr/test_business.py b/tests/managers/gr/test_business.py index 70f50ca..022086a 100644 --- a/tests/managers/gr/test_business.py +++ b/tests/managers/gr/test_business.py @@ -58,10 +58,10 @@ class TestBusinessBankAccountManager: class TestBusinessAddressManager: def test_create( - self, gr_business: Business, business_address_manager: BusinessAddressManager + self, gr_business: Business, gr_business_address_manager: BusinessAddressManager ): - - res = business_address_manager.create( + assert gr_business.id + res = gr_business_address_manager.create( uuid=uuid4().hex, business_id=gr_business.id ) assert isinstance(res, BusinessAddress) @@ -110,7 +110,7 @@ class TestBusinessManager: self, gr_business_manager: BusinessManager, gr_user: GRUser, - team_manager: TeamManager, + gr_team_manager: TeamManager, gr_membership_manager: MembershipManager, gr_business_factory: Callable[..., Business], gr_team_factory: Callable[..., Team], @@ -136,13 +136,13 @@ class TestBusinessManager: # Add the Business to the Team... now the Business should be available # to the gr_user - team_manager.add_business(team=t1, business=b1) + gr_team_manager.add_business(team=t1, business=b1) res = gr_business_manager.get_by_user_id(user_id=gr_user.id) assert len(res) == 1 # Add another Business to the Team! b2 = gr_business_factory() - team_manager.add_business(team=t1, business=b2) + gr_team_manager.add_business(team=t1, business=b2) res = gr_business_manager.get_by_user_id(user_id=gr_user.id) assert len(res) == 2 diff --git a/tests/managers/test_events.py b/tests/managers/test_events.py index e256876..5ebd015 100644 --- a/tests/managers/test_events.py +++ b/tests/managers/test_events.py @@ -19,6 +19,7 @@ from generalresearch.models.events import ( MessageKind, ) from generalresearch.models.legacy.bucket import Bucket +from generalresearch.models.thl import Product from generalresearch.models.thl.definitions import Status, StatusCode1 from generalresearch.models.thl.session import Session, Wall from generalresearch.models.thl.user import User @@ -41,7 +42,6 @@ def event_subscriber(thl_redis_config: RedisConfig, product_id: str) -> EventSub class TestActiveUsers: - def test_run_empty(self, event_manager: EventManager, product_id: str): res = event_manager.get_user_stats(product_id) assert res == { @@ -54,7 +54,7 @@ class TestActiveUsers: def test_run( self, event_manager: EventManager, - product_id: str, + product_factory, user_factory: Callable[..., User], ): event_manager.clear_global_user_stats() @@ -65,7 +65,7 @@ class TestActiveUsers: event_manager.handle_user(user1) event_manager.handle_user(user1) - res = event_manager.get_user_stats(product_id) + res = event_manager.get_user_stats(user1.product_id) assert res == { "active_users_last_1h": 1, "active_users_last_24h": 1, @@ -81,8 +81,8 @@ class TestActiveUsers: } # Create a 2nd user in another product - product_id2 = uuid4().hex - user2: User = user_factory(product_id=product_id2) + product2 = product_factory() + user2: User = user_factory(product=product2) assert isinstance(user2, User) assert isinstance(user2.created, datetime) # Change to say user was created >24 hrs ago @@ -90,14 +90,14 @@ class TestActiveUsers: event_manager.handle_user(user2) # And now each have 1 active user - assert event_manager.get_user_stats(product_id) == { + assert event_manager.get_user_stats(user1.product_id) == { "active_users_last_1h": 1, "active_users_last_24h": 1, "signups_last_24h": 1, "in_progress_users": 0, } # user2 was created older than 24hrs ago - assert event_manager.get_user_stats(product_id2) == { + assert event_manager.get_user_stats(user2.product_id) == { "active_users_last_1h": 1, "active_users_last_24h": 1, "signups_last_24h": 0, @@ -113,13 +113,14 @@ class TestActiveUsers: def test_inprogress( self, - event_manager: EventSubscriber, - product_id: str, + event_manager: EventManager, user_factory: Callable[..., User], + product ): event_manager.clear_global_user_stats() - user1: User = user_factory() - user2: User = user_factory() + user1: User = user_factory(product=product) + user2: User = user_factory(product=product) + product_id = product.id # No matter how many times we do this, they're only active once event_manager.mark_user_inprogress(user1) @@ -142,11 +143,11 @@ class TestActiveUsers: def test_expiry( self, event_manager: EventManager, - product_id: str, user_factory: Callable[..., User], ): event_manager.clear_global_user_stats() user1: User = user_factory() + product_id = user1.product_id event_manager.handle_user(user1) event_manager.mark_user_inprogress(user1) sec_24hr = timedelta(hours=24).total_seconds() @@ -171,7 +172,6 @@ class TestActiveUsers: class TestSessionStats: - def test_run_empty(self, event_manager: EventManager, product_id: str): res = event_manager.get_session_stats(product_id) assert res == { @@ -194,14 +194,15 @@ class TestSessionStats: def test_run( self, event_manager: EventManager, - product_id: str, + product_factory: Callable[..., Product], user_factory: Callable[..., User], utc_now: datetime, utc_hour_ago: datetime, ): event_manager.clear_global_session_stats() - - user: User = user_factory() + product = product_factory() + product_id = product.id + user: User = user_factory(product=product) session = Session( country_iso="us", started=utc_hour_ago + timedelta(minutes=10), @@ -278,29 +279,29 @@ class TestSessionStats: field_name = str(field) assert res == {field_name: "1"} assert ( - 3600 - 60 < event_manager.redis_client.httl(name, field_name)[0] < 3600 + 60 + 3600 - 61 < event_manager.redis_client.httl(name, field_name)[0] < 3600 + 60 ) # Second BP, fail - product_id2 = uuid4().hex - user2: User = user_factory(product_id=product_id2) + product2 = product_factory() + user2: User = user_factory(product=product2) session3 = Session( country_iso="us", started=utc_now - timedelta(minutes=1), user=user2, ) - event_manager.session_on_enter(session=session3, user=user) + event_manager.session_on_enter(session=session3, user=user2) session3.update( finished=utc_now, status=Status.FAIL, status_code_1=StatusCode1.BUYER_FAIL, ) - event_manager.session_on_finish(session=session3, user=user) + event_manager.session_on_finish(session=session3, user=user2) avg_loi_complete = ( round(session.elapsed.total_seconds()) + round(session2.elapsed.total_seconds()) ) / 2 - assert event_manager.get_session_stats(product_id) == { + assert event_manager.get_global_session_stats() == { "session_enters_last_1h": 2, "session_enters_last_24h": 3, "session_fails_last_1h": 1, @@ -456,6 +457,7 @@ class TestTaskStatsManager: class TestChannelsSubscriptions: + @pytest.mark.skip("sits there doing nothing forever? todo") def test_stats_worker( self, event_manager: EventManager, 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 643731c..de368d8 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 @@ -54,19 +54,17 @@ def broken_release(self, *args, **kwargs): class TestThlLedgerManagerBPPayout: + @pytest.fixture(autouse=True) + def setup(self, create_main_accounts): + create_main_accounts() def test_create_tx_with_bp_payment( self, user_factory: Callable[..., User], product_user_wallet_no: Product, - create_main_accounts: Callable[..., None], caplog, 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_user_wallet_no) @@ -137,8 +135,9 @@ class TestThlLedgerManagerBPPayout: ) payoutevent_uuid = uuid4().hex - with caplog.at_level(logging.INFO), pytest.raises( - LedgerTransactionConditionFailedError + with ( + caplog.at_level(logging.INFO), + pytest.raises(LedgerTransactionConditionFailedError), ): thl_ledger_manager.create_tx_bp_payout( user.product, @@ -202,8 +201,9 @@ class TestThlLedgerManagerBPPayout: assert balance == int(rand_amount) * -1 # Test some basic assertions - with caplog.at_level(logging.INFO), pytest.raises( - expected_exception=LedgerTransactionConditionFailedError + with ( + caplog.at_level(logging.INFO), + pytest.raises(expected_exception=LedgerTransactionConditionFailedError), ): thl_ledger_manager.create_tx_bp_payout( product=product, @@ -316,7 +316,10 @@ class TestThlLedgerManagerBPPayout: ) def test_create_tx_redis_lock_release_error( - self, product: Product, thl_ledger_manager: ThlLedgerManager + self, + product: Product, + thl_ledger_manager: ThlLedgerManager, + monkeypatch: pytest.MonkeyPatch, ): rand_amount: USDCent = USDCent(randint(100, 1_000)) payoutevent_uuid = uuid4().hex @@ -329,18 +332,16 @@ class TestThlLedgerManagerBPPayout: product, rand_amount * USDCent(2), now, direction=Direction.CREDIT ) - original_acquire = Lock.acquire - original_release = Lock.release - Lock.acquire = broken_acquire - # Create TX will fail on lock enter, no tx will actually get created - with pytest.raises(expected_exception=Exception) as e: - thl_ledger_manager.create_tx_bp_payout( - product=product, - amount=rand_amount, - payoutevent_uuid=payoutevent_uuid, - created=datetime.now(tz=UTC), - ) + with monkeypatch.context() as m: + m.setattr(Lock, "acquire", broken_acquire) + with pytest.raises(expected_exception=Exception) as e: + thl_ledger_manager.create_tx_bp_payout( + product=product, + amount=rand_amount, + payoutevent_uuid=payoutevent_uuid, + created=datetime.now(tz=UTC), + ) assert e.type is LedgerTransactionCreateError assert str(e.value) == "Redis error: Simulated timeout during acquire" txs = thl_ledger_manager.get_tx_filtered_by_account( @@ -349,17 +350,16 @@ class TestThlLedgerManagerBPPayout: txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"] assert len(txs) == 0 - Lock.acquire = original_acquire - Lock.release = broken_release - # Create TX will fail on lock exit, after the tx was created! - with pytest.raises(expected_exception=Exception) as e: - thl_ledger_manager.create_tx_bp_payout( - product=product, - amount=rand_amount, - payoutevent_uuid=payoutevent_uuid, - created=datetime.now(tz=UTC), - ) + with monkeypatch.context() as m: + m.setattr(Lock, "release", broken_release) + with pytest.raises(expected_exception=Exception) as e: + thl_ledger_manager.create_tx_bp_payout( + product=product, + amount=rand_amount, + payoutevent_uuid=payoutevent_uuid, + created=datetime.now(tz=UTC), + ) assert e.type is LedgerTransactionReleaseLockError assert str(e.value) == "Redis error: Simulated timeout during release" @@ -369,11 +369,9 @@ class TestThlLedgerManagerBPPayout: ) txs = [tx for tx in txs if tx.metadata["tx_type"] != "plug"] assert len(txs) == 1 - Lock.release = original_release class TestPayoutEventManagerBPPayout: - def test_create( self, product: Product, diff --git a/tests/managers/thl/test_wall_manager.py b/tests/managers/thl/test_wall_manager.py index 29d2660..777252f 100644 --- a/tests/managers/thl/test_wall_manager.py +++ b/tests/managers/thl/test_wall_manager.py @@ -248,17 +248,17 @@ class TestWallCacheManager: self, wall_cache_manager: WallCacheManager, user: User, - session_factory: Callable[..., Session], + bare_session_factory: Callable[..., Session], wall_factory: Callable[..., Wall], ): start1 = datetime.now(UTC) - timedelta(hours=3) start2 = datetime.now(UTC) - timedelta(hours=2) start3 = datetime.now(UTC) - timedelta(hours=1) - session = session_factory(started=start1, user=user) + session = bare_session_factory(started=start1, user=user) wall_factory( session_id=session.id, - user_id=session.user_id, + user=session.user, started=start1, req_cpi=Decimal("1.23"), req_survey_id="11111", @@ -274,7 +274,7 @@ class TestWallCacheManager: wall_factory( session_id=session.id, - user_id=session.user_id, + user=session.user, started=start2, req_cpi=Decimal("1.23"), req_survey_id="22222", @@ -298,10 +298,10 @@ class TestWallCacheManager: attempts10000 = [attempts[0]] * 6000 wall_cache_manager.update_attempts_redis_(attempts10000, user_id=user.user_id) - session = session_factory(started=start3, user=user) + session = bare_session_factory(started=start3, user=user) wall_factory( session_id=session.id, - user_id=session.user_id, + user=session.user, started=start3, req_cpi=Decimal("1.23"), req_survey_id="33333", |
