prusalink: extract press_button_and_verify fixture for button tests#170332
Open
heikkih wants to merge 11 commits into
Open
prusalink: extract press_button_and_verify fixture for button tests#170332heikkih wants to merge 11 commits into
heikkih wants to merge 11 commits into
Conversation
All four button tests followed the same shape: assert entity in `unknown` state, patch the API method, press the button, assert the mock was called, then patch with `side_effect=Conflict` and assert HomeAssistantError surfaces. Extract that into a single fixture so the tests only declare which entity/method pair they exercise. Per balloob's follow-up suggestion on home-assistant#170193: home-assistant#170193 (comment) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
9 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request refactors the PrusaLink button tests by extracting the repeated “press button + verify success + verify Conflict handling” logic into a reusable fixture, reducing duplication while keeping the same test coverage and scenarios.
Changes:
- Added a
press_button_and_verifyfixture to centralize button-press assertions and Conflict→HomeAssistantError behavior. - Updated existing button tests to use the new helper and removed unused
hass_client/ClientSessionGeneratorusage. - Kept the existing “continue button unavailable when printing” test as an explicit state assertion.
Per Copilot's review on home-assistant#170332: patching the base class `PrusaLinkUpdateCoordinator._fetch_data` is a no-op because each concrete coordinator overrides `_fetch_data`, so Python's MRO never resolves to the patched base-class method. The button press calls `coordinator.async_request_refresh()` on every coordinator (see `button.py:111-113`), so the original patch never actually prevented the refresh it claimed to. The refresh still succeeds silently against the already-mocked `mock_api` GET methods, which is why the tests pass without the patch. Tests stay green (6/6) and coverage on `button.py` remains 100%. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Per Copilot's review on home-assistant#170332: `AsyncMock.assert_awaited_once()` fails if the integration ever stops awaiting the async pyprusalink method, while `mock_calls == 1` would silently pass that regression. `patch()` auto-detects the patched method as async since 3.8, so `mock_meth` is already an `AsyncMock` and the new assertion is the strictly stronger check. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The continue_job button uses the entity_id with 'workshop_' prefix instead of just 'mock_title_'. See home-assistant#170560
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.
Proposed change
Follow-up to @balloob's review comment on #170193:
All four button tests in
tests/components/prusalink/test_button.pyfollowed the same shape:unknownstate.services.async_call, assert the mock was called exactly once.side_effect=Conflict, press again, assertHomeAssistantErrorsurfaces.That repetition is now extracted into a single fixture,
press_button_and_verify(entity_id, method), so each test only declares the entity/method pair it exercises plus which printer state it should be in.Copilot review follow-up
Two comments from the updated Copilot review were explicitly re-checked before finalizing:
assert_awaited_once()vscall_countassert_awaited_once()intentionally.PrusaLink.cancel_job,pause_job,resume_job, andcontinue_jobare coroutine functions and patch toAsyncMock.Removed base
_fetch_datapatchPrusaLinkUpdateCoordinator._fetch_data, but concrete coordinators override_fetch_data, so patching the base method does not intercept the real refresh path.Additionally, the helper fixture now has an explicit return type annotation:
Callable[[str, str], Awaitable[None]]Net effect
tests/components/prusalink/test_button.py: repetitive press-and-verify logic is centralized in one fixture.Type of change
Checklist
python -m pytest tests/components/prusalink/test_button.pypython -m mypy tests/components/prusalink/test_button.pyruff check tests/components/prusalink/test_button.pyprek run --files tests/components/prusalink/test_button.py