Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/mocks/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -167,6 +169,8 @@ async def get_properties(self, *, timeout: Optional[float] = None, **kwargs) ->
return self.properties

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -207,6 +211,8 @@ async def get_properties(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.properties

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return command

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -296,6 +302,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -407,6 +415,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -473,6 +483,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Dict[str, Any]:
Expand Down Expand Up @@ -528,6 +540,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -599,6 +613,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -675,6 +691,8 @@ async def get_kinematics(
return self.kinematics

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -750,6 +768,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -889,6 +909,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -986,6 +1008,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -1039,6 +1063,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -1080,6 +1106,8 @@ async def get_readings(
return self.readings

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -1107,6 +1135,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -1147,6 +1177,8 @@ async def get_geometries(self, *, extra: Optional[Dict[str, Any]] = None, timeou
return self.geometries

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down Expand Up @@ -1184,6 +1216,8 @@ async def set_position(
self.position = position

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand All @@ -1203,6 +1237,8 @@ async def push(self, *, extra: Optional[Mapping[str, Any]] = None, timeout: Opti
self.pushed = True

async def do_command(self, command: Mapping[str, ValueTypes], *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
self.timeout = timeout
self.extra = kwargs.get("extra")
return {"command": command}

async def get_status(self, *, timeout: Optional[float] = None, **kwargs) -> Mapping[str, ValueTypes]:
Expand Down
43 changes: 16 additions & 27 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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")
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

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:
Expand All @@ -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"}

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)

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)

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)

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)



async def test_get_status(self, sensor: MockSensor, service: SensorRPCService):
async with ChannelFor([service]) as channel:
client = SensorServiceStub(channel)
Expand All @@ -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:
Expand Down