Spaces:
Running on T4
Running on T4
Download backend/tests/unit/test_billing_status.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.08 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/unit/test_billing_status.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/unit/test_billing_status.py
-
curl -L -o test_billing_status.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/unit/test_billing_status.py
4.08 kB
| """Store data to subscription status: pure mapping rules.""" | |
| from datetime import UTC, datetime, timedelta | |
| import pytest | |
| from app.integrations.app_store import AppleNotification, AppleTransaction | |
| from app.integrations.play_store import PlayLineItem, PlaySubscription, parse_subscription | |
| from app.modules.billing.service import apple_state, play_state | |
| NOW = datetime(2026, 10, 22, 12, tzinfo=UTC) | |
| LATER, EARLIER = NOW + timedelta(days=20), NOW - timedelta(days=1) | |
| PRO = ["pro_monthly"] | |
| def _tx(expires=LATER, revoked=None) -> AppleTransaction: | |
| return AppleTransaction("1000", "pro_monthly", expires, revoked) | |
| def _note(kind, subtype=None, grace=None, tx=None) -> AppleNotification: | |
| return AppleNotification(kind, subtype, tx or _tx(), grace) | |
| def test_apple_transaction_states(): | |
| assert apple_state(_tx(), NOW).status == "active" | |
| assert apple_state(_tx(EARLIER), NOW).status == "expired" | |
| assert apple_state(_tx(revoked=NOW), NOW).status == "refunded" | |
| assert apple_state(_tx(expires=None), NOW) is None | |
| def test_apple_notification_states(kind, subtype, status): | |
| tx = _tx(EARLIER) if kind == "DID_FAIL_TO_RENEW" else _tx() | |
| assert apple_state(tx, NOW, _note(kind, subtype, tx=tx)).status == status | |
| def test_apple_grace_period_extends_expiry(): | |
| tx = _tx(EARLIER) | |
| state = apple_state(tx, NOW, _note("DID_FAIL_TO_RENEW", "GRACE_PERIOD", grace=LATER, tx=tx)) | |
| assert (state.status, state.expires_at) == ("grace", LATER) | |
| def _sub(state="ACTIVE", expires=LATER, order="GPA.1111-2222-3333-44444..3", items=None) -> PlaySubscription: | |
| return PlaySubscription(state, items or (PlayLineItem("pro_monthly", expires),), order, None, True) | |
| def test_play_states(state, expires, status): | |
| result = play_state(_sub(state, expires), "token", PRO, NOW) | |
| assert (result.status, result.expires_at, result.platform) == (status, expires, "android") | |
| def test_play_stable_id_is_the_base_order_id(): | |
| assert play_state(_sub(), "token", PRO, NOW).original_transaction_id == "GPA.1111-2222-3333-44444" | |
| assert play_state(_sub(order="GPA.9"), "t", PRO, NOW).original_transaction_id == "GPA.9" | |
| assert play_state(_sub(order=None), "token-x", PRO, NOW).original_transaction_id == "token-x" | |
| def test_play_pending_is_not_a_purchase(): | |
| assert play_state(_sub("PENDING"), "t", PRO, NOW) is None | |
| assert play_state(PlaySubscription("ACTIVE", (), None, None, False), "t", PRO, NOW) is None | |
| def test_play_prefers_the_pro_line_item(): | |
| items = (PlayLineItem("other", LATER + timedelta(days=5)), PlayLineItem("pro_monthly", LATER)) | |
| assert play_state(_sub(items=items), "t", PRO, NOW).product_id == "pro_monthly" | |
| def test_parse_subscription_v2_response(): | |
| sub = parse_subscription({ | |
| "subscriptionState": "SUBSCRIPTION_STATE_IN_GRACE_PERIOD", | |
| "latestOrderId": "GPA.1-2-3-4..0", | |
| "linkedPurchaseToken": "old-token", | |
| "acknowledgementState": "ACKNOWLEDGEMENT_STATE_PENDING", | |
| "lineItems": [{"productId": "pro_monthly", "expiryTime": "2026-11-22T12:00:00.123456789Z"}], | |
| }) | |
| assert sub.state == "IN_GRACE_PERIOD" | |
| assert sub.linked_purchase_token == "old-token" and not sub.acknowledged | |
| assert sub.line_items[0].expires_at == datetime(2026, 11, 22, 12, 0, 0, 123456, tzinfo=UTC) | |