|
| 1 | +import pytest |
| 2 | +from grpclib.testing import ChannelFor |
| 3 | + |
| 4 | +from viam.components.button import ButtonClient |
| 5 | +from viam.components.button.service import ButtonRPCService |
| 6 | +from viam.gen.component.button.v1.button_pb2 import ( |
| 7 | + PushRequest |
| 8 | +) |
| 9 | +from viam.proto.common import ( |
| 10 | + DoCommandRequest, |
| 11 | + DoCommandResponse, |
| 12 | +) |
| 13 | +from viam.proto.component.button import ButtonServiceStub |
| 14 | +from viam.resource.manager import ResourceManager |
| 15 | +from viam.utils import dict_to_struct, struct_to_dict |
| 16 | + |
| 17 | +from . import loose_approx |
| 18 | +from .mocks.components import MockButton |
| 19 | + |
| 20 | +EXTRA_PARAMS = {"foo": "bar", "baz": [1, 2, 3]} |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="function") |
| 24 | +def button() -> MockButton: |
| 25 | + return MockButton(name="button") |
| 26 | + |
| 27 | + |
| 28 | +class TestButton: |
| 29 | + async def test_push(self, button): |
| 30 | + await button.push(timeout=1.23, extra=EXTRA_PARAMS) |
| 31 | + assert button.pushed is True |
| 32 | + assert button.timeout == loose_approx(1.23) |
| 33 | + assert button.extra == EXTRA_PARAMS |
| 34 | + |
| 35 | + async def test_do(self, button): |
| 36 | + command = {"command": "args"} |
| 37 | + resp = await button.do_command(command) |
| 38 | + assert resp == {"command": command} |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="function") |
| 42 | +def manager(button) -> ResourceManager: |
| 43 | + return ResourceManager([button]) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="function") |
| 47 | +def service(manager) -> ButtonRPCService: |
| 48 | + return ButtonRPCService(manager) |
| 49 | + |
| 50 | + |
| 51 | +class TestService: |
| 52 | + async def test_push(self, button, service): |
| 53 | + async with ChannelFor([service]) as channel: |
| 54 | + client = ButtonServiceStub(channel) |
| 55 | + request = PushRequest(name=button.name, extra=dict_to_struct(EXTRA_PARAMS)) |
| 56 | + assert button.extra is None |
| 57 | + await client.Push(request, timeout=1.23) |
| 58 | + assert button.pushed is True |
| 59 | + assert button.extra == EXTRA_PARAMS |
| 60 | + assert button.timeout == loose_approx(1.23) |
| 61 | + |
| 62 | + async def test_do(self, button: MockButton, service: ButtonRPCService): |
| 63 | + async with ChannelFor([service]) as channel: |
| 64 | + client = ButtonServiceStub(channel) |
| 65 | + command = {"command": "args"} |
| 66 | + request = DoCommandRequest(name=button.name, command=dict_to_struct(command)) |
| 67 | + response: DoCommandResponse = await client.DoCommand(request) |
| 68 | + result = struct_to_dict(response.result) |
| 69 | + assert result == {"command": command} |
| 70 | + |
| 71 | + |
| 72 | +class TestClient: |
| 73 | + async def test_push(self, button, service): |
| 74 | + async with ChannelFor([service]) as channel: |
| 75 | + client = ButtonClient(button.name, channel) |
| 76 | + assert button.extra is None |
| 77 | + await client.push(timeout=3.45, extra=EXTRA_PARAMS) |
| 78 | + assert button.pushed is True |
| 79 | + assert button.extra == EXTRA_PARAMS |
| 80 | + assert button.timeout == loose_approx(3.45) |
| 81 | + |
| 82 | + async def test_do(self, button, manager, service): |
| 83 | + async with ChannelFor([service]) as channel: |
| 84 | + client = ButtonClient(button.name, channel) |
| 85 | + command = {"command": "args"} |
| 86 | + resp = await client.do_command(command) |
| 87 | + assert resp == {"command": command} |
0 commit comments