Skip to content

Commit 29c6fd8

Browse files
cecilleandy31415
andauthored
python testing: add test functions for IDs (#36766)
* python testing: add test functions for IDs - Add functions to say whether an ID is in the standard range - simpify API to accept an int ID rather than the enum since this is how they appear on the device. - fix tests TEST: see unit test * Update src/python_testing/matter_testing_infrastructure/chip/testing/global_attribute_ids.py Co-authored-by: Andrei Litvin <andy314@gmail.com> --------- Co-authored-by: Andrei Litvin <andy314@gmail.com>
1 parent 918320e commit 29c6fd8

File tree

3 files changed

+87
-43
lines changed

3 files changed

+87
-43
lines changed

src/python_testing/TC_DeviceConformance.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def record_warning(location, problem):
295295
continue
296296

297297
device_type_list = endpoint[Clusters.Descriptor][Clusters.Descriptor.Attributes.DeviceTypeList]
298-
invalid_device_types = [x for x in device_type_list if not is_valid_device_type_id(device_type_id_type(x.deviceType))]
298+
invalid_device_types = [x for x in device_type_list if not is_valid_device_type_id(x.deviceType)]
299299
standard_device_types = [x for x in endpoint[Clusters.Descriptor]
300300
[Clusters.Descriptor.Attributes.DeviceTypeList] if device_type_id_type(x.deviceType) == DeviceTypeIdType.kStandard]
301301
endpoint_clusters = []

src/python_testing/TestIdChecks.py

+58-38
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#
1717

1818
from chip.testing.global_attribute_ids import (AttributeIdType, ClusterIdType, CommandIdType, DeviceTypeIdType, attribute_id_type,
19-
cluster_id_type, command_id_type, device_type_id_type, is_valid_attribute_id,
20-
is_valid_cluster_id, is_valid_command_id, is_valid_device_type_id)
19+
cluster_id_type, command_id_type, device_type_id_type, is_standard_attribute_id,
20+
is_standard_cluster_id, is_standard_command_id, is_standard_device_type_id,
21+
is_valid_attribute_id, is_valid_cluster_id, is_valid_command_id,
22+
is_valid_device_type_id)
2123
from chip.testing.matter_testing import MatterBaseTest, default_matter_test_main
2224
from mobly import asserts
2325

@@ -39,29 +41,33 @@ def check_standard(id):
3941
id_type = device_type_id_type(id)
4042
msg = f"Incorrect device type range assessment, expecting standard {id:08x}, type = {id_type}"
4143
asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kStandard, msg)
42-
asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg)
43-
asserts.assert_true(is_valid_device_type_id(id_type, allow_test=False), msg)
44+
asserts.assert_true(is_valid_device_type_id(id, allow_test=True), msg)
45+
asserts.assert_true(is_valid_device_type_id(id, allow_test=False), msg)
46+
asserts.assert_true(is_standard_device_type_id(id), msg)
4447

4548
def check_manufacturer(id):
4649
id_type = device_type_id_type(id)
4750
msg = f"Incorrect device type range assessment, expecting manufacturer {id:08x}, type = {id_type}"
4851
asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kManufacturer, msg)
49-
asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg)
50-
asserts.assert_true(is_valid_device_type_id(id_type, allow_test=False), msg)
52+
asserts.assert_true(is_valid_device_type_id(id, allow_test=True), msg)
53+
asserts.assert_true(is_valid_device_type_id(id, allow_test=False), msg)
54+
asserts.assert_false(is_standard_device_type_id(id), msg)
5155

5256
def check_test(id):
5357
id_type = device_type_id_type(id)
5458
msg = f"Incorrect device type range assessment, expecting test {id:08x}, type = {id_type}"
5559
asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kTest, msg)
56-
asserts.assert_true(is_valid_device_type_id(id_type, allow_test=True), msg)
57-
asserts.assert_false(is_valid_device_type_id(id_type, allow_test=False), msg)
60+
asserts.assert_true(is_valid_device_type_id(id, allow_test=True), msg)
61+
asserts.assert_false(is_valid_device_type_id(id, allow_test=False), msg)
62+
asserts.assert_false(is_standard_device_type_id(id), msg)
5863

5964
def check_all_bad(id):
6065
id_type = device_type_id_type(id)
6166
msg = f"Incorrect device type range assessment, expecting invalid {id:08x}, type = {id_type}"
6267
asserts.assert_equal(device_type_id_type(id), DeviceTypeIdType.kInvalid, msg)
63-
asserts.assert_false(is_valid_device_type_id(id_type, allow_test=True), msg)
64-
asserts.assert_false(is_valid_device_type_id(id_type, allow_test=False), msg)
68+
asserts.assert_false(is_valid_device_type_id(id, allow_test=True), msg)
69+
asserts.assert_false(is_valid_device_type_id(id, allow_test=False), msg)
70+
asserts.assert_false(is_standard_device_type_id(id), msg)
6571

