|
4 | 4 |
|
5 | 5 | import datetime as std_datetime |
6 | 6 | import unittest.mock |
7 | | -from typing import cast |
| 7 | +from typing import Any, Iterable, cast |
8 | 8 | from unittest.mock import NonCallableMock |
9 | 9 |
|
10 | 10 | import numpy as np |
|
25 | 25 | from ni.protobuf.types.precision_timestamp_conversion import ( |
26 | 26 | hightime_datetime_to_protobuf, |
27 | 27 | ) |
| 28 | +from ni.protobuf.types.vector_conversion import vector_to_protobuf |
| 29 | +from ni.protobuf.types.vector_pb2 import Vector as VectorProto |
28 | 30 | from ni.protobuf.types.waveform_conversion import float64_analog_waveform_to_protobuf |
29 | 31 | from ni.protobuf.types.waveform_pb2 import DoubleAnalogWaveform |
30 | 32 | from nitypes.vector import Vector |
@@ -116,6 +118,45 @@ def test___publish_analog_waveform_data___calls_data_store_service_client( |
116 | 118 | assert request.test_adapter_ids == [] |
117 | 119 |
|
118 | 120 |
|
| 121 | +@pytest.mark.parametrize( |
| 122 | + "value", [[1, 2, 3], [1.0, 2.0, 3.0], [True, False, True], ["one", "two", "three"]] |
| 123 | +) |
| 124 | +def test___publish_basic_iterable_data___calls_data_store_service_client( |
| 125 | + data_store_client: DataStoreClient, |
| 126 | + mocked_data_store_service_client: NonCallableMock, |
| 127 | + value: Iterable[Any], |
| 128 | +) -> None: |
| 129 | + expected_vector = Vector(value) |
| 130 | + expected_protobuf_vector = VectorProto() |
| 131 | + expected_protobuf_vector.CopyFrom(vector_to_protobuf(expected_vector)) |
| 132 | + published_measurement = PublishedMeasurement(published_measurement_id="response_id") |
| 133 | + expected_response = PublishMeasurementResponse(published_measurement=published_measurement) |
| 134 | + mocked_data_store_service_client.publish_measurement.return_value = expected_response |
| 135 | + |
| 136 | + # Now, when client.publish_measurement calls foo.MyClass().publish(), it will use the mock |
| 137 | + result = data_store_client.publish_measurement("name", value, "step_id") |
| 138 | + |
| 139 | + args, __ = mocked_data_store_service_client.publish_measurement.call_args |
| 140 | + request = cast(PublishMeasurementRequest, args[0]) # The PublishMeasurementRequest object |
| 141 | + assert result.published_measurement_id == "response_id" |
| 142 | + assert request.step_id == "step_id" |
| 143 | + assert request.measurement_name == "name" |
| 144 | + assert request.vector == expected_protobuf_vector |
| 145 | + |
| 146 | + |
| 147 | +def test___unsupported_list___publish_measurement___raises_type_error( |
| 148 | + data_store_client: DataStoreClient, |
| 149 | +) -> None: |
| 150 | + with pytest.raises(TypeError) as exc: |
| 151 | + _ = data_store_client.publish_measurement( |
| 152 | + measurement_name="name", |
| 153 | + value=[[1, 2, 3], [4, 5, 6]], # List of lists will error during vector creation. |
| 154 | + step_id="step_id", |
| 155 | + ) |
| 156 | + |
| 157 | + assert exc.value.args[0].startswith("Unsupported iterable:") |
| 158 | + |
| 159 | + |
119 | 160 | def test___publish_analog_waveform_data_without_timestamp_parameter___uses_waveform_t0( |
120 | 161 | data_store_client: DataStoreClient, |
121 | 162 | mocked_data_store_service_client: NonCallableMock, |
|
0 commit comments