Skip to content

ref: composable test TC via tc_context context manager#5

Merged
aradng merged 1 commit into
mainfrom
ref/composable-test-tc
Jun 27, 2026
Merged

ref: composable test TC via tc_context context manager#5
aradng merged 1 commit into
mainfrom
ref/composable-test-tc

Conversation

@aradng

@aradng aradng commented Jun 26, 2026

Copy link
Copy Markdown
Owner

Why

TC and settings_mock were locked to function scope — not by choice, but because settings_mock patches load_settings via the function-scoped mocker fixture, and TC depends on it. A session-scoped fixture can't depend on a function-scoped one (ScopeMismatch), so a consumer that wants a single session-wide TC (settings are usually identical across a service's tests) has to copy fastloom's internals (the load_settings monkeypatch + Configs singleton 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 of mocker), and make the shipped fixtures thin wrappers over them:

  • patched_settings(service_settings, tenant_settings) — patches load_settings only.
  • tc_context(service_settings, tenant_settings)patched_settings + the Configs singleton build/teardown.
  • settings_mock / TC — unchanged signatures, now thin fixtures over the context managers.

mock_authz still depends on settings_mock; its behaviour is unchanged (no extra Configs build per test). This is non-breakingsettings_mock/TC keep the same names/params.

Payoff

A consumer can now build TC at whatever scope/autouse it needs, with no internals copied:

@pytest.fixture(scope="session", autouse=True)
def TC(service_settings, tenant_settings):
    with tc_context(service_settings, tenant_settings) as tc:
        yield tc

Why context managers over a mocker-based fixture: scope lives on the fixture, not the builder, and start()/stop() ties the patch lifetime to the enclosing fixture's scope instead of welding it to mocker's function scope.

Patch bump 0.4.410.4.42 (plugin.json synced).

🤖 Generated with Claude Code

@aradng aradng force-pushed the ref/composable-test-tc branch 2 times, most recently from 303ef3c to 21db576 Compare June 26, 2026 23:57
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
@aradng aradng force-pushed the ref/composable-test-tc branch from 21db576 to 9a619a9 Compare June 27, 2026 00:07
@aradng aradng closed this Jun 27, 2026
@aradng aradng reopened this Jun 27, 2026
@aradng aradng merged commit 5ec71a5 into main Jun 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant