import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) from atlassian.jira import Entry, entry_of, Link from failure import Failure from atlassian import jira BLOCKED = {"id": "9", "type": {"inward": "is blocked by"}, "inwardIssue": {"key": "ABC-2"}} SHAPE = "ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: " def raw(*links: object, **more: object) -> dict[str, object]: return {"key": "ABC-1", "fields": {"issuelinks": list(links)}} | more class TheHeadOfAnEntry(unittest.TestCase): def test_holds_the_key_the_fields_the_blocked_by_links_and_the_property_asked_for(self): outward = {"id": "3", "type": {"inward": "is blocked by"}, "outwardIssue": {"key": "ABC-3"}} relates = {"id": "4", "type": {"inward": "relates to"}, "inwardIssue": {"key": "ABC-4"}} found = entry_of(raw(BLOCKED, outward, relates, properties={"plan": {"L": 2}}), "plan") self.assertEqual(found, Entry("ABC-1", {"issuelinks": [BLOCKED, outward, relates]}, (Link("ABC-2", "9"),), {"L": 2})) self.assertEqual(entry_of(raw(), "plan"), Entry("ABC-1", {"issuelinks": []}, (), {})) def test_finds_a_blocked_by_link_listed_after_a_link_of_another_kind(self): outward = {"id": "3", "type": {"inward": "is blocked by"}, "outwardIssue": {"key": "ABC-3"}} relates = {"id": "4", "type": {"inward": "relates to"}, "inwardIssue": {"key": "ABC-4"}} for other in (outward, relates): with self.subTest(other=other): self.assertEqual(jira.blocked_by_links({"issuelinks": [other, BLOCKED]}), (Link("ABC-2", "9"),)) def test_an_entry_without_an_issue_key_is_a_failure(self): for wrong in ([], {}, {"key": 7}, {"key": "abc"}): with self.subTest(wrong=wrong): self.assertEqual(entry_of(wrong, "plan"), Failure("the search answered an issue without an issue key")) def test_an_entry_in_another_shape_is_a_failure_naming_the_issue_and_the_field(self): cases = { "its fields are no object": {"key": "ABC-1"}, "its properties are no object": raw(properties=[]), "its issuelinks are no list": {"key": "ABC-1", "fields": {}}, "its issuelinks hold a link that is no object": raw("link"), "its issuelinks hold a link whose type names no inward text": raw(BLOCKED | {"type": None}), "its issuelinks hold a link whose id or whose linked issue's key is no text": raw(BLOCKED | {"inwardIssue": {"key": 7}}), } for how, wrong in cases.items(): with self.subTest(how=how): self.assertEqual(entry_of(wrong, "plan"), Failure(SHAPE + how)) self.assertEqual(entry_of(raw(BLOCKED | {"id": 9}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text")) self.assertEqual(entry_of(raw(BLOCKED | {"inwardIssue": None}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text")) def test_a_fault_in_reading_the_links_is_not_reported_as_jiras(self): class Faulty(dict): def get(self, key, default=None): raise TypeError("ours") with self.assertRaisesRegex(TypeError, "ours"): entry_of({"key": "ABC-1", "fields": Faulty()}, "plan") class TheCategoryOfAnIssuesStatus(unittest.TestCase): def test_is_the_key_jira_gives_the_category(self): self.assertEqual(jira.status_category({"fields": {"status": {"statusCategory": {"key": "done"}}}}), "done") def test_an_answer_in_another_shape_is_a_failure_naming_the_field(self): for wrong in (["ABC-1"], {}, {"fields": []}, {"fields": {}}, {"fields": {"status": "Done"}}, {"fields": {"status": {}}}, {"fields": {"status": {"statusCategory": "done"}}}, {"fields": {"status": {"statusCategory": {}}}}, {"fields": {"status": {"statusCategory": {"key": 3}}}}): with self.subTest(wrong=wrong): self.assertEqual(jira.status_category(wrong), Failure("its status category has no key that is text")) class TheRefusalOfAnIssueType(unittest.TestCase): TYPES = {"issueTypes": [{"name": "Feature", "subtask": False, "hierarchyLevel": 0}, {"name": "Bug", "subtask": False, "hierarchyLevel": 0}, {"name": "Epic", "subtask": False, "hierarchyLevel": 1}, {"name": "Subtask", "subtask": True, "hierarchyLevel": -1}]} def refusal(self, kind: str, answer: object) -> str | None: asked: list[tuple[str, str, object]] = [] def http(method: str, url: str, authorization: str, body: object) -> object: asked.append((method, url, body)) return answer refused = jira.issue_type_refusal("https://acme.atlassian.net", "ABC", kind, "jira-finding-type", "Basic x", http) self.assertEqual(asked, [("GET", "https://acme.atlassian.net/rest/api/3/issue/createmeta/ABC/issuetypes", None)]) return refused def test_is_none_where_the_project_has_the_type(self): self.assertIsNone(self.refusal("Bug", self.TYPES)) def test_names_the_types_the_project_has_and_the_setting_where_it_lacks_the_type(self): self.assertEqual(self.refusal("Task", self.TYPES), "ABC has no issue type 'Task' for an issue under an epic; it has Feature and Bug: name one in `jira-finding-type`") def test_a_sub_task_type_and_the_epic_type_are_not_ones_for_an_issue_under_an_epic(self): for kind in ("Subtask", "Epic"): with self.subTest(kind=kind): self.assertEqual(self.refusal(kind, self.TYPES), f"ABC has no issue type {kind!r} for an issue under an epic; it has Feature and Bug: name one in `jira-finding-type`") def test_says_the_project_has_no_other_type_where_it_has_only_sub_task_types(self): self.assertEqual(self.refusal("Bug", {"issueTypes": [{"name": "Subtask", "subtask": True}]}), "ABC has no issue type 'Bug' for an issue under an epic; it has no other type for one") def test_says_why_where_jira_fails(self): self.assertEqual(self.refusal("Bug", Failure("Jira answered 403")), "cannot read ABC's issue types: Jira answered 403") def test_says_so_where_jira_answers_in_another_shape(self): for wrong in ([], {}, {"issueTypes": "Bug"}, {"issueTypes": [{"subtask": False}]}): with self.subTest(wrong=wrong): self.assertEqual(self.refusal("Bug", wrong), "Jira answered ABC's issue types in a shape its REST API version 3 doesn't document") if __name__ == "__main__": unittest.main()