Skip to content

Refactor integration tests to use stateful fakes and fixtures#868

Open
allenporter wants to merge 1 commit into
Python-roborock:mainfrom
allenporter:refactor-roborock-testing-integration
Open

Refactor integration tests to use stateful fakes and fixtures#868
allenporter wants to merge 1 commit into
Python-roborock:mainfrom
allenporter:refactor-roborock-testing-integration

Conversation

@allenporter

@allenporter allenporter commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Motivation

Following the merge of the roborock.testing library (#860), this PR refactors integration tests in test_device_manager.py to use FakeRoborockCloud and V1VacuumSimulator. In doing so, we introduce some usability improvements to roborock.testing to simplify test implementation for both internal and downstream consumers (like the Home Assistant integration):

  1. Eliminated Nested Patcher Contexts: Refactored patch_device_manager to be exposed as a standard pytest fixture. This removes the need for deep with cloud.patch_device_manager(): indentation, especially when asserting reconnection backoffs in background tasks which must run across the entire lifespan of the test.
  2. Simplified Transport-Level Error Injection: Added a first-class inject_error(exception) and clear_error() API to FakeChannel. Test writers can now easily simulate and recover from connection/subscription/publish failures without hacky manual modification of mock side_effect attributes and private wrappers.
  3. Type-Safe EAPI Response Mocking: Modified set_homes_response() on FakeWebApiClient to accept the HomeData dataclass directly (serializing it automatically under the hood). Added a get_default_home_data() helper so tests can retrieve the default registered layout and copy/modify it using standard dataclasses.replace rather than building raw dictionaries from scratch.
  4. Unified Callback Interception: Refactored the simulator's RPC routing to use a first-class publish_handler registration property on FakeChannel instead of overriding mock attributes directly, keeping the mock assertions clean and orthogonal.
  5. Self-Contained Sleep Mocking: Added reconnection sleep backoff patching directly inside patch_device_manager()'s context manager. Downstream integration test suites run instantly out-of-box without needing duplicate sleep-patching fixtures.

Changes Made

  • roborock.testing:
    • channel.py: Added publish_handler, inject_error(exception), clear_error(), and _default_publish_handler(message).
    • cloud.py: Added get_default_home_data() and refactored set_homes_response(home_data). Patched MIN_BACKOFF_INTERVAL and MAX_BACKOFF_INTERVAL inside patch_device_manager().
    • simulator.py: Refactored simulator routing to register on publish_handler instead of mutating mock objects.
  • tests:
    • conftest.py: Created shared fixtures cloud, fake_device, and patch_device_manager to be globally accessible.
    • test_device_manager.py: Completely refactored integration tests to use stateful fakes, fixtures, error injection, and dataclass-based home details response overrides.

@allenporter allenporter force-pushed the refactor-roborock-testing-integration branch from e5387b2 to 0f60d33 Compare July 7, 2026 18:32
@allenporter allenporter marked this pull request as draft July 7, 2026 18:33
@allenporter allenporter force-pushed the refactor-roborock-testing-integration branch from 0f60d33 to 6ad41f2 Compare July 7, 2026 18:34
@allenporter allenporter marked this pull request as ready for review July 7, 2026 18:36
@allenporter allenporter force-pushed the refactor-roborock-testing-integration branch from 6ad41f2 to bdb8b88 Compare July 7, 2026 18:40
@allenporter allenporter force-pushed the refactor-roborock-testing-integration branch from bdb8b88 to c90b396 Compare July 7, 2026 18:53
@allenporter allenporter requested a review from Lash-L July 7, 2026 20:34
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.

2 participants