Skip to content

Commit

Permalink
Added Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rquidute committed Nov 8, 2023
1 parent df0e129 commit fe24421
Show file tree
Hide file tree
Showing 12 changed files with 609 additions and 44 deletions.
59 changes: 59 additions & 0 deletions app/tests/python_tests/test_python_folder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pathlib import Path
from unittest import mock

from test_collections.sdk_tests.support.python_testing.models.python_test_folder import (
PythonTestFolder,
)

test_python_path = Path("/test/python")


def test_python_folder_version() -> None:
version_file_content = "python_test_version"

# We mock open to read version_file_content and Path exists to ignore that we're
# testing with a fake path
with mock.patch(
"test_collections.sdk_tests.support.python_testing.models.python_test_folder.open",
new=mock.mock_open(read_data=version_file_content),
), mock.patch.object(target=Path, attribute="exists", return_value=True) as _:
python_test_folder = PythonTestFolder(test_python_path)

assert python_test_folder.version == version_file_content


def test_python_folder_version_missing() -> None:
expected_version = "Unknown"
with mock.patch.object(target=Path, attribute="exists", return_value=False) as _:
python_folder = PythonTestFolder(test_python_path)
assert python_folder.version == expected_version


def test_python_folder_filename_pattern() -> None:
"""Test PythonTestFolder will search for files with filename pattern."""
with mock.patch.object(target=Path, attribute="glob") as path_glob:
# Default file_name_patter: *
python_folder = PythonTestFolder(test_python_path)
_ = python_folder.python_file_paths()
path_glob.assert_called_once_with("*.py")

path_glob.reset_mock()
pattern = "TC_*"
python_test_folder = PythonTestFolder(test_python_path, filename_pattern=pattern)
_ = python_test_folder.python_file_paths()
path_glob.assert_called_once_with(f"{pattern}.py")
87 changes: 87 additions & 0 deletions app/tests/python_tests/test_python_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pathlib import Path
from unittest import mock

from test_collections.sdk_tests.support.python_testing.models.python_test_parser import (
PythonParserException,
parse_python_test,
)

sample_invalid_python_file_content = """
class TC_Sample(MatterBaseTest):
def steps_TC_Sample(self) -> list[TestStep]:
steps = [
TestStep(1, "Commissioning, already done", is_commissioning=True),
TestStep(2, "Second step"),
TestStep(3, "Third step"),
]
return steps
@async_test_body
async def test_steps_TC_Sample(self):
print("Test execution")
"""

sample_python_file_content = """
class TC_Sample(MatterBaseTest):
def desc_TC_Sample(self) -> str:
return "Sample TC Description"
def steps_TC_Sample(self) -> list[TestStep]:
steps = [
TestStep(1, "Commissioning, already done", is_commissioning=True),
TestStep(2, "Second step"),
TestStep(3, "Third step"),
]
return steps
@async_test_body
async def test_steps_TC_Sample(self):
print("Test execution")
"""


def test_python_file_parser_throws_pythonparserexception() -> None:
file_path = Path("/test/file.py")

with mock.patch(
"test_collections.sdk_tests.support.python_testing.models.python_test_parser.open",
new=mock.mock_open(read_data=sample_invalid_python_file_content),
):
try:
parse_python_test(file_path)
except PythonParserException as e:
assert "Test Case file does not have methods desc_file or steps_file" == str(e)


def test_python_file_parser() -> None:
file_path = Path("/test/file.py")

# We mock builtin `open` method to read sample python file content,
# to avoid having to load a real file.
with mock.patch(
"test_collections.sdk_tests.support.python_testing.models.python_test_parser.open",
new=mock.mock_open(read_data=sample_python_file_content),
) as file_open:
test = parse_python_test(file_path)

file_open.assert_called_once_with(file_path, "r")
assert test.path == file_path
1 change: 1 addition & 0 deletions app/tests/python_tests/test_python_script/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unit-test-python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

#
# Copyright (c) 2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# type: ignore
# Ignore mypy type check for this file

"""
This is just a sample test case that should come from SDK.
It should not compile or run.
"""
class TC_Sample(MatterBaseTest):

def desc_TC_Sample(self) -> str:
return "Sample TC Description"

def steps_TC_Sample(self) -> list[TestStep]:
steps = [
TestStep(1, "Commissioning, already done", is_commissioning=True),
TestStep(2, "Second step"),
TestStep(3, "Third step"),
]
return steps

@async_test_body
async def test_steps_TC_Sample(self):
print("Test execution")
Loading

0 comments on commit fe24421

Please sign in to comment.