6672
for id in standard_good:
6773
check_standard(id)
@@ -100,29 +106,33 @@ def check_standard(id):
100106
id_type = cluster_id_type(id)
101107
msg = f"Incorrect cluster range assessment, expecting standard {id:08x}, type = {id_type}"
102108
asserts.assert_equal(id_type, ClusterIdType.kStandard, msg)
103-
asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg)
104-
asserts.assert_true(is_valid_cluster_id(id_type, allow_test=False), msg)
109+
asserts.assert_true(is_valid_cluster_id(id, allow_test=True), msg)
110+
asserts.assert_true(is_valid_cluster_id(id, allow_test=False), msg)
111+
asserts.assert_true(is_standard_cluster_id(id), msg)
105112

106113
def check_manufacturer(id):
107114
id_type = cluster_id_type(id)
108115
msg = f"Incorrect cluster range assessment, expecting manufacturer {id:08x}, type = {id_type}"
109116
asserts.assert_equal(id_type, ClusterIdType.kManufacturer, msg)
110-
asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg)
111-
asserts.assert_true(is_valid_cluster_id(id_type, allow_test=False), msg)
117+
asserts.assert_true(is_valid_cluster_id(id, allow_test=True), msg)
118+
asserts.assert_true(is_valid_cluster_id(id, allow_test=False), msg)
119+
asserts.assert_false(is_standard_cluster_id(id), msg)
112120

113121
def check_test(id):
114122
id_type = cluster_id_type(id)
115123
msg = f"Incorrect cluster range assessment, expecting test {id:08x}, type = {id_type}"
116124
asserts.assert_equal(id_type, ClusterIdType.kTest, msg)
117-
asserts.assert_true(is_valid_cluster_id(id_type, allow_test=True), msg)
118-
asserts.assert_false(is_valid_cluster_id(id_type, allow_test=False), msg)
125+
asserts.assert_true(is_valid_cluster_id(id, allow_test=True), msg)
126+
asserts.assert_false(is_valid_cluster_id(id, allow_test=False), msg)
127+
asserts.assert_false(is_standard_cluster_id(id), msg)
119128

120129
def check_all_bad(id):
121130
id_type = cluster_id_type(id)
122131
msg = f"Incorrect cluster range assessment, expecting invalid {id:08x}, type = {id_type}"
123132
asserts.assert_equal(id_type, ClusterIdType.kInvalid, msg)
124-
asserts.assert_false(is_valid_cluster_id(id_type, allow_test=True), msg)
125-
asserts.assert_false(is_valid_cluster_id(id_type, allow_test=False), msg)
133+
asserts.assert_false(is_valid_cluster_id(id, allow_test=True), msg)
134+
asserts.assert_false(is_valid_cluster_id(id, allow_test=False), msg)
135+
asserts.assert_false(is_standard_cluster_id(id), msg)
126136

127137
for id in standard_good:
128138
check_standard(id)
@@ -160,36 +170,41 @@ def check_standard_global(id):
160170
id_type = attribute_id_type(id)
161171
msg = f"Incorrect attribute range assessment, expecting standard global {id:08x}, type = {id_type}"
162172
asserts.assert_equal(id_type, AttributeIdType.kStandardGlobal, msg)
163-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg)
164-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg)
173+
asserts.assert_true(is_valid_attribute_id(id, allow_test=True), msg)
174+
asserts.assert_true(is_valid_attribute_id(id, allow_test=False), msg)
175+
asserts.assert_true(is_standard_attribute_id(id), msg)
165176

166177
def check_standard_non_global(id):
167178
id_type = attribute_id_type(id)
168179
msg = f"Incorrect attribute range assessment, expecting standard non-global {id:08x}, type = {id_type}"
169180
asserts.assert_equal(id_type, AttributeIdType.kStandardNonGlobal, msg)
170-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg)
171-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg)
181+
asserts.assert_true(is_valid_attribute_id(id, allow_test=True), msg)
182+
asserts.assert_true(is_valid_attribute_id(id, allow_test=False), msg)
183+
asserts.assert_true(is_standard_attribute_id(id), msg)
172184

