|
1 | 1 | from unittest import mock |
2 | 2 |
|
3 | 3 | import enapter |
4 | | -import tests |
5 | 4 |
|
6 | 5 |
|
7 | | -async def test_publish_telemetry(fake: tests.FakeDataGenerator) -> None: |
8 | | - hardware_id = fake.hardware_id() |
9 | | - channel_id = fake.channel_id() |
10 | | - timestamp = fake.timestamp() |
| 6 | +async def test_subscribe_to_command_requests() -> None: |
| 7 | + @enapter.async_.generator |
| 8 | + async def subscribe(topic: str): |
| 9 | + assert ( |
| 10 | + topic |
| 11 | + == "v1/to/6BAA9455E3E70682C2094CAC629F6FBED82C07CD/main/v1/command/requests" |
| 12 | + ) |
| 13 | + yield enapter.mqtt.Message( |
| 14 | + topic=topic, |
| 15 | + payload='{"id": "bbe17a10-3107-47cb-b0ec-99648debade6", "name": "my_command", "arguments": {"foo": "bar"}}', |
| 16 | + qos=0, |
| 17 | + retain=False, |
| 18 | + mid=1, |
| 19 | + properties=None, |
| 20 | + ) |
| 21 | + |
| 22 | + mock_client = mock.AsyncMock() |
| 23 | + mock_client.subscribe = subscribe |
| 24 | + channel = enapter.mqtt.api.device.Channel( |
| 25 | + client=mock_client, |
| 26 | + hardware_id="6BAA9455E3E70682C2094CAC629F6FBED82C07CD", |
| 27 | + channel_id="main", |
| 28 | + ) |
| 29 | + async with channel.subscribe_to_command_requests() as requests: |
| 30 | + request = await requests.__anext__() |
| 31 | + assert request == enapter.mqtt.api.device.CommandRequest( |
| 32 | + id="bbe17a10-3107-47cb-b0ec-99648debade6", |
| 33 | + name="my_command", |
| 34 | + arguments={"foo": "bar"}, |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +async def test_publish_command_response() -> None: |
| 39 | + mock_client = mock.AsyncMock() |
| 40 | + channel = enapter.mqtt.api.device.Channel( |
| 41 | + client=mock_client, |
| 42 | + hardware_id="6BAA9455E3E70682C2094CAC629F6FBED82C07CD", |
| 43 | + channel_id="main", |
| 44 | + ) |
| 45 | + await channel.publish_command_response( |
| 46 | + enapter.mqtt.api.device.CommandResponse( |
| 47 | + id="bbe17a10-3107-47cb-b0ec-99648debade6", |
| 48 | + state=enapter.mqtt.api.device.CommandState.COMPLETED, |
| 49 | + payload={"foo": "bar"}, |
| 50 | + ) |
| 51 | + ) |
| 52 | + mock_client.publish.assert_called_once_with( |
| 53 | + "v1/from/6BAA9455E3E70682C2094CAC629F6FBED82C07CD/main/v1/command/responses", |
| 54 | + '{"id": "bbe17a10-3107-47cb-b0ec-99648debade6", "state": "completed", "payload": {"foo": "bar"}}', |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +async def test_publish_telemetry() -> None: |
11 | 59 | mock_client = mock.AsyncMock() |
12 | 60 | channel = enapter.mqtt.api.device.Channel( |
13 | | - client=mock_client, hardware_id=hardware_id, channel_id=channel_id |
| 61 | + client=mock_client, |
| 62 | + hardware_id="6BAA9455E3E70682C2094CAC629F6FBED82C07CD", |
| 63 | + channel_id="main", |
14 | 64 | ) |
15 | 65 | await channel.publish_telemetry( |
16 | | - enapter.mqtt.api.device.Telemetry(timestamp=timestamp) |
| 66 | + enapter.mqtt.api.device.Telemetry( |
| 67 | + timestamp=1234567890, alerts=["aaa"], values={"foo": "bar"} |
| 68 | + ) |
17 | 69 | ) |
18 | 70 | mock_client.publish.assert_called_once_with( |
19 | | - f"v1/from/{hardware_id}/{channel_id}/v1/telemetry", |
20 | | - '{"timestamp": ' + str(timestamp) + ', "alerts": null}', |
| 71 | + "v1/from/6BAA9455E3E70682C2094CAC629F6FBED82C07CD/main/v1/telemetry", |
| 72 | + '{"timestamp": 1234567890, "alerts": ["aaa"], "foo": "bar"}', |
21 | 73 | ) |
22 | 74 |
|
23 | 75 |
|
24 | | -async def test_publish_properties(fake: tests.FakeDataGenerator) -> None: |
25 | | - hardware_id = fake.hardware_id() |
26 | | - channel_id = fake.channel_id() |
27 | | - timestamp = fake.timestamp() |
| 76 | +async def test_publish_properties() -> None: |
28 | 77 | mock_client = mock.AsyncMock() |
29 | 78 | channel = enapter.mqtt.api.device.Channel( |
30 | | - client=mock_client, hardware_id=hardware_id, channel_id=channel_id |
| 79 | + client=mock_client, |
| 80 | + hardware_id="6BAA9455E3E70682C2094CAC629F6FBED82C07CD", |
| 81 | + channel_id="main", |
31 | 82 | ) |
32 | 83 | await channel.publish_properties( |
33 | | - enapter.mqtt.api.device.Properties(timestamp=timestamp) |
| 84 | + enapter.mqtt.api.device.Properties(timestamp=1234567890, values={"foo": "bar"}) |
| 85 | + ) |
| 86 | + mock_client.publish.assert_called_once_with( |
| 87 | + "v1/from/6BAA9455E3E70682C2094CAC629F6FBED82C07CD/main/v1/register", |
| 88 | + '{"timestamp": 1234567890, "foo": "bar"}', |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +async def test_publish_log() -> None: |
| 93 | + mock_client = mock.AsyncMock() |
| 94 | + channel = enapter.mqtt.api.device.Channel( |
| 95 | + client=mock_client, |
| 96 | + hardware_id="6BAA9455E3E70682C2094CAC629F6FBED82C07CD", |
| 97 | + channel_id="main", |
| 98 | + ) |
| 99 | + await channel.publish_log( |
| 100 | + enapter.mqtt.api.device.Log( |
| 101 | + timestamp=1234567890, |
| 102 | + severity=enapter.mqtt.api.device.LogSeverity.INFO, |
| 103 | + message="Test log", |
| 104 | + persist=False, |
| 105 | + ) |
34 | 106 | ) |
35 | 107 | mock_client.publish.assert_called_once_with( |
36 | | - f"v1/from/{hardware_id}/{channel_id}/v1/register", |
37 | | - '{"timestamp": ' + str(timestamp) + "}", |
| 108 | + "v1/from/6BAA9455E3E70682C2094CAC629F6FBED82C07CD/main/v3/logs", |
| 109 | + '{"timestamp": 1234567890, "message": "Test log", "severity": "info", "persist": false}', |
38 | 110 | ) |
0 commit comments