Skip to content

Commit

Permalink
[matter_yamltests] Allow response field to be configured from the con…
Browse files Browse the repository at this point in the history
…fig section (project-chip#27323)
  • Loading branch information
vivien-apple authored Jun 20, 2023
1 parent 9946106 commit fcdc27a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
11 changes: 11 additions & 0 deletions scripts/py_matter_yamltests/matter_yamltests/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,14 @@ def __init__(self, content):

self.tag_key_with_error(content, 'wait')
self.tag_key_with_error(content, 'response')


class TestStepResponseVariableError(TestStepError):
"""Raise when a test step response use a variable but this variable does not exist in the config section.
"""

def __init__(self, content):
message = 'The variable does not exist in the config section.'
super().__init__(message)

self.tag_key_with_error(content, 'response')
25 changes: 20 additions & 5 deletions scripts/py_matter_yamltests/matter_yamltests/yaml_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from typing import Tuple, Union

from .errors import (TestStepError, TestStepGroupResponseError, TestStepInvalidTypeError, TestStepKeyError,
TestStepNodeIdAndGroupIdError, TestStepValueAndValuesError, TestStepVerificationStandaloneError,
TestStepWaitResponseError)
TestStepNodeIdAndGroupIdError, TestStepResponseVariableError, TestStepValueAndValuesError,
TestStepVerificationStandaloneError, TestStepWaitResponseError)
from .fixes import add_yaml_support_for_scientific_notation_without_dot

try:
Expand Down Expand Up @@ -78,12 +78,13 @@ def __check_content(self, content):
tests = content.get('tests', [])
for step_index, step in enumerate(tests):
try:
self.__check_test_step(step)
config = content.get('config', {})
self.__check_test_step(config, step)
except TestStepError as e:
e.update_context(step, step_index)
raise

def __check_test_step(self, content):
def __check_test_step(self, config: dict, content):
schema = {
'label': str,
'identity': str,
Expand All @@ -101,7 +102,7 @@ def __check_test_step(self, content):
'verification': str,
'PICS': str,
'arguments': dict,
'response': (dict, list),
'response': (dict, list, str), # Can be a variable
'minInterval': int,
'maxInterval': int,
'timedInteractionTimeoutMs': int,
Expand All @@ -116,13 +117,21 @@ def __check_test_step(self, content):
self.__rule_step_with_verification_should_be_disabled_or_interactive(
content)
self.__rule_wait_should_not_expect_a_response(content)
self.__rule_response_variable_should_exist_in_config(config, content)

if 'arguments' in content:
arguments = content.get('arguments')
self.__check_test_step_arguments(arguments)

if 'response' in content:
response = content.get('response')

# If the response is a variable, update the response value with the content of the variable such
# such that the error message looks nice if needed.
if isinstance(response, str):
response = config.get(response)
content['response'] = response

if isinstance(response, list):
[self.__check_test_step_response(x) for x in response]
else:
Expand Down Expand Up @@ -241,3 +250,9 @@ def __rule_response_value_and_values_are_mutually_exclusive(self, content):
def __rule_wait_should_not_expect_a_response(self, content):
if 'wait' in content and 'response' in content:
raise TestStepWaitResponseError(content)

def __rule_response_variable_should_exist_in_config(self, config, content):
if 'response' in content:
response = content.get('response')
if isinstance(response, str) and response not in config:
raise TestStepResponseVariableError(content)
2 changes: 1 addition & 1 deletion scripts/py_matter_yamltests/test_yaml_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def test_key_tests_step_response_key(self):
_, _, _, _, tests = load(content.format(value=value))
self.assertEqual(tests, [{'response': [{'value': True}]}])

wrong_values = self._get_wrong_values([dict, list], spaces=6)
wrong_values = self._get_wrong_values([dict, list, str], spaces=6)
for value in wrong_values:
x = content.format(value=value)
self.assertRaises(TestStepInvalidTypeError, load, x)
Expand Down

0 comments on commit fcdc27a

Please sign in to comment.