Skip to content

Commit

Permalink
Fix Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rquidute committed Dec 1, 2023
1 parent 5904c9d commit 3198ecd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 86 deletions.
23 changes: 1 addition & 22 deletions app/tests/python_tests/test_python_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,6 @@ def test_python_test_case_class_pics() -> None:
assert case_class.pics() == test_PICS


def test_python_test_case_class_default_test_parameters() -> None:
"""Test that the default_test_parameters of the python test is available in the class
method default_test_parameters on TestCase.
Also parameters with type in Python test should be flattened and type dropped."""

test_input_config = {
"param1": "value1",
"param2": {"type": "config_type", "defaultValue": "value2"},
}

test = python_test_instance(config=test_input_config)
expected_default_test_parameters = {"param1": "value1", "param2": "value2"}

# Create a subclass of PythonTest
case_class: Type[PythonTestCase] = PythonTestCase.class_factory(
test=test, python_test_version="version"
)
assert case_class.default_test_parameters() == expected_default_test_parameters


def test_class_factory_test_public_id() -> None:
"""Test that class factory correctly finds identifier 'TC-XX-1.1' in python test name.
And set it as public_id in metadata"""
Expand Down Expand Up @@ -189,7 +168,7 @@ async def test_python_version_logging() -> None:
except TestError:
pass
logger_info.assert_called()
logger_info.assert_any_call(f"Python Test Version: {test_python_version}")
logger_info.assert_any_call("Test Setup")


def test_normal_steps_for_python_tests() -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import re
from asyncio import sleep
from multiprocessing.managers import BaseManager
from typing import Any, Type, TypeVar
Expand Down Expand Up @@ -95,81 +96,49 @@ def pics(cls) -> set[str]:
@classmethod
def class_factory(cls, test: PythonTest, python_test_version: str) -> Type[T]:
"""class factory method for PythonTestCase."""

identifier = cls.__test_identifier(test.name)
class_name = cls.__class_name(identifier)
title = identifier

return type(
test.name,
class_name,
(cls,),
{
"python_test": test,
"python_test_version": python_test_version,
"chip_tool_test_identifier": test.name,
"chip_tool_test_identifier": class_name,
"metadata": {
"public_id": test.name,
"public_id": identifier,
"version": "0.0.1",
"title": test.name,
"title": title,
"description": test.description,
},
},
)

@staticmethod
def __test_identifier(name: str) -> str:
"""Find TC-XX-1.1 in YAML title.
Note some have [TC-XX-1.1] and others TC-XX-1.1
"""
title_pattern = re.compile(r"(?P<title>TC-[^\s\]]*)")
if match := re.search(title_pattern, name):
return match["title"]
else:
return name

@staticmethod
def __class_name(identifier: str) -> str:
"""Replace all non-alphanumeric characters with _ to make valid class name."""
return re.sub("[^0-9a-zA-Z]+", "_", identifier)

async def setup(self) -> None:
logger.info("Test Setup")

async def cleanup(self) -> None:
logger.info("Test Cleanup")

def __generate_command_arguments(self) -> list:
# All valid arguments for python test
valid_args = [
"ble_interface_id",
"commissioning_method",
"controller_node_id",
"discriminator",
"endpoint",
"logs_path",
"PICS",
"paa_trust_store_path",
"timeout",
"trace_to",
"int_arg",
"float_arg",
"string_arg",
"json_arg",
"hex_arg",
"bool_arg",
"storage_path",
"passcode",
"dut-node-id",
]

dut_config = self.project.config.dut_config
test_parameters = self.project.config.test_parameters

pairing_mode = (
"on-network"
if dut_config.pairing_mode == "onnetwork"
else dut_config.pairing_mode
)

arguments = []
# Retrieve arguments from dut_config
arguments.append(f"--discriminator {dut_config.discriminator}")
arguments.append(f"--passcode {dut_config.setup_code}")
arguments.append(f"--commissioning-method {pairing_mode}")

# Retrieve arguments from test_parameters

if test_parameters:
for name, value in test_parameters.items():
if name in valid_args:
if str(value) != "":
arguments.append(f"--{name.replace('_','-')} {str(value)}")
else:
arguments.append(f"--{name.replace('_','-')} " "")
else:
logger.warning(f"Argument {name} is not valid")

return arguments

async def execute(self) -> None:
try:
logger.info("Running Python Test: " + self.metadata["title"])
Expand All @@ -180,15 +149,15 @@ async def execute(self) -> None:
test_runner_hooks = manager.TestRunnerHooks() # type: ignore

runner_class = RUNNER_CLASS_PATH + RUNNER_CLASS
command = [f"{runner_class} {self.metadata['title']}"]

# Generate the command argument by getting the test_parameters from
# project configuration
command_arguments = self.__generate_command_arguments()
command.extend(command_arguments)
command = (
f"{runner_class} {self.metadata['title']}"
" --commissioning-method on-network --discriminator 3840 --passcode"
" 20202021 --storage-path /root/admin_storage.json"
" --paa-trust-store-path /paa-root-certs"
)

if self.chip_tool.pics_file_created:
command.append(f" --PICS {PICS_FILE_PATH}")
command += f" --PICS {PICS_FILE_PATH}"

# TODO Ignoring stream from docker execution
self.chip_tool.send_command(
Expand Down

0 comments on commit 3198ecd

Please sign in to comment.