11import  pytest 
2- from  ni .protobuf .types  import  array_pb2 , attribute_value_pb2 , scalar_pb2 
2+ from  ni .protobuf .types  import  array_pb2 , attribute_value_pb2 , scalar_pb2 ,  vector_pb2 
33from  nitypes .scalar  import  Scalar 
4+ from  nitypes .vector  import  Vector 
45from  typing_extensions  import  Mapping 
56
6- from  nipanel .converters .protobuf_types  import  Double2DArrayConverter , ScalarConverter 
7+ from  nipanel .converters .protobuf_types  import  (
8+     Double2DArrayConverter ,
9+     ScalarConverter ,
10+     VectorConverter ,
11+ )
712
813
914# ======================================================== 
@@ -84,47 +89,8 @@ def test___double2darray_empty_data___convert___returns_empty_list() -> None:
8489# ======================================================== 
8590# Scalar: Protobuf to Python 
8691# ======================================================== 
87- def  test___bool_scalar_protobuf___convert___valid_bool_scalar () ->  None :
88-     attrs  =  _units_to_scalar_attribute_map ("volts" )
89-     protobuf_value  =  scalar_pb2 .Scalar (attributes = attrs )
90-     protobuf_value .bool_value  =  True 
91- 
92-     converter  =  ScalarConverter ()
93-     python_value  =  converter .to_python_value (protobuf_value )
94- 
95-     assert  isinstance (python_value .value , bool )
96-     assert  python_value .value  is  True 
97-     assert  python_value .units  ==  "volts" 
98- 
99- 
100- def  test___int32_scalar_protobuf___convert___valid_int_scalar () ->  None :
101-     attrs  =  _units_to_scalar_attribute_map ("volts" )
102-     protobuf_value  =  scalar_pb2 .Scalar (attributes = attrs )
103-     protobuf_value .sint32_value  =  10 
104- 
105-     converter  =  ScalarConverter ()
106-     python_value  =  converter .to_python_value (protobuf_value )
107- 
108-     assert  isinstance (python_value .value , int )
109-     assert  python_value .value  ==  10 
110-     assert  python_value .units  ==  "volts" 
111- 
112- 
113- def  test___double_scalar_protobuf___convert___valid_float_scalar () ->  None :
114-     attrs  =  _units_to_scalar_attribute_map ("volts" )
115-     protobuf_value  =  scalar_pb2 .Scalar (attributes = attrs )
116-     protobuf_value .double_value  =  20.0 
117- 
118-     converter  =  ScalarConverter ()
119-     python_value  =  converter .to_python_value (protobuf_value )
120- 
121-     assert  isinstance (python_value .value , float )
122-     assert  python_value .value  ==  20.0 
123-     assert  python_value .units  ==  "volts" 
124- 
125- 
12692def  test___string_scalar_protobuf___convert___valid_str_scalar () ->  None :
127-     attrs  =  _units_to_scalar_attribute_map ("volts" )
93+     attrs  =  _units_to_attribute_map ("volts" )
12894    protobuf_value  =  scalar_pb2 .Scalar (attributes = attrs )
12995    protobuf_value .string_value  =  "value" 
13096
@@ -136,65 +102,9 @@ def test___string_scalar_protobuf___convert___valid_str_scalar() -> None:
136102    assert  python_value .units  ==  "volts" 
137103
138104
139- def  test___scalar_protobuf_value_unset___convert___throws_type_error () ->  None :
140-     attrs  =  _units_to_scalar_attribute_map ("volts" )
141-     protobuf_value  =  scalar_pb2 .Scalar (attributes = attrs )
142- 
143-     converter  =  ScalarConverter ()
144-     with  pytest .raises (ValueError ) as  exc :
145-         _  =  converter .to_python_value (protobuf_value )
146- 
147-     assert  exc .value .args [0 ].startswith ("Could not determine the data type of 'value'." )
148- 
149- 
150- def  test___scalar_protobuf_units_unset___convert___python_units_blank () ->  None :
151-     protobuf_value  =  scalar_pb2 .Scalar ()
152-     protobuf_value .bool_value  =  True 
153- 
154-     converter  =  ScalarConverter ()
155-     python_value  =  converter .to_python_value (protobuf_value )
156- 
157-     assert  isinstance (python_value .value , bool )
158-     assert  python_value .value  is  True 
159-     assert  python_value .units  ==  "" 
160- 
161- 
162105# ======================================================== 
163106# Scalar: Python to Protobuf 
164107# ======================================================== 
165- def  test___bool_scalar___convert___valid_bool_scalar_protobuf () ->  None :
166-     python_value  =  Scalar (True , "volts" )
167- 
168-     converter  =  ScalarConverter ()
169-     protobuf_value  =  converter .to_protobuf_message (python_value )
170- 
171-     assert  protobuf_value .WhichOneof ("value" ) ==  "bool_value" 
172-     assert  protobuf_value .bool_value  is  True 
173-     assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "volts" 
174- 
175- 
176- def  test___int_scalar___convert___valid_int32_scalar_protobuf () ->  None :
177-     python_value  =  Scalar (10 , "volts" )
178- 
179-     converter  =  ScalarConverter ()
180-     protobuf_value  =  converter .to_protobuf_message (python_value )
181- 
182-     assert  protobuf_value .WhichOneof ("value" ) ==  "sint32_value" 
183-     assert  protobuf_value .sint32_value  ==  10 
184-     assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "volts" 
185- 
186- 
187- def  test___float_scalar___convert___valid_double_scalar_protobuf () ->  None :
188-     python_value  =  Scalar (20.0 , "volts" )
189- 
190-     converter  =  ScalarConverter ()
191-     protobuf_value  =  converter .to_protobuf_message (python_value )
192- 
193-     assert  protobuf_value .WhichOneof ("value" ) ==  "double_value" 
194-     assert  protobuf_value .double_value  ==  20.0 
195-     assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "volts" 
196- 
197- 
198108def  test___str_scalar___convert___valid_string_scalar_protobuf () ->  None :
199109    python_value  =  Scalar ("value" , "volts" )
200110
@@ -206,17 +116,39 @@ def test___str_scalar___convert___valid_string_scalar_protobuf() -> None:
206116    assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "volts" 
207117
208118
209- def  test___scalar_units_unset___convert___protobuf_units_blank () ->  None :
210-     python_value  =  Scalar (10 )
119+ # ======================================================== 
120+ # Vector: Protobuf to Python 
121+ # ======================================================== 
122+ def  test___string_vector_protobuf___convert___valid_str_vector () ->  None :
123+     attrs  =  _units_to_attribute_map ("volts" )
124+     protobuf_value  =  vector_pb2 .Vector (
125+         attributes = attrs ,
126+         string_array = array_pb2 .StringArray (values = ["one" , "two" , "three" ]),
127+     )
128+ 
129+     converter  =  VectorConverter ()
130+     python_value  =  converter .to_python_value (protobuf_value )
211131
212-     converter  =  ScalarConverter ()
132+     assert  isinstance (python_value , Vector )
133+     assert  list (python_value ) ==  ["one" , "two" , "three" ]
134+     assert  python_value .units  ==  "volts" 
135+ 
136+ 
137+ # ======================================================== 
138+ # Vector: Python to Protobuf 
139+ # ======================================================== 
140+ def  test___str_vector___convert___valid_string_vector_protobuf () ->  None :
141+     python_value  =  Vector (["one" , "two" , "three" ], "volts" )
142+ 
143+     converter  =  VectorConverter ()
213144    protobuf_value  =  converter .to_protobuf_message (python_value )
214145
215-     assert  protobuf_value .WhichOneof ("value" ) ==  "sint32_value" 
216-     assert  protobuf_value .sint32_value  ==  10 
217-     assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "" 
146+     assert  isinstance (protobuf_value , vector_pb2 .Vector )
147+     assert  protobuf_value .WhichOneof ("value" ) ==  "string_array" 
148+     assert  list (protobuf_value .string_array .values ) ==  ["one" , "two" , "three" ]
149+     assert  protobuf_value .attributes ["NI_UnitDescription" ].string_value  ==  "volts" 
218150
219151
220- def  _units_to_scalar_attribute_map (units : str ) ->  Mapping [str , attribute_value_pb2 .AttributeValue ]:
152+ def  _units_to_attribute_map (units : str ) ->  Mapping [str , attribute_value_pb2 .AttributeValue ]:
221153    value  =  attribute_value_pb2 .AttributeValue (string_value = units )
222154    return  {"NI_UnitDescription" : value }
0 commit comments