1- from typing import Any , Union
1+ from typing import Any , Collection , Union
22
33import numpy as np
44import pytest
55from google .protobuf import any_pb2 , wrappers_pb2
66from google .protobuf .message import Message
77from ni .protobuf .types .scalar_pb2 import ScalarData
88from ni .pythonpanel .v1 import python_panel_types_pb2
9+ from ni_measurement_plugin_sdk_service ._internal .stubs .ni .protobuf .types .array_pb2 import (
10+ Double2DArray ,
11+ )
912from ni_measurement_plugin_sdk_service ._internal .stubs .ni .protobuf .types .waveform_pb2 import (
1013 DoubleAnalogWaveform ,
1114)
1417from typing_extensions import TypeAlias
1518
1619import nipanel ._convert
20+ import tests .types
1721
1822
1923_AnyWrappersPb2 : TypeAlias = Union [
4448 (456.2 , "float" ),
4549 (123 , "int" ),
4650 ("mystr" , "str" ),
51+ (tests .types .MyIntFlags .VALUE1 , "int" ),
52+ (tests .types .MyIntEnum .VALUE10 , "int" ),
53+ (tests .types .MixinIntEnum .VALUE11 , "int" ),
54+ (tests .types .MyStrEnum .VALUE1 , "str" ),
55+ (tests .types .MixinStrEnum .VALUE11 , "str" ),
4756 ([False , False ], "Collection.bool" ),
4857 ([b"mystr" , b"mystr" ], "Collection.bytes" ),
4958 ([456.2 , 1.0 ], "Collection.float" ),
6978 (frozenset ([456.2 , 1.0 ]), "Collection.float" ),
7079 (frozenset ([123 , 456 ]), "Collection.int" ),
7180 (frozenset (["mystr" , "mystr2" ]), "Collection.str" ),
81+ ([[1.0 , 2.0 ], [1.0 , 2.0 ]], "Collection.Collection.float" ),
82+ ([(1.0 , 2.0 ), (3.0 , 4.0 )], "Collection.Collection.float" ),
83+ ([set ([1.0 , 2.0 ]), set ([3.0 , 4.0 ])], "Collection.Collection.float" ),
84+ ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])], "Collection.Collection.float" ),
85+ (([1.0 , 2.0 ], [3.0 , 4.0 ]), "Collection.Collection.float" ),
86+ (((1.0 , 2.0 ), (3.0 , 4.0 )), "Collection.Collection.float" ),
87+ ((set ([1.0 , 2.0 ]), set ([3.0 , 4.0 ])), "Collection.Collection.float" ),
88+ ((frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])), "Collection.Collection.float" ),
89+ (set ([(1.0 , 2.0 ), (3.0 , 4.0 )]), "Collection.Collection.float" ),
90+ (set ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])]), "Collection.Collection.float" ),
91+ (frozenset ([(1.0 , 2.0 ), (3.0 , 4.0 )]), "Collection.Collection.float" ),
92+ (frozenset ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])]), "Collection.Collection.float" ),
7293 ],
7394)
7495def test___various_python_objects___get_best_matching_type___returns_correct_type_string (
@@ -194,6 +215,63 @@ def test___python_analog_waveform___to_any___valid_double_analog_waveform() -> N
194215 assert list (unpack_dest .y_data ) == [0.0 , 0.0 , 0.0 ]
195216
196217
218+ @pytest .mark .parametrize (
219+ "python_value" ,
220+ [
221+ # lists of collections
222+ ([[1.0 , 2.0 ], [3.0 , 4.0 ]]),
223+ ([(1.0 , 2.0 ), (3.0 , 4.0 )]),
224+ ([set ([1.0 , 2.0 ]), set ([3.0 , 4.0 ])]),
225+ ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])]),
226+ # tuples of collections
227+ (([1.0 , 2.0 ], [3.0 , 4.0 ])),
228+ (((1.0 , 2.0 ), (3.0 , 4.0 ))),
229+ ((set ([1.0 , 2.0 ]), set ([3.0 , 4.0 ]))),
230+ ((frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ]))),
231+ ],
232+ )
233+ def test___python_2dcollection_of_float___to_any___valid_double2darray (
234+ python_value : Collection [Collection [float ]],
235+ ) -> None :
236+ expected_data = [1.0 , 2.0 , 3.0 , 4.0 ]
237+ expected_rows = 2
238+ expected_columns = 2
239+ result = nipanel ._convert .to_any (python_value )
240+ unpack_dest = Double2DArray ()
241+ _assert_any_and_unpack (result , unpack_dest )
242+
243+ assert isinstance (unpack_dest , Double2DArray )
244+ assert unpack_dest .rows == expected_rows
245+ assert unpack_dest .columns == expected_columns
246+ assert unpack_dest .data == expected_data
247+
248+
249+ @pytest .mark .parametrize (
250+ "python_value" ,
251+ [
252+ (set ([(1.0 , 2.0 ), (3.0 , 4.0 )])),
253+ (set ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])])),
254+ (frozenset ([(1.0 , 2.0 ), (3.0 , 4.0 )])),
255+ (frozenset ([frozenset ([1.0 , 2.0 ]), frozenset ([3.0 , 4.0 ])])),
256+ ],
257+ )
258+ def test___python_set_of_collection_of_float___to_any___valid_double2darray (
259+ python_value : Collection [Collection [float ]],
260+ ) -> None :
261+ expected_data = [1.0 , 2.0 , 3.0 , 4.0 ]
262+ expected_rows = 2
263+ expected_columns = 2
264+ result = nipanel ._convert .to_any (python_value )
265+ unpack_dest = Double2DArray ()
266+ _assert_any_and_unpack (result , unpack_dest )
267+
268+ assert isinstance (unpack_dest , Double2DArray )
269+ assert unpack_dest .rows == expected_rows
270+ assert unpack_dest .columns == expected_columns
271+ # Sets and frozensets don't maintain order, so sort before comparing.
272+ assert sorted (unpack_dest .data ) == sorted (expected_data )
273+
274+
197275# ========================================================
198276# Protobuf Types: Protobuf to Python
199277# ========================================================
@@ -219,6 +297,17 @@ def test___double_analog_waveform___from_any___valid_python_analog_waveform() ->
219297 assert result .dtype == np .float64
220298
221299
300+ def test___double2darray___from_any___valid_python_2dcollection () -> None :
301+ pb_value = Double2DArray (data = [1.0 , 2.0 , 3.0 , 4.0 ], rows = 2 , columns = 2 )
302+ packed_any = _pack_into_any (pb_value )
303+
304+ result = nipanel ._convert .from_any (packed_any )
305+
306+ expected_value = [[1.0 , 2.0 ], [3.0 , 4.0 ]]
307+ assert isinstance (result , type (expected_value ))
308+ assert result == expected_value
309+
310+
222311# ========================================================
223312# Pack/Unpack Helpers
224313# ========================================================
0 commit comments