From cdd744b5d22eb9ba94e6c1e7d356bb6f24158536 Mon Sep 17 00:00:00 2001 From: Frank Harkins Date: Tue, 30 Apr 2024 12:16:45 +0100 Subject: [PATCH] Fix tests --- ecosystem/cli/ci.py | 10 ++-- ecosystem/utils/submission_parser.py | 14 ++--- tests/test_cli.py | 80 +++++++++++----------------- tests/utils/test_utils.py | 2 +- 4 files changed, 45 insertions(+), 61 deletions(-) diff --git a/ecosystem/cli/ci.py b/ecosystem/cli/ci.py index 03324522ba..d47d0bf2bf 100644 --- a/ecosystem/cli/ci.py +++ b/ecosystem/cli/ci.py @@ -18,19 +18,19 @@ class CliCI: """ @staticmethod - def add_member_from_issue(body: str) -> None: + def add_member_from_issue(body: str, resources_dir: str | None = None) -> None: """Parse an issue created from the issue template and add the member to the database Args: body: body of the created issue + working_dir: (For testing) Path to the working directory Returns: None (side effect is updating database) """ - current_dir = Path.cwd() - resources_dir = Path(current_dir, "ecosystem/resources") + resources_dir = Path(resources_dir or (Path.cwd() / "ecosystem/resources")) - parsed_result = parse_submission_issue(body, current_dir) + parsed_result = parse_submission_issue(body) DAO(path=resources_dir).write(parsed_result) - set_actions_output([ ("SUBMISSION_NAME", parsed_result.name) ]) + set_actions_output([("SUBMISSION_NAME", parsed_result.name)]) diff --git a/ecosystem/utils/submission_parser.py b/ecosystem/utils/submission_parser.py index dcd0380f41..090e03e390 100644 --- a/ecosystem/utils/submission_parser.py +++ b/ecosystem/utils/submission_parser.py @@ -18,10 +18,10 @@ def _clean_section(section: str) -> {str: str}: return (title, section) -def _section_titles_to_ids(sections: dict[str, str], cwd: str) -> dict[str, str]: +def _section_titles_to_ids(sections: dict[str, str]) -> dict[str, str]: """Given a section title, find its `id` from the issue template""" issue_template = yaml.load( - Path(cwd, ".github/ISSUE_TEMPLATE/submission.yml").read_text(), + Path(".github/ISSUE_TEMPLATE/submission.yml").read_text(), Loader=yaml.SafeLoader, ) mapping = { @@ -32,8 +32,7 @@ def _section_titles_to_ids(sections: dict[str, str], cwd: str) -> dict[str, str] return {mapping[key]: value for key, value in sections.items()} - -def parse_submission_issue(body_of_issue: str, current_directory: str) -> Repository: +def parse_submission_issue(body_of_issue: str) -> Repository: """Parse issue body. Args: @@ -47,9 +46,12 @@ def parse_submission_issue(body_of_issue: str, current_directory: str) -> Reposi sections = defaultdict( None, [_clean_section(s) for s in issue_formatted.split("### ")[1:]] ) - args = _section_titles_to_ids(sections, current_directory) + args = _section_titles_to_ids(sections) - args = { key: (None if value=="_No response_" else value) for key, value in args.items() } + args = { + key: (None if value == "_No response_" else value) + for key, value in args.items() + } if args["labels"] is None: args["labels"] = [] diff --git a/tests/test_cli.py b/tests/test_cli.py index 1daa3a4c7d..74ad020cea 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -28,12 +28,10 @@ class TestCli(TestCase): def setUp(self) -> None: self.path = Path(tempfile.mkdtemp()) - if not os.path.exists(self.path): - os.makedirs(self.path) + (self.path / "members").mkdir(parents=True, exist_ok=True) with open(self.path / "labels.json", "w") as file: file.write("{}") self.current_dir = os.path.dirname(os.path.abspath(__file__)) - print(self.current_dir) with open( "{}/resources/issue.md".format(self.current_dir), "r" ) as issue_body_file: @@ -46,7 +44,7 @@ def setUp(self) -> None: def tearDown(self) -> None: shutil.rmtree(self.path) - def test_parser_issue(self): + def test_add_member_from_issue(self): """Tests issue parsing function. Function: Cli -> parser_issue @@ -57,61 +55,45 @@ def test_parser_issue(self): # Issue 1 captured_output = io.StringIO() with redirect_stdout(captured_output): - CliCI.parser_issue(self.issue_body) + CliCI.add_member_from_issue(self.issue_body, self.path) output_value = captured_output.getvalue().split("\n") - self.assertEqual(output_value[0], "SUBMISSION_NAME=My awesome project") - self.assertEqual( - output_value[1], - "SUBMISSION_REPO=http://github.com/awesome/awesome", - ) - self.assertEqual( - output_value[2], - "SUBMISSION_DESCRIPTION=An awesome repo for awesome project multiple" - " paragraphs", - ) - self.assertEqual(output_value[3], "SUBMISSION_LICENCE=Apache License 2.0") - self.assertEqual(output_value[4], "SUBMISSION_CONTACT=toto@gege.com") - self.assertEqual(output_value[5], "SUBMISSION_ALTERNATIVES=tititata") - self.assertEqual(output_value[6], "SUBMISSION_AFFILIATIONS=_No response_") - self.assertEqual( - output_value[7], - "SUBMISSION_LABELS=['tool', 'tutorial', 'paper implementation']", - ) - self.assertEqual( - output_value[8], - "SUBMISSION_WEBSITE=https://qiskit.org/ecosystem/", - ) + + retrieved_repos = DAO(self.path).get_all() + expected = { + "name": "My awesome project", + "url": "http://github.com/awesome/awesome", + "description": "An awesome repo for awesome project multiple paragraphs", + "contact_info": "toto@gege.com", + "alternatives": "tititata", + "licence": "Apache License 2.0", + "labels": ["tool", "tutorial", "paper implementation"], + "website": "https://qiskit.org/ecosystem/", + } + self.assertEqual(len(retrieved_repos), 1) + self.assertEqual(list(retrieved_repos)[0].to_dict(), expected) # Issue 2 captured_output = io.StringIO() with redirect_stdout(captured_output): - CliCI.parser_issue(self.issue_body_2) + CliCI.add_member_from_issue(self.issue_body_2, self.path) output_value = captured_output.getvalue().split("\n") + self.assertEqual(output_value[0], "SUBMISSION_NAME=My awesome project") - self.assertEqual(output_value[0], "SUBMISSION_NAME=awesome") - self.assertEqual( - output_value[1], - "SUBMISSION_REPO=http://github.com/awesome/awesome", - ) - self.assertEqual( - output_value[2], - "SUBMISSION_DESCRIPTION=An awesome repo for awesome project", - ) - self.assertEqual(output_value[3], "SUBMISSION_LICENCE=Apache License 2.0") - self.assertEqual(output_value[4], "SUBMISSION_CONTACT=toto@gege.com") - self.assertEqual(output_value[5], "SUBMISSION_ALTERNATIVES=_No response_") - self.assertEqual(output_value[6], "SUBMISSION_AFFILIATIONS=Awesome Inc.") - self.assertEqual( - output_value[7], - "SUBMISSION_LABELS=[]", - ) - self.assertEqual( - output_value[8], - "SUBMISSION_WEBSITE=None", - ) + retrieved_repos = DAO(self.path).get_all() + expected = { + "name": "My awesome project", + "url": "http://github.com/awesome/awesome", + "description": "An awesome repo for awesome project", + "contact_info": "toto@gege.com", + "licence": "Apache License 2.0", + "affiliations": "Awesome Inc.", + "labels": [], + } + self.assertEqual(len(retrieved_repos), 1) + self.assertEqual(list(retrieved_repos)[0].to_dict(), expected) def test_update_badges(self): """Tests creating badges.""" diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 211d337b34..2d69feefbf 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -36,7 +36,7 @@ def test_issue_parsing(self): self.assertEqual(parsed_result.contact_info, "toto@gege.com") self.assertEqual(parsed_result.alternatives, "tititata") self.assertEqual(parsed_result.licence, "Apache License 2.0") - self.assertEqual(parsed_result.affiliations, "_No response_") + self.assertEqual(parsed_result.affiliations, None) self.assertEqual( parsed_result.labels, ["tool", "tutorial", "paper implementation"] )