|
| 1 | +import datetime |
| 2 | +import os |
| 3 | +import uuid |
1 | 4 | from unittest import mock |
2 | 5 |
|
3 | 6 | import pytest |
4 | 7 | from grpclib.testing import ChannelFor |
5 | 8 |
|
6 | 9 | from viam.errors import GRPCError |
7 | 10 | from viam.module import Module |
| 11 | +from viam.module.resource_data_consumer import ResourceDataConsumer |
8 | 12 | from viam.module.service import ModuleRPCService |
9 | 13 | from viam.proto.app.robot import ComponentConfig |
10 | 14 | from viam.proto.module import ( |
@@ -44,6 +48,33 @@ def service(module: Module) -> ModuleRPCService: |
44 | 48 | return ModuleRPCService(module) |
45 | 49 |
|
46 | 50 |
|
| 51 | +class TestResourceDataConsumer: |
| 52 | + async def test_historical_data(self): |
| 53 | + with mock.patch("viam.app.data_client.DataClient.tabular_data_by_mql", new=mock.AsyncMock()) as mocked: |
| 54 | + with mock.patch("viam.app.viam_client._get_access_token") as patched_auth: |
| 55 | + ACCESS_TOKEN = "MY_ACCESS_TOKEN" |
| 56 | + patched_auth.return_value = ACCESS_TOKEN |
| 57 | + |
| 58 | + os.environ["VIAM_API_KEY"] = "MY_API_KEY" |
| 59 | + os.environ["VIAM_API_KEY_ID"] = str(uuid.uuid4()) |
| 60 | + os.environ["VIAM_PRIMARY_ORG_ID"] = "my_org" |
| 61 | + os.environ["VIAM_MACHINE_PART_ID"] = "my_part" |
| 62 | + |
| 63 | + delta = datetime.timedelta(hours=2) |
| 64 | + |
| 65 | + # Define a helper approx matcher because the time received fields will vary slightly |
| 66 | + class DeltaApprox: |
| 67 | + def __eq__(self, other): |
| 68 | + gte = datetime.datetime.now() - delta |
| 69 | + return other - gte < datetime.timedelta(seconds=1) |
| 70 | + |
| 71 | + query = ResourceDataConsumer.construct_query("my_part", "resource", delta) |
| 72 | + query[0]["$match"]["time_received"]["$gte"] = DeltaApprox() |
| 73 | + |
| 74 | + await ResourceDataConsumer.query_tabular_data("resource", delta) |
| 75 | + mocked.assert_called_once_with("my_org", query) |
| 76 | + |
| 77 | + |
47 | 78 | class TestModule: |
48 | 79 | async def test_add_resource(self, module: Module): |
49 | 80 | req = AddResourceRequest( |
|
0 commit comments