Skip to content

Commit

Permalink
Revert "Cherry-pick test_python_parser.py from origin/main"
Browse files Browse the repository at this point in the history
This reverts commit 2cd99b7.
  • Loading branch information
swan-amazon committed Dec 20, 2024
1 parent ff8943b commit f17722d
Showing 1 changed file with 12 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,148 +20,19 @@

from ...python_testing.models.python_test_parser import parse_python_script

sample_single_test_python_file_content = """
class TC_Sample(MatterBaseTest):

def desc_TC_Sample(self) -> str:
return "Sample TC Description"
def test_python_file_parser() -> None:
file_path = Path(
"/app/backend/test_collections/matter/sdk_tests/support/tests/python_tests/test_python_script/python_tests_info.json"
)

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
tests = parse_python_script(file_path)

def test_TC_Sample(self):
print("Test execution")
def pics_TC_Sample(self):
pics = ["MCORE.ROLE.COMMISSIONEE"]
"""

sample_multi_tests_single_class_python_file_content = """
class TC_CommonSample(MatterBaseTest):
def steps_TC_Sample_1_1(self) -> list[TestStep]:
steps = [
TestStep(1, "Commissioning, already done", is_commissioning=True),
TestStep(2, "Second step"),
TestStep(3, "Third step"),
]
return steps
def test_TC_ABC_1_1(self):
print("Test execution")
def test_TC_Sample_1_1(self):
print("Test execution")
def test_TC_Sample_1_2(self):
print("Test execution")
"""

sample_multi_tests_multi_classes_python_file_content = """
class TC_CommonSample(MatterBaseTest):
def steps_TC_Sample_1_1(self) -> list[TestStep]:
steps = [
TestStep(1, "Commissioning, already done", is_commissioning=True),
TestStep(2, "Second step"),
TestStep(3, "Third step"),
]
return steps
def test_TC_ABC_1_1(self):
print("Test execution")
def test_TC_Sample_1_1(self):
print("Test execution")
def test_TC_Sample_1_2(self):
print("Test execution")
class TC_CommonSample_2(MatterBaseTest):
def test_TC_ABC_1_2(self):
print("Test execution")
def test_TC_DEF_1_3(self):
print("Test execution")
"""


def test_single_test_python_file_parser() -> None:
file_path = Path("/test/TC_Sample.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.matter.sdk_tests.support.python_testing.models.python_test_parser."
"open",
new=mock.mock_open(read_data=sample_single_test_python_file_content),
) as file_open:
tests = parse_python_script(file_path)

file_open.assert_called_once_with(file_path, "r")

assert len(tests) is 1
assert all(test.path == file_path for test in tests)
assert len(tests[0].steps) is 3
assert tests[0].description == "Sample TC Description"
assert len(tests) == 4
assert "sdk/TC_Commissioning_Sample" == str(tests[0].path)
assert len(tests[0].steps) == 3
assert tests[0].description == "Commissioning Sample TC Description"
assert "MCORE.ROLE.COMMISSIONEE" in tests[0].PICS
assert len(tests[0].PICS) is 1


def test_multi_tests_single_class_python_file_parser() -> None:
file_path = Path("/test/TC_Sample.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.matter.sdk_tests.support.python_testing.models.python_test_parser."
"open",
new=mock.mock_open(
read_data=sample_multi_tests_single_class_python_file_content
),
) as file_open:
tests = parse_python_script(file_path)

file_open.assert_called_once_with(file_path, "r")

assert len(tests) is 3
assert all(test.path == file_path for test in tests)
test_names = [test.name for test in tests]
assert "TC_ABC_1_1" in test_names
assert "TC_Sample_1_1" in test_names
assert "TC_Sample_1_2" in test_names


def test_multi_tests_multi_classes_python_file_parser() -> None:
file_path = Path("/test/TC_Sample.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.matter.sdk_tests.support.python_testing.models.python_test_parser."
"open",
new=mock.mock_open(
read_data=sample_multi_tests_multi_classes_python_file_content
),
) as file_open:
tests = parse_python_script(file_path)

file_open.assert_called_once_with(file_path, "r")

assert len(tests) is 5
assert all(test.path == file_path for test in tests)
test_names = [test.name for test in tests]
assert "TC_ABC_1_1" in test_names
assert "TC_Sample_1_1" in test_names
assert "TC_Sample_1_2" in test_names
assert "TC_ABC_1_2" in test_names
assert "TC_DEF_1_3" in test_names
assert len(tests[0].PICS) == 2
count = sum(1 for test in tests if str(test.path) == "sdk/TC_SameClass")
assert count == 2

0 comments on commit f17722d

Please sign in to comment.