Skip to content

test: reduce copy-paste in test_settings_contracts.py's Required/RoundTrip test classes #225

Description

@johanzander

Background

Follow-up from the code review on #224 (issue #219, Battery/Home settings unification). Two pairs of near-identical test classes were added to backend/tests/test_settings_contracts.py to mirror what #216 did for Price settings — flagged by the review as copy-paste that should eventually be a shared helper, but not worth blocking #224 on.

The problem

  1. TestBatteryRequiredFieldsConsistency and TestHomeRequiredFieldsConsistency are copy-paste of each other, differing only in the dataclass name, the exclusion set's name/contents (has_default vs internal_only), and the error string.
  2. TestBatterySettingsRoundTrip and TestHomeSettingsRoundTrip repeat the same three-step shape (mutate a fresh store dict → run through build_system_settings → build the "patch" side manually → compare BSM state) in two ~30-line classes. Genuinely different in one place (battery filters through _BATTERY_MODEL_ATTRS, home passes through unfiltered), but the store-setup/instantiate/compare boilerplate around that difference is duplicated.
  3. A comment above TestBatterySettingsRoundTrip/TestHomeSettingsRoundTrip says "mirrors arch: unify settings update paths to eliminate PRICE_STORE_TO_API translation registry #197's TestPriceSettingsRoundTrip" — that class doesn't exist anywhere in this repo yet (verified via full-tree grep). It only exists on the separate, unmerged #216 branch. Not factually wrong (it's a valid reference to prior art on another branch) but worth rewording once fix: unify Battery/Home/Price settings startup/PATCH paths, fix live efficiency-field bug (#197) #216 merges so a reader grepping for it doesn't waste time wondering if it was deleted.

Why it matters

When a third settings class needs the same "required fields match dataclass" or "startup/PATCH round-trip" guard (e.g. once #216 merges Price into this pattern), the copy-paste is likely to happen a third time, and any improvement to the assertion/error message has to be repeated in every copy.

Proposed fix

  • Extract a parametrized helper (or a single test taking (dataclass, required_fields, exclusions)) for the *RequiredFieldsConsistency pair.
  • Extract shared setup/compare helpers for the *SettingsRoundTrip pair, keeping the one genuinely different line (battery's _BATTERY_MODEL_ATTRS filter vs home's unfiltered passthrough) inline per class.
  • Once fix: unify Battery/Home/Price settings startup/PATCH paths, fix live efficiency-field bug (#197) #216 merges, reword the TestPriceSettingsRoundTrip comment reference (or fold Price into the same parametrized structure).

Files affected

  • backend/tests/test_settings_contracts.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    bot-analyzedTriage bot has processed this issueenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions