Repository navigation
test(payment): pay the routers.payment import once per file, not per test - #23537
Merged
Merged
Conversation
…test Each of the three declined-proration tests re-imported the full real module graph (stripe SDK ~2s CPU, FastAPI/pydantic, models.users, config.*) inside its call phase because the per-test fixture wiped sys.modules after every test. That tripped the fast-unit duration guard at 2.15-2.48s against the 1.00s CPU budget (CI run 40118719182, Backend unit shard 6/6, on a PR that did not touch this file). Move the stub install + routers.payment import into a module-scoped fixture: paid once, in fixture setup, which the guard intentionally excludes from per-test CPU. Reset the shared stub mocks' call state per test so every test still starts from uncalled mocks -- the same isolation the per-test reload provided. Assertions unchanged. Stub modules now replace sys.modules entries unconditionally instead of writing mock attributes onto already-imported real modules, so a batched pytest session no longer carries those mutations past teardown. Measured call-phase CPU per test: 2.35-2.51s -> 0.00s (<5ms); file wall 11.05s -> ~2.4s in the file-isolation runner. Removes the three stopgap fast_unit_duration_allowlist entries this file gained in #23512: the tests are now genuinely fast and stay guard-visible.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Luna round 1 (PR #23537, P2): in a batched pytest session the real utils.subscription/utils.stripe can already be bound as attributes of the utils package when the fixture runs, and `from utils import subscription` resolves through the parent attribute before the sys.modules submodule fallback. routers.payment could then bind the real module for the subscription_utils/stripe_utils aliases, so a future test driving a checkout endpoint through this fixture would run real subscription checks instead of the configured mocks. Set utils.stripe/utils.subscription to the stub modules for the fixture window and restore the original attributes at teardown. Verified: file-isolation guard run still green (4 passed, per-test call CPU < 5ms); batched duo with test_subscription_lapsed_paid_heal still 10 passed with real modules restored afterwards.
#23527 landed a parallel guard fix for this file on main (per-test payment_router fixture calling the old _load_payment_router in fixture setup). Keep its public shape -- tests take a payment_router fixture and alias it locally -- but back it with this lane's module-scoped fixture: one import per file instead of one per test, shared-mock call-state reset between tests, unconditional stub replacement (no real-module attribute mutation in batched sessions), and utils.stripe/utils.subscription parent pinning. Drops main's now-dead per-test fixture. Allowlist entries for this file stay removed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
tests/unit/test_payment_declined_proration.pytripped the fast-unit CPU duration guard on CI run 40118719182 (Backend unit shard 6/6, a pyright-fix PR that does not touch this file): 2.15–2.48s per test against the 1.00s budget.Root cause: the three router tests each call
_load_payment_router(), which re-importsrouters.paymentfrom scratch, and the autouse fixture wipes every module imported during a test out ofsys.modulesafterwards. Every test therefore re-paid the full real import graph inside its call phase — the exact signal the guard measures. Profiled under the same load:import stripealone ≈ 2.1s CPU,import fastapi(+pydantic) ≈ 0.9s,models.users≈ 0.05s.Change
routers.paymentimport into a module-scoped fixture: the import graph is paid once per file, in fixture setup. The duration guard measures call-phase CPU only by design ("shared class/file setup ... is not misattributed as a per-test regression" —backend/tests/conftest.py).reset_mock(return_value=False, side_effect=False)), so every test starts from uncalled mocks with the once-installed stub configuration intact — the same isolation the per-test reload provided. Test 2 assertsupdate_user_subscriptioncalled once; tests 3–4 assert it not called, so leaky state would fail loudly.sys.modulesentries unconditionally instead of writing mock attributes onto already-imported real modules. In a batched (non-file-isolated) pytest session the old pattern permanently mutated real modules (utils.subscription.get_plan_type_from_price_idbecame a bareMagicMock, which breakstest_subscription_lapsed_paid_heal.py::test_lapsed_paid_heals_from_stripe_subscription_id_directly— reproduced with the pre-change file; that pre-existing hazard is owned bytest_payment_promotion_codes.py, unchanged here). This file now leaves zero real-module mutations behind.Measured (backend/test.sh file-isolation runner, BACKEND_UNIT_TEST_FILE_LIST, nice -n 19)
test_subscription_lapsed_paid_heal.py: 10 passed, no stub leak.Failure-Class: none
UI-Evidence: none -- reason: backend test files only, no web/admin/app changes
utilspackage attributesstripe/subscriptionto the stub modules for the fixture window (with teardown restore), sofrom utils import stripe|subscriptioninsiderouters.paymentbinds the stubs even in a batched session where the real submodules are already attached to the package.