1+ import unittest
2+ from PyAres .Utils import ares_data_schema_utils
3+ from PyAres .Models import AresDataType , AresSchemaEntry
4+ from ares_datamodel import ares_data_type_pb2
5+
6+ class TestAresDataSchemaUtils (unittest .TestCase ):
7+ def test_create_simple_entry (self ):
8+ entry = ares_data_schema_utils .create_settings_schema_entry (
9+ AresDataType .STRING ,
10+ optional = True ,
11+ choices = []
12+ )
13+ self .assertEqual (entry .type , ares_data_type_pb2 .AresDataType .STRING )
14+ self .assertTrue (entry .optional )
15+
16+ def test_create_entry_with_string_choices (self ):
17+ choices = ["A" , "B" ]
18+ entry = ares_data_schema_utils .create_settings_schema_entry (
19+ AresDataType .STRING ,
20+ optional = False ,
21+ choices = choices
22+ )
23+ self .assertEqual (entry .string_choices .strings , choices )
24+
25+ def test_create_entry_with_number_choices (self ):
26+ choices = [1 , 2.0 ]
27+ entry = ares_data_schema_utils .create_settings_schema_entry (
28+ AresDataType .NUMBER ,
29+ optional = False ,
30+ choices = choices
31+ )
32+ self .assertEqual (entry .number_choices .numbers , choices )
33+
34+ def test_create_entry_with_mixed_choices (self ):
35+ # Should result in no choices being set if types are mixed
36+ choices = ["A" , 1 ]
37+ entry = ares_data_schema_utils .create_settings_schema_entry (
38+ AresDataType .STRING ,
39+ optional = False ,
40+ choices = choices
41+ )
42+
43+ self .assertEqual (len (entry .string_choices .strings ), 0 )
44+ self .assertEqual (len (entry .number_choices .numbers ), 0 )
45+
46+ def test_convert_ares_schema_entry_to_proto (self ):
47+ py_entry = AresSchemaEntry (
48+ type = AresDataType .NUMBER ,
49+ optional = True ,
50+ description = "Test Desc" ,
51+ unit = "m/s" ,
52+ choices = [1.0 , 2.0 ]
53+ )
54+ proto = ares_data_schema_utils .convert_ares_schema_entry_to_proto (py_entry )
55+ self .assertEqual (proto .type , ares_data_type_pb2 .AresDataType .NUMBER )
56+ self .assertTrue (proto .optional )
57+ self .assertEqual (proto .description , "Test Desc" )
58+ self .assertEqual (proto .unit , "m/s" )
59+ self .assertEqual (proto .number_choices .numbers , [1.0 , 2.0 ])
60+
61+ def test_nested_struct_schema (self ):
62+ nested_field = AresSchemaEntry (type = AresDataType .STRING , description = "Inner" )
63+ struct_entry = AresSchemaEntry (
64+ type = AresDataType .STRUCT ,
65+ struct_schema = {"inner_key" : nested_field }
66+ )
67+
68+ proto = ares_data_schema_utils .convert_ares_schema_entry_to_proto (struct_entry )
69+ self .assertEqual (proto .type , ares_data_type_pb2 .AresDataType .STRUCT )
70+ self .assertIn ("inner_key" , proto .struct_schema .fields )
71+
72+ inner_proto = proto .struct_schema .fields ["inner_key" ]
73+ self .assertEqual (inner_proto .type , ares_data_type_pb2 .AresDataType .STRING )
74+ self .assertEqual (inner_proto .description , "Inner" )
75+
76+ if __name__ == '__main__' :
77+ unittest .main (verbosity = 2 )
0 commit comments