From ef7d4bc39ec6fb26d500cdb02e35f6fd8348c465 Mon Sep 17 00:00:00 2001 From: Oleksandr Sirko Date: Mon, 16 Dec 2024 17:40:40 +0100 Subject: [PATCH] add tests for pixit validator --- .../TestMatterTestingSupport.py | 126 +++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index d39c869456e8f9..dc4122035ed663 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -22,7 +22,7 @@ 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, +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 @@ -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 + + 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 + if __name__ == "__main__": default_matter_test_main()