Skip to content

Commit 3cbb00b

Browse files
author
Michael Johansen
committed
Add new protobuf converters to _convert.py.
Signed-off-by: Michael Johansen <[email protected]>
1 parent 58bd0c3 commit 3cbb00b

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/nipanel/_convert.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
IntCollectionConverter,
2222
StrCollectionConverter,
2323
)
24+
from nipanel.converters.protobuf_types import DoubleAnalogWaveformConverter, ScalarConverter
2425

2526
_logger = logging.getLogger(__name__)
2627

@@ -38,6 +39,9 @@
3839
FloatCollectionConverter(),
3940
IntCollectionConverter(),
4041
StrCollectionConverter(),
42+
# Protobuf Types
43+
DoubleAnalogWaveformConverter(),
44+
ScalarConverter(),
4145
]
4246

4347
_CONVERTIBLE_COLLECTION_TYPES = {

tests/unit/test_convert.py

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
from typing import Any, Type, Union
1+
from typing import Any, Union
22

3+
import numpy as np
34
import pytest
45
from google.protobuf import any_pb2, wrappers_pb2
6+
from google.protobuf.message import Message
7+
from ni.protobuf.types.scalar_pb2 import ScalarData
58
from ni.pythonpanel.v1 import python_panel_types_pb2
9+
from ni_measurement_plugin_sdk_service._internal.stubs.ni.protobuf.types.waveform_pb2 import (
10+
DoubleAnalogWaveform,
11+
)
12+
from nitypes.scalar import Scalar
13+
from nitypes.waveform import AnalogWaveform
614
from typing_extensions import TypeAlias
715

816
import nipanel._convert
@@ -26,7 +34,7 @@
2634

2735

2836
# ========================================================
29-
# Python to Protobuf
37+
# Built-in Types: Python to Protobuf
3038
# ========================================================
3139
@pytest.mark.parametrize(
3240
"proto_type, default_value, expected_value",
@@ -39,7 +47,7 @@
3947
],
4048
)
4149
def test___python_builtin_scalar___to_any___valid_wrapperpb2_value(
42-
proto_type: Type[_AnyWrappersPb2], default_value: Any, expected_value: Any
50+
proto_type: type[_AnyWrappersPb2], default_value: Any, expected_value: Any
4351
) -> None:
4452
result = nipanel._convert.to_any(expected_value)
4553
unpack_dest = proto_type(value=default_value)
@@ -60,7 +68,7 @@ def test___python_builtin_scalar___to_any___valid_wrapperpb2_value(
6068
],
6169
)
6270
def test___python_panel_collection___to_any___valid_paneltype_value(
63-
proto_type: Type[_AnyPanelPbTypes], default_value: Any, expected_value: Any
71+
proto_type: type[_AnyPanelPbTypes], default_value: Any, expected_value: Any
6472
) -> None:
6573
result = nipanel._convert.to_any(expected_value)
6674
unpack_dest = proto_type(values=default_value)
@@ -71,7 +79,7 @@ def test___python_panel_collection___to_any___valid_paneltype_value(
7179

7280

7381
# ========================================================
74-
# Protobuf to Python
82+
# Built-in Types: Protobuf to Python
7583
# ========================================================
7684
@pytest.mark.parametrize(
7785
"proto_type, expected_value",
@@ -84,7 +92,7 @@ def test___python_panel_collection___to_any___valid_paneltype_value(
8492
],
8593
)
8694
def test___wrapperpb2_value___from_any___valid_python_value(
87-
proto_type: Type[_AnyWrappersPb2], expected_value: Any
95+
proto_type: type[_AnyWrappersPb2], expected_value: Any
8896
) -> None:
8997
pb_value = proto_type(value=expected_value)
9098
packed_any = _pack_into_any(pb_value)
@@ -106,7 +114,7 @@ def test___wrapperpb2_value___from_any___valid_python_value(
106114
],
107115
)
108116
def test___paneltype_value___from_any___valid_python_value(
109-
proto_type: Type[_AnyPanelPbTypes], expected_value: Any
117+
proto_type: type[_AnyPanelPbTypes], expected_value: Any
110118
) -> None:
111119
pb_value = proto_type(values=expected_value)
112120
packed_any = _pack_into_any(pb_value)
@@ -117,15 +125,64 @@ def test___paneltype_value___from_any___valid_python_value(
117125
assert result == expected_value
118126

119127

128+
# ========================================================
129+
# Protobuf Types: Python to Protobuf
130+
# ========================================================
131+
def test___python_scalar_object___to_any___valid_scalar_data_value() -> None:
132+
scalar_obj = Scalar(1.0, "amps")
133+
result = nipanel._convert.to_any(scalar_obj)
134+
unpack_dest = ScalarData()
135+
_assert_any_and_unpack(result, unpack_dest)
136+
137+
assert isinstance(unpack_dest, ScalarData)
138+
assert unpack_dest.double_value == 1.0
139+
assert unpack_dest.units == "amps"
140+
141+
142+
def test___python_analog_waveform___to_any___valid_double_analog_waveform() -> None:
143+
wfm_obj = AnalogWaveform(3)
144+
result = nipanel._convert.to_any(wfm_obj)
145+
unpack_dest = DoubleAnalogWaveform()
146+
_assert_any_and_unpack(result, unpack_dest)
147+
148+
assert isinstance(unpack_dest, DoubleAnalogWaveform)
149+
assert list(unpack_dest.y_data) == [0.0, 0.0, 0.0]
150+
151+
152+
# ========================================================
153+
# Protobuf Types: Protobuf to Python
154+
# ========================================================
155+
def test___scalar_data___from_any___valid_python_scalar_object() -> None:
156+
pb_value = ScalarData(units="amps", double_value=1.0)
157+
packed_any = _pack_into_any(pb_value)
158+
159+
result = nipanel._convert.from_any(packed_any)
160+
161+
assert isinstance(result, Scalar)
162+
assert result.value == 1.0
163+
assert result.units == "amps"
164+
165+
166+
def test___double_analog_waveform___from_any___valid_python_analog_waveform() -> None:
167+
pb_value = DoubleAnalogWaveform(y_data=[0.0, 0.0, 0.0])
168+
packed_any = _pack_into_any(pb_value)
169+
170+
result = nipanel._convert.from_any(packed_any)
171+
172+
assert isinstance(result, AnalogWaveform)
173+
assert result.sample_count == result.capacity == len(result.raw_data) == 3
174+
assert result.dtype == np.float64
175+
176+
120177
# ========================================================
121178
# Pack/Unpack Helpers
122179
# ========================================================
123-
def _assert_any_and_unpack(packed_message: any_pb2.Any, unpack_destination: Any) -> None:
180+
def _assert_any_and_unpack(packed_message: any_pb2.Any, unpack_destination: Message) -> None:
124181
assert isinstance(packed_message, any_pb2.Any)
125182
assert packed_message.Unpack(unpack_destination)
126183

127184

128-
def _pack_into_any(proto_value: Union[_AnyWrappersPb2, _AnyPanelPbTypes]) -> any_pb2.Any:
185+
def _pack_into_any(proto_value: Message) -> any_pb2.Any:
129186
as_any = any_pb2.Any()
130187
as_any.Pack(proto_value)
131188
return as_any

0 commit comments

Comments
 (0)