173185
def check_manufacturer(id):
174186
id_type = attribute_id_type(id)
175187
msg = f"Incorrect attribute range assessment, expecting manufacturer {id:08x}, type = {id_type}"
176188
asserts.assert_equal(id_type, AttributeIdType.kManufacturer, msg)
177-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg)
178-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=False), msg)
189+
asserts.assert_true(is_valid_attribute_id(id, allow_test=True), msg)
190+
asserts.assert_true(is_valid_attribute_id(id, allow_test=False), msg)
191+
asserts.assert_false(is_standard_attribute_id(id), msg)
179192

180193
def check_test(id):
181194
id_type = attribute_id_type(id)
182195
msg = f"Incorrect attribute range assessment, expecting test {id:08x}, type = {id_type}"
183196
asserts.assert_equal(id_type, AttributeIdType.kTest, msg)
184-
asserts.assert_true(is_valid_attribute_id(id_type, allow_test=True), msg)
185-
asserts.assert_false(is_valid_attribute_id(id_type, allow_test=False), msg)
197+
asserts.assert_true(is_valid_attribute_id(id, allow_test=True), msg)
198+
asserts.assert_false(is_valid_attribute_id(id, allow_test=False), msg)
199+
asserts.assert_false(is_standard_attribute_id(id), msg)
186200

187201
def check_all_bad(id):
188202
id_type = attribute_id_type(id)
189203
msg = f"Incorrect attribute range assessment, expecting invalid {id:08x}, type = {id_type}"
190204
asserts.assert_equal(id_type, AttributeIdType.kInvalid, msg)
191-
asserts.assert_false(is_valid_attribute_id(id_type, allow_test=True), msg)
192-
asserts.assert_false(is_valid_attribute_id(id_type, allow_test=False), msg)
205+
asserts.assert_false(is_valid_attribute_id(id, allow_test=True), msg)
206+
asserts.assert_false(is_valid_attribute_id(id, allow_test=False), msg)
207+
asserts.assert_false(is_standard_attribute_id(id), msg)
193208

194209
for id in standard_global_good:
195210
check_standard_global(id)
@@ -225,36 +240,41 @@ def check_standard_global(id):
225240
id_type = command_id_type(id)
226241
msg = f"Incorrect command range assessment, expecting standard global {id:08x}, type = {id_type}"
227242
asserts.assert_equal(id_type, CommandIdType.kStandardGlobal, msg)
228-
asserts.assert_true(is_valid_command_id(id_type, allow_test=True), msg)
229-
asserts.assert_true(is_valid_command_id(id_type, allow_test=False), msg)
243+
asserts.assert_true(is_valid_command_id(id, allow_test=True), msg)
244+
asserts.assert_true(is_valid_command_id(id, allow_test=False), msg)
245+
asserts.assert_true(is_standard_command_id(id), msg)
230246

231247
def check_scoped_non_global(id):
232248
id_type = command_id_type(id)
233249
msg = f"Incorrect command range assessment, expecting scoped non-global {id:08x}, type = {id_type}"
234250
asserts.assert_equal(id_type, CommandIdType.kScopedNonGlobal, msg)
235-
asserts.assert_true(is_valid_command_id(id_type, allow_test=True), msg)
236-
asserts.assert_true(is_valid_command_id(id_type, allow_test=False), msg)
251+
asserts.assert_true(is_valid_command_id(id, allow_test=True), msg)
252+
asserts.assert_true(is_valid_command_id(id, allow_test=False), msg)
253+
asserts.assert_true(is_standard_command_id(id), msg)
237254

238255
def check_manufacturer(id):
239256
id_type = command_id_type(id)
240257
msg = f"Incorrect command range assessment, expecting manufacturer {id:08x}, type = {id_type}"
241258
asserts.assert_equal(id_type, CommandIdType.kManufacturer, msg)
242-
asserts.assert_true(is_valid_command_id(id_type, allow_test=True), msg)
243-
asserts.assert_true(is_valid_command_id(id_type, allow_test=False), msg)
259+
asserts.assert_true(is_valid_command_id(id, allow_test=True), msg)
260+
asserts.assert_true(is_valid_command_id(id, allow_test=False), msg)
261+
asserts.assert_false(is_standard_command_id(id), msg)
244262

245263
def check_test(id):
246264
id_type = command_id_type(id)
247265
msg = f"Incorrect command range assessment, expecting test {id:08x}, type = {id_type}"
248266
asserts.assert_equal(id_type, CommandIdType.kTest, msg)
249-
asserts.assert_true(is_valid_command_id(id_type, allow_test=True), msg)
250-
asserts.assert_false(is_valid_command_id(id_type, allow_test=False), msg)
267+
asserts.assert_true(is_valid_command_id(id, allow_test=True), msg)
268+
asserts.assert_false(is_valid_command_id(id, allow_test=False), msg)
269+
asserts.assert_false(is_standard_command_id(id), msg)
251270

