Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Apr 30, 2024
1 parent 39e0b8e commit cdd744b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 61 deletions.
10 changes: 5 additions & 5 deletions ecosystem/cli/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
14 changes: 8 additions & 6 deletions ecosystem/utils/submission_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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:
Expand All @@ -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"] = []
Expand Down
80 changes: 31 additions & 49 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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], "[email protected]")
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": "[email protected]",
"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], "[email protected]")
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": "[email protected]",
"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."""
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_issue_parsing(self):
self.assertEqual(parsed_result.contact_info, "[email protected]")
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"]
)

0 comments on commit cdd744b

Please sign in to comment.