ref: composable test TC via tc_context context manager#5
Merged
Conversation
303ef3c to
21db576
Compare
settings_mock/TC were locked to function scope because they patched via
the function-scoped `mocker` fixture, so a consumer couldn't build a
session-scoped TC without copying fastloom internals into its conftest.
Extract the load_settings patch + Configs lifecycle into scope-agnostic
context managers (`patched_settings`, `tc_context`) using start()/stop(),
generic over the settings types like the old settings_mock, and make
settings_mock/TC thin fixtures over them. mock_authz still depends on
settings_mock; behaviour is unchanged. Consumers can now wrap tc_context
at whatever scope/autouse they need:
@pytest.fixture(scope="session", autouse=True)
def TC(service_settings, tenant_settings):
with tc_context(service_settings, tenant_settings) as tc:
yield tc
21db576 to
9a619a9
Compare
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.
Why
TCandsettings_mockwere locked to function scope — not by choice, but becausesettings_mockpatchesload_settingsvia the function-scopedmockerfixture, andTCdepends on it. A session-scoped fixture can't depend on a function-scoped one (ScopeMismatch), so a consumer that wants a single session-wideTC(settings are usually identical across a service's tests) has to copy fastloom's internals (theload_settingsmonkeypatch +Configssingleton lifecycle) into its own conftest. That's the rigidity.What
Extract the reusable logic into two scope-agnostic context managers (using
patch().start()/stop()instead ofmocker), and make the shipped fixtures thin wrappers over them:patched_settings(service_settings, tenant_settings)— patchesload_settingsonly.tc_context(service_settings, tenant_settings)—patched_settings+ theConfigssingleton build/teardown.settings_mock/TC— unchanged signatures, now thin fixtures over the context managers.mock_authzstill depends onsettings_mock; its behaviour is unchanged (no extraConfigsbuild per test). This is non-breaking —settings_mock/TCkeep the same names/params.Payoff
A consumer can now build
TCat whatever scope/autouse it needs, with no internals copied:Why context managers over a
mocker-based fixture: scope lives on the fixture, not the builder, andstart()/stop()ties the patch lifetime to the enclosing fixture's scope instead of welding it tomocker's function scope.Patch bump
0.4.41→0.4.42(plugin.json synced).🤖 Generated with Claude Code