252271
def check_all_bad(id):
253272
id_type = command_id_type(id)
254273
msg = f"Incorrect command range assessment, expecting invalid {id:08x}, type = {id_type}"
255274
asserts.assert_equal(id_type, CommandIdType.kInvalid, msg)
256-
asserts.assert_false(is_valid_command_id(id_type, allow_test=True), msg)
257-
asserts.assert_false(is_valid_command_id(id_type, allow_test=False), msg)
275+
asserts.assert_false(is_valid_command_id(id, allow_test=True), msg)
276+
asserts.assert_false(is_valid_command_id(id, allow_test=False), msg)
277+
asserts.assert_false(is_standard_command_id(id), msg)
258278

259279
for id in standard_global_good:
260280
check_standard_global(id)

src/python_testing/matter_testing_infrastructure/chip/testing/global_attribute_ids.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ def device_type_id_type(id: int) -> DeviceTypeIdType:
116116
return DeviceTypeIdType.kInvalid
117117

118118

119-
def is_valid_device_type_id(id_type: DeviceTypeIdType, allow_test=False) -> bool:
119+
def is_standard_device_type_id(id: int) -> bool:
120+
id_type = device_type_id_type(id)
121+
return id_type == DeviceTypeIdType.kStandard
122+
123+
124+
def is_valid_device_type_id(id: int, allow_test=False) -> bool:
125+
id_type = device_type_id_type(id)
120126
valid = [DeviceTypeIdType.kStandard, DeviceTypeIdType.kManufacturer]
121127
if allow_test:
122128
valid.append(DeviceTypeIdType.kTest)
@@ -133,7 +139,13 @@ def cluster_id_type(id: int) -> ClusterIdType:
133139
return ClusterIdType.kInvalid
134140

135141

136-
def is_valid_cluster_id(id_type: ClusterIdType, allow_test: bool = False) -> bool:
142+
def is_standard_cluster_id(id: int) -> bool:
143+
id_type = cluster_id_type(id)
144+
return id_type == ClusterIdType.kStandard
145+
146+
147+
def is_valid_cluster_id(id: int, allow_test: bool = False) -> bool:
148+
id_type = cluster_id_type(id)
137149
valid = [ClusterIdType.kStandard, ClusterIdType.kManufacturer]
138150
if allow_test:
139151
valid.append(ClusterIdType.kTest)
@@ -152,13 +164,19 @@ def attribute_id_type(id: int) -> AttributeIdType:
152164
return AttributeIdType.kInvalid
153165

154166

155-
def is_valid_attribute_id(id_type: AttributeIdType, allow_test: bool = False):
167+
def is_valid_attribute_id(id: int, allow_test: bool = False):
168+
id_type = attribute_id_type(id)
156169
valid = [AttributeIdType.kStandardGlobal, AttributeIdType.kStandardNonGlobal, AttributeIdType.kManufacturer]
157170
if allow_test:
158171
valid.append(AttributeIdType.kTest)
159172
return id_type in valid
160173

161174

175+
def is_standard_attribute_id(id: int):
176+
id_type = attribute_id_type(id)
177+
return id_type in [AttributeIdType.kStandardGlobal, AttributeIdType.kStandardNonGlobal]
178+
179+
162180
def command_id_type(id: int) -> CommandIdType:
163181
if id in STANDARD_PREFIX and id in COMMAND_ID_GLOBAL_STANDARD_SUFFIX:
164182
return CommandIdType.kStandardGlobal
@@ -171,7 +189,13 @@ def command_id_type(id: int) -> CommandIdType:
171189
return CommandIdType.kInvalid
172190

173191

174-
def is_valid_command_id(id_type: CommandIdType, allow_test: bool = False):
192+
def is_standard_command_id(id: int):
193+
id_type = command_id_type(id)
194+
return id_type in [CommandIdType.kScopedNonGlobal, CommandIdType.kStandardGlobal]
195+
196+
197+
def is_valid_command_id(id: int, allow_test: bool = False):
198+
id_type = command_id_type(id)
175199
valid = [CommandIdType.kStandardGlobal, CommandIdType.kScopedNonGlobal, CommandIdType.kManufacturer]
176200
if allow_test:
177201
valid.append(CommandIdType.kTest)

0 commit comments

Comments
 (0)