Skip to content

Commit

Permalink
Consolidate basic assert tests (#37168)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored Jan 23, 2025
1 parent 575a440 commit b2b6275
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 109 deletions.
17 changes: 9 additions & 8 deletions src/python_testing/TC_DGSW_2_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#

import chip.clusters as Clusters
from chip.testing import matter_asserts
from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main


Expand Down Expand Up @@ -81,41 +82,41 @@ async def test_TC_DGSW_2_1(self):
# Validate each element in the thread_metrics_list
for metric in thread_metrics_list:
# The Id field is mandatory
self.assert_valid_uint64(metric.id, "Id")
matter_asserts.assert_valid_uint64(metric.id, "Id")

# Validate the optional Name field
if metric.name is not None:
self.assert_valid_str(metric.name, "Name")
matter_asserts.assert_is_string(metric.name, "Name")

# Validate the optional StackFreeCurrent field
if metric.stackFreeCurrent is not None:
self.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")
matter_asserts.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")

# Validate the optional StackFreeMinimum field
if metric.stackFreeMinimum is not None:
self.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")
matter_asserts.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")

# Validate the optional StackSize field
if metric.stackSize is not None:
self.assert_valid_uint32(metric.stackSize, "StackSize")
matter_asserts.assert_valid_uint32(metric.stackSize, "StackSize")

# STEP 3: TH reads from the DUT the CurrentHeapFree attribute
self.step(3)
if self.pics_guard(attributes.CurrentHeapFree.attribute_id in attribute_list):
current_heap_free_attr = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapFree)
self.assert_valid_uint64(current_heap_free_attr, "CurrentHeapFree")
matter_asserts.assert_valid_uint64(current_heap_free_attr, "CurrentHeapFree")

# STEP 4: TH reads from the DUT the CurrentHeapUsed attribute
self.step(4)
if self.pics_guard(attributes.CurrentHeapUsed.attribute_id in attribute_list):
current_heap_used_attr = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapUsed)
self.assert_valid_uint64(current_heap_used_attr, "CurrentHeapUsed")
matter_asserts.assert_valid_uint64(current_heap_used_attr, "CurrentHeapUsed")

# STEP 5: TH reads from the DUT the CurrentHeapHighWatermark attribute
self.step(5)
if self.pics_guard(attributes.CurrentHeapHighWatermark.attribute_id in attribute_list):
current_heap_high_watermark_attr = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapHighWatermark)
self.assert_valid_uint64(current_heap_high_watermark_attr, "CurrentHeapHighWatermark")
matter_asserts.assert_valid_uint64(current_heap_high_watermark_attr, "CurrentHeapHighWatermark")


if __name__ == "__main__":
Expand Down
17 changes: 4 additions & 13 deletions src/python_testing/TC_DGSW_2_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
#

import chip.clusters as Clusters
from chip.testing import matter_asserts
from chip.testing.matter_testing import EventChangeCallback, MatterBaseTest, TestStep, async_test_body, default_matter_test_main
from mobly import asserts


class TC_DGSW_2_2(MatterBaseTest):
Expand All @@ -66,22 +66,13 @@ def validate_soft_fault_event_data(self, event_data):
"""

# Validate 'Id' field: Ensure it is a uint64 type
asserts.assert_true(
self.is_valid_uint_value(event_data.id, bit_count=64),
"The 'Id' field must be a uint64 type"
)
matter_asserts.assert_valid_uint64(event_data.id, "Id")

# Validate 'Name' field: Ensure it is a string
asserts.assert_true(
isinstance(event_data.name, str),
"The 'Name' field must be a string type"
)
matter_asserts.assert_is_string(event_data.name, "Name")

# Validate 'FaultRecording' field: Ensure it is an octet string (bytes or bytearray)
asserts.assert_true(
self.is_valid_octet_string(event_data.faultRecording),
"The 'FaultRecording' field must be an octet string (bytes or bytearray)"
)
matter_asserts.assert_is_octstr(event_data.faultRecording, "FaultRecording")

def desc_TC_DGSW_2_2(self) -> str:
"""Returns a description of this test"""
Expand Down
27 changes: 14 additions & 13 deletions src/python_testing/TC_DGSW_2_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import logging

import chip.clusters as Clusters
from chip.testing import matter_asserts
from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main
from mobly import asserts

Expand Down Expand Up @@ -94,31 +95,31 @@ async def test_TC_DGSW_2_3(self):
# Iterate over all items in the list and validate each one
for metric in thread_metrics_original:
# The Id field is mandatory
self.assert_valid_uint64(metric.id, "Id")
matter_asserts.assert_valid_uint64(metric.id, "Id")

if metric.name is not None:
self.assert_valid_str(metric.name, "Name")
matter_asserts.assert_is_string(metric.name, "Name")

if metric.stackFreeCurrent is not None:
self.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")
matter_asserts.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")

if metric.stackFreeMinimum is not None:
self.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")
matter_asserts.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")

if metric.stackSize is not None:
self.assert_valid_uint32(metric.stackSize, "StackSize")
matter_asserts.assert_valid_uint32(metric.stackSize, "StackSize")

# STEP 4: TH reads from the DUT the CurrentHeapHighWatermark attribute
self.step(4)
if self.pics_guard(attributes.CurrentHeapHighWatermark.attribute_id in attribute_list):
high_watermark_original = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapHighWatermark)
self.assert_valid_uint64(high_watermark_original, "CurrentHeapHighWatermark")
matter_asserts.assert_valid_uint64(high_watermark_original, "CurrentHeapHighWatermark")

# STEP 5: TH reads from the DUT the CurrentHeapUsed attribute
self.step(5)
if self.pics_guard(attributes.CurrentHeapUsed.attribute_id in attribute_list):
current_heap_used_original = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapUsed)
self.assert_valid_uint64(current_heap_used_original, "CurrentHeapUsed")
matter_asserts.assert_valid_uint64(current_heap_used_original, "CurrentHeapUsed")

if high_watermark_original is not None:
asserts.assert_true(current_heap_used_original <= high_watermark_original,
Expand All @@ -133,7 +134,7 @@ async def test_TC_DGSW_2_3(self):
self.step(7)
if self.pics_guard(attributes.CurrentHeapHighWatermark.attribute_id in attribute_list):
current_heap_high_watermark = await self.read_dgsw_attribute_expect_success(endpoint=endpoint, attribute=attributes.CurrentHeapHighWatermark)
self.assert_valid_uint64(current_heap_high_watermark, "CurrentHeapHighWatermark")
matter_asserts.assert_valid_uint64(current_heap_high_watermark, "CurrentHeapHighWatermark")

# Verify that the returned value is <= high_watermark_original
asserts.assert_true(current_heap_high_watermark <= high_watermark_original,
Expand All @@ -152,19 +153,19 @@ async def test_TC_DGSW_2_3(self):

# Validate all elements in the list
for metric in thread_metrics_reset:
self.assert_valid_uint64(metric.id, "Id")
matter_asserts.assert_valid_uint64(metric.id, "Id")

if metric.name is not None:
self.assert_valid_str(metric.name, "Name")
matter_asserts.assert_is_string(metric.name, "Name")

if metric.stackFreeCurrent is not None:
self.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")
matter_asserts.assert_valid_uint32(metric.stackFreeCurrent, "StackFreeCurrent")

if metric.stackFreeMinimum is not None:
self.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")
matter_asserts.assert_valid_uint32(metric.stackFreeMinimum, "StackFreeMinimum")

if metric.stackSize is not None:
self.assert_valid_uint32(metric.stackSize, "StackSize")
matter_asserts.assert_valid_uint32(metric.stackSize, "StackSize")

# Ensure the list length matches thread_metrics_original to simplify matching
asserts.assert_equal(len(thread_metrics_reset), len(thread_metrics_original),
Expand Down
23 changes: 12 additions & 11 deletions src/python_testing/TC_DGWIFI_2_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import chip.clusters as Clusters
from chip.clusters.Types import Nullable, NullValue
from chip.testing import matter_asserts
from chip.testing.matter_testing import MatterBaseTest, TestStep, async_test_body, default_matter_test_main
from mobly import asserts

Expand Down Expand Up @@ -131,7 +132,7 @@ async def test_TC_DGWIFI_2_1(self):
# Just do a minimal check here; you can refine or extend based on the spec.
if security_type is not NullValue:
security_type_value = security_type.Value
self.assert_valid_uint8(security_type_value, "SecurityType")
matter_asserts.assert_valid_uint8(security_type_value, "SecurityType")

# Check if the security_type is a valid SecurityTypeEnum member
self.assert_true(
Expand All @@ -154,7 +155,7 @@ async def test_TC_DGWIFI_2_1(self):
# WiFiVersion is an enum. If not configured or operational, might be NULL.
if wifi_version is not NullValue:
wifi_version_value = wifi_version.Value
self.assert_valid_uint8(wifi_version_value, "WiFiVersion")
matter_asserts.assert_valid_uint8(wifi_version_value, "WiFiVersion")

# Check if the wifi_version is a valid WiFiVersionEnum member
self.assert_true(wifi_version_value in [item.value for item in Clusters.Objects.WiFiNetworkDiagnostics.Enums.WiFiVersionEnum],
Expand All @@ -171,7 +172,7 @@ async def test_TC_DGWIFI_2_1(self):
channel_number = await self.read_dgwifi_attribute_expect_success(endpoint=endpoint, attribute=attributes.ChannelNumber)
# If not operational, might be NULL. Else we expect an unsigned integer channel.
if channel_number is not NullValue:
self.assert_valid_uint16(channel_number.Value, "ChannelNumber")
matter_asserts.assert_valid_uint16(channel_number.Value, "ChannelNumber")

#
# STEP 6: TH reads RSSI attribute
Expand All @@ -195,7 +196,7 @@ async def test_TC_DGWIFI_2_1(self):
"BeaconLostCount must be of type 'Nullable' when not None.")

if beacon_lost_count is not NullValue:
self.assert_valid_uint32(beacon_lost_count.Value, "BeaconLostCount")
matter_asserts.assert_valid_uint32(beacon_lost_count.Value, "BeaconLostCount")

#
# STEP 8: TH reads BeaconRxCount attribute
Expand All @@ -207,7 +208,7 @@ async def test_TC_DGWIFI_2_1(self):
"BeaconRxCount must be of type 'Nullable' when not None.")

if beacon_rx_count is not NullValue:
self.assert_valid_uint32(beacon_rx_count.Value, "BeaconRxCount")
matter_asserts.assert_valid_uint32(beacon_rx_count.Value, "BeaconRxCount")

#
# STEP 9: TH reads PacketMulticastRxCount attribute
Expand All @@ -219,7 +220,7 @@ async def test_TC_DGWIFI_2_1(self):
"PacketMulticastRxCount must be of type 'Nullable' when not None.")

if pkt_multi_rx is not NullValue:
self.assert_valid_uint32(pkt_multi_rx.Value, "PacketMulticastRxCount")
matter_asserts.assert_valid_uint32(pkt_multi_rx.Value, "PacketMulticastRxCount")

#
# STEP 10: TH reads PacketMulticastTxCount attribute
Expand All @@ -231,7 +232,7 @@ async def test_TC_DGWIFI_2_1(self):
"PacketMulticastTxCount must be of type 'Nullable' when not None.")

if pkt_multi_tx is not NullValue:
self.assert_valid_uint32(pkt_multi_tx.Value, "PacketMulticastTxCount")
matter_asserts.assert_valid_uint32(pkt_multi_tx.Value, "PacketMulticastTxCount")

#
# STEP 11: TH reads PacketUnicastRxCount attribute
Expand All @@ -243,7 +244,7 @@ async def test_TC_DGWIFI_2_1(self):
"PacketUnicastRxCount must be of type 'Nullable' when not None.")

if pkt_uni_rx is not NullValue:
self.assert_valid_uint32(pkt_uni_rx.Value, "PacketUnicastRxCount")
matter_asserts.assert_valid_uint32(pkt_uni_rx.Value, "PacketUnicastRxCount")

#
# STEP 12: TH reads PacketUnicastTxCount attribute
Expand All @@ -255,7 +256,7 @@ async def test_TC_DGWIFI_2_1(self):
"PacketUnicastTxCount must be of type 'Nullable' when not None.")

if pkt_uni_tx is not NullValue:
self.assert_valid_uint32(pkt_uni_tx.Value, "PacketUnicastTxCount")
matter_asserts.assert_valid_uint32(pkt_uni_tx.Value, "PacketUnicastTxCount")

#
# STEP 13: TH reads CurrentMaxRate attribute
Expand All @@ -268,7 +269,7 @@ async def test_TC_DGWIFI_2_1(self):
"CurrentMaxRate must be of type 'Nullable' when not None.")

if current_max_rate is not NullValue:
self.assert_valid_uint64(current_max_rate.Value, "CurrentMaxRate")
matter_asserts.assert_valid_uint64(current_max_rate.Value, "CurrentMaxRate")

#
# STEP 14: TH reads OverrunCount attribute
Expand All @@ -281,7 +282,7 @@ async def test_TC_DGWIFI_2_1(self):
"OverrunCount must be of type 'Nullable' when not None.")

if overrun_count is not NullValue:
self.assert_valid_uint64(overrun_count.Value, "OverrunCount")
matter_asserts.assert_valid_uint64(overrun_count.Value, "OverrunCount")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit b2b6275

Please sign in to comment.