Skip to content

test: fix MockSensor do_command state tracking and add missing coverage#1188

Open
Shailesh katara (luckys00) wants to merge 5 commits into
viamrobotics:mainfrom
luckys00:fix-mock-sensor-state
Open

test: fix MockSensor do_command state tracking and add missing coverage#1188
Shailesh katara (luckys00) wants to merge 5 commits into
viamrobotics:mainfrom
luckys00:fix-mock-sensor-state

Conversation

@luckys00

Copy link
Copy Markdown

What this PR does

Fixes an inconsistency in the MockSensor class where do_command was dropping state parameters (timeout and extra), and adds the missing test coverage for do_command across the Sensor, Service, and Client test suites.

Why is it needed?

While get_readings and get_geometries properly save self.extra and self.timeout to the mock's state, the do_command implementation was bypassing this and simply returning the command. This makes it impossible to assert if custom commands with timeouts were actually processed correctly by the mock.

Additionally, this PR adds proper test coverage for do_command in test_sensor.py to ensure the timeout parameter successfully travels through the gRPC channel.

Changes Made:

  • Updated do_command in tests/mocks/components.py to properly track self.timeout and self.extra.
  • Added missing do_command tests in TestSensor, TestService, and TestClient (asserting timeout state logic while acknowledging gRPC drops extra for this specific request).

@viambot

viambot commented Apr 3, 2026

Copy link
Copy Markdown
Member

👋 Thanks for requesting a review from the team!

We aim to review PRs within one business day. If this is urgent
or blocking you, please reach out in #team-sdk and
we'll prioritize it.

@CLAassistant

CLAassistant commented Apr 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@njooma Naveed Jooma (njooma) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks for contributing! We are actually in the process of updating all our tests. If you want to take a look at how the test_camera.py tests are now implemented, we are moving towards that for all our resources.

If you like, you can update the sensor tests to use that framework!

@luckys00

Copy link
Copy Markdown
Author

Thanks for the review and the heads-up, Naveed Jooma (@njooma) ! It's great to hear about the new testing framework. I'll take a look at how test_camera.py is implemented and update the do_command tests for the Sensor components to match that new structure. Will push the changes shortly!

@luckys00

Copy link
Copy Markdown
Author

hey Naveed Jooma (@njooma) is this ok.
review it.

@njooma Naveed Jooma (njooma) self-requested a review May 20, 2026 14:50

@njooma Naveed Jooma (njooma) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is closer! Some notes that follow the new direction we want to go with testing

Comment thread tests/test_sensor.py
@@ -28,39 +28,16 @@
def sensor() -> MockSensor:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should no longer be using components in the mocks file. Instead, we should make a magic mock (see here)

Comment thread tests/test_sensor.py
assert geometries == GEOMETRIES


@pytest.fixture(scope="function")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the manager fixture

Comment thread tests/test_sensor.py
async with ChannelFor([service]) as channel:
client = SensorServiceStub(channel)
command = {"command": "args"}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set the return type of the mock's method to return an expected value (see here)

Comment thread tests/test_sensor.py

response: DoCommandResponse = await client.DoCommand(request, timeout=2.34)
result = struct_to_dict(response.result)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert the returned response contains the expected value set above (see here)

Comment thread tests/test_sensor.py

assert result == {"command": command}

assert sensor.timeout ==expected_grpc_timeout(2.34)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking fields on the mock, assert that the mocked's method is called with the expected inputs (see here)

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.

4 participants