-
Notifications
You must be signed in to change notification settings - Fork 69
test: fix MockSensor do_command state tracking and add missing coverage #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0811603
cd08eae
4a73f12
b662cf4
35ec330
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,39 +28,16 @@ | |
| def sensor() -> MockSensor: | ||
| return MockSensor(name="sensor", result=READINGS) | ||
|
|
||
|
|
||
| class TestSensor: | ||
| async def test_get_readings(self, sensor): | ||
| assert sensor.extra is None | ||
| readings = await sensor.get_readings(extra=EXTRA_PARAMS, timeout=1.23) | ||
| assert readings == READINGS | ||
| assert sensor.extra == EXTRA_PARAMS | ||
| assert sensor.timeout == expected_grpc_timeout(1.23) | ||
|
|
||
| async def test_do(self, sensor): | ||
| command = {"command": "args"} | ||
| resp = await sensor.do_command(command) | ||
| assert resp == {"command": command} | ||
|
|
||
| async def test_get_status(self, sensor): | ||
| status = await sensor.get_status() | ||
| assert status == {} | ||
|
|
||
| async def test_get_geometries(self, sensor): | ||
| geometries = await sensor.get_geometries() | ||
| assert geometries == GEOMETRIES | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the |
||
| def manager(sensor) -> ResourceManager: | ||
| return ResourceManager([sensor]) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function") | ||
| def service(manager) -> SensorRPCService: | ||
| return SensorRPCService(manager) | ||
|
|
||
|
|
||
|
|
||
| class TestService: | ||
| async def test_get_readings(self, sensor, service): | ||
| async with ChannelFor([service]) as channel: | ||
|
|
@@ -76,11 +53,18 @@ async def test_do(self, sensor: MockSensor, service: SensorRPCService): | |
| async with ChannelFor([service]) as channel: | ||
| client = SensorServiceStub(channel) | ||
| command = {"command": "args"} | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
| request = DoCommandRequest(name=sensor.name, command=dict_to_struct(command)) | ||
| response: DoCommandResponse = await client.DoCommand(request) | ||
| assert sensor.extra is None | ||
|
|
||
| response: DoCommandResponse = await client.DoCommand(request, timeout=2.34) | ||
| result = struct_to_dict(response.result) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the returned response contains the expected value set above (see here) |
||
| assert result == {"command": command} | ||
|
|
||
| assert sensor.timeout ==expected_grpc_timeout(2.34) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
|
|
||
|
|
||
| async def test_get_status(self, sensor: MockSensor, service: SensorRPCService): | ||
| async with ChannelFor([service]) as channel: | ||
| client = SensorServiceStub(channel) | ||
|
|
@@ -106,12 +90,17 @@ async def test_get_readings(self, sensor, service): | |
| assert sensor.extra == EXTRA_PARAMS | ||
| assert sensor.timeout == expected_grpc_timeout(3.45) | ||
|
|
||
| async def test_do(self, sensor, manager, service): | ||
| async def test_do(self, sensor, service): | ||
| async with ChannelFor([service]) as channel: | ||
| client = SensorClient(sensor.name, channel) | ||
| command = {"command": "args"} | ||
| resp = await client.do_command(command) | ||
|
|
||
|
|
||
| resp = await client.do_command(command, timeout=3.45) | ||
| assert resp == {"command": command} | ||
| assert sensor.timeout ==expected_grpc_timeout(3.45) | ||
|
|
||
|
|
||
|
|
||
| async def test_get_status(self, sensor, service): | ||
| async with ChannelFor([service]) as channel: | ||
|
|
||
There was a problem hiding this comment.
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
mocksfile. Instead, we should make a magic mock (see here)