Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative PIXITs in python tests #395 #36817

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 127 additions & 3 deletions src/python_testing/TestMatterTestingSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

import chip.clusters as Clusters
from chip.clusters.Types import Nullable, NullValue
from chip.testing.matter_testing import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main,
get_wait_seconds_from_set_time, parse_matter_test_args, type_matches,
utc_time_in_matter_epoch)
from chip.testing.matter_testing import (MatterBaseTest, PIXITDefinition, PIXITType, PIXITValidationError, PIXITValidator,
async_test_body, compare_time, default_matter_test_main, get_wait_seconds_from_set_time,
parse_matter_test_args, type_matches, utc_time_in_matter_epoch)
from chip.testing.pics import parse_pics, parse_pics_xml
from chip.testing.taglist_and_topology_test import (TagProblem, create_device_type_list_for_root, create_device_type_lists,
find_tag_list_problems, find_tree_roots, flat_list_ok, get_all_children,
Expand Down Expand Up @@ -656,6 +656,130 @@ def test_parse_matter_test_args(self):
asserts.assert_equal(parsed.global_test_params.get("PIXIT.TEST.STR.MULTI.2"), "bar")
asserts.assert_equal(parsed.global_test_params.get("PIXIT.TEST.JSON"), {"key": "value"})

def test_pixit_validation(self):
# Test integer validation
PIXITValidator.validate_int_pixit_value("123") # Should pass
PIXITValidator.validate_int_pixit_value("-123") # Should pass
PIXITValidator.validate_int_pixit_value("0") # Should pass

try:
PIXITValidator.validate_int_pixit_value("abc")
asserts.fail("Expected ValueError for invalid integer")
except ValueError:
pass

try:
PIXITValidator.validate_int_pixit_value("12.34")
asserts.fail("Expected ValueError for float value")
except ValueError:
pass

# Test boolean validation
PIXITValidator.validate_bool_pixit_value("true") # Should pass
PIXITValidator.validate_bool_pixit_value("false") # Should pass
PIXITValidator.validate_bool_pixit_value("True") # Should pass
PIXITValidator.validate_bool_pixit_value("False") # Should pass

try:
PIXITValidator.validate_bool_pixit_value("yes")
asserts.fail("Expected ValueError for invalid boolean")
except ValueError:
pass

try:
PIXITValidator.validate_bool_pixit_value("1")
asserts.fail("Expected ValueError for numeric boolean")
except ValueError:
pass

# Test float validation
PIXITValidator.validate_float_pixit_value("123.45") # Should pass
PIXITValidator.validate_float_pixit_value("-123.45") # Should pass
PIXITValidator.validate_float_pixit_value("0.0") # Should pass

try:
PIXITValidator.validate_float_pixit_value("abc")
asserts.fail("Expected ValueError for invalid float")
except ValueError:
pass

# Test string validation - should accept most inputs
PIXITValidator.validate_string_pixit_value("abc") # Should pass
PIXITValidator.validate_string_pixit_value("123") # Should pass

# Test JSON validation
PIXITValidator.validate_json_pixit_value('{"key": "value"}') # Should pass
PIXITValidator.validate_json_pixit_value('[]') # Should pass
PIXITValidator.validate_json_pixit_value('null') # Should pass

try:
PIXITValidator.validate_json_pixit_value('{invalid json}')
asserts.fail("Expected ValueError for invalid JSON")
except ValueError:
pass

try:
PIXITValidator.validate_json_pixit_value('{"unclosed": "object"')
asserts.fail("Expected ValueError for malformed JSON")
except ValueError:
pass

# Test hex validation
PIXITValidator.validate_hex_pixit_value("0x1234") # Should pass
PIXITValidator.validate_hex_pixit_value("1234") # Should pass
PIXITValidator.validate_hex_pixit_value("ABCD") # Should pass
PIXITValidator.validate_hex_pixit_value("abcd") # Should pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one of the other formats we sometimes get is hex:12ab. I don't know if that is handled correctly in the argument parser though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support for hex:12ab format


try:
PIXITValidator.validate_hex_pixit_value("0x123") # Odd number of digits
asserts.fail("Expected ValueError for odd number of hex digits")
except ValueError:
pass

try:
PIXITValidator.validate_hex_pixit_value("123") # Odd number of digits
asserts.fail("Expected ValueError for odd number of hex digits")
except ValueError:
pass

try:
PIXITValidator.validate_hex_pixit_value("WXYZ") # Invalid hex chars
asserts.fail("Expected ValueError for invalid hex characters")
except ValueError:
pass

# Test full PIXIT validation
pixit_def = PIXITDefinition(
name="PIXIT.TEST.INT",
pixit_type=PIXITType.INT,
description="Test integer PIXIT",
required=True
)

PIXITValidator.validate_value("123", pixit_def) # Should pass

try:
PIXITValidator.validate_value("abc", pixit_def)
asserts.fail("Expected PIXITValidationError for invalid type")
except PIXITValidationError:
pass

try:
PIXITValidator.validate_value(None, pixit_def) # Required but None
asserts.fail("Expected PIXITValidationError for missing required value")
except PIXITValidationError:
pass

# Test optional PIXIT
optional_pixit = PIXITDefinition(
name="PIXIT.TEST.INT",
pixit_type=PIXITType.INT,
description="Test integer PIXIT",
required=False
)

PIXITValidator.validate_value(None, optional_pixit) # Should pass as it's optional

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also please add a test for the validate_pixits function to ensure they get mapped properly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added:

image


if __name__ == "__main__":
default_matter_test_main()
Loading
Loading