Files
claude-config/bin/do_epic/backend/contract.py
T

89 lines
4.5 KiB
Python

"""What every backend of an epic's plan must hold, as tests. A backend's test case inherits EpicStoreContract and unittest.TestCase, and builds an epic with no issues in new_store. Its assign and close change an issue as a person would in the tracker."""
import re
import typing
from do_epic.backend.protocol import EpicIssue, EpicStore
from failure import Failure
class EpicStoreContract:
assertEqual: typing.Callable[..., None]
assertIsInstance: typing.Callable[..., None]
subTest: typing.Callable[..., typing.ContextManager[None]]
def new_store(self) -> EpicStore:
raise NotImplementedError
def assign(self, store: EpicStore, key: str, name: str) -> None:
"""Assigns the issue named key to the person whose display name is name."""
raise NotImplementedError
def close(self, store: EpicStore, key: str, status: str) -> None:
"""Moves the issue named key to the done status named status."""
raise NotImplementedError
def filed(self, store: EpicStore, parent: str, summary: str, detail: str = "", labels: tuple[str, ...] = ()) -> str:
key = store.file(parent, summary, detail, labels)
assert isinstance(key, str), key
return key
def issues(self, store: EpicStore) -> list[EpicIssue]:
read = store.read()
assert isinstance(read, list), read
return read
def test_an_epic_with_no_issues_reads_empty(self) -> None:
self.assertEqual(self.new_store().read(), [])
def test_reads_back_a_child_issue_it_filed(self) -> None:
store = self.new_store()
key = self.filed(store, store.epic_key, "Count the runs.", "Why it counts.\n\nA second paragraph.", ("security", "api"))
self.assertEqual(self.issues(store), [EpicIssue(key, "Count the runs.", "Why it counts.\n\nA second paragraph.", ("api", "security"), None, "To Do", False, store.epic_key)])
def test_reads_back_a_sub_task_it_filed_after_its_parent(self) -> None:
store = self.new_store()
first = self.filed(store, store.epic_key, "Count the runs.")
second = self.filed(store, store.epic_key, "Ship the later thing.")
task = self.filed(store, first, "Count them in the api.", labels=("api",))
read = self.issues(store)
self.assertEqual([(issue.key, issue.parent) for issue in read], [(first, store.epic_key), (task, first), (second, store.epic_key)])
self.assertEqual((read[1].summary, read[1].labels, read[1].detail), ("Count them in the api.", ("api",), ""))
def test_files_each_issue_under_its_own_key_in_the_epics_project(self) -> None:
store = self.new_store()
child = self.filed(store, store.epic_key, "Count the runs.")
keys = [child, self.filed(store, store.epic_key, "Count the runs."), self.filed(store, child, "Count them in the api.")]
self.assertEqual(len(set(keys)), 3)
project = store.epic_key.rpartition("-")[0]
self.assertEqual([re.fullmatch(f"{project}-[1-9][0-9]*", key) is not None for key in keys], [True] * 3)
def test_reads_the_assignee_and_a_done_status(self) -> None:
store = self.new_store()
first = self.filed(store, store.epic_key, "Count the runs.")
second = self.filed(store, store.epic_key, "Ship the later thing.")
self.assign(store, first, "Ada Lovelace")
self.close(store, second, "Done")
read = {issue.key: issue for issue in self.issues(store)}
self.assertEqual((read[first].assignee, read[first].status, read[first].done), ("Ada Lovelace", "To Do", False))
self.assertEqual((read[second].assignee, read[second].status, read[second].done), (None, "Done", True))
def test_refuses_to_file_under_a_sub_task(self) -> None:
store = self.new_store()
child = self.filed(store, store.epic_key, "Count the runs.")
task = self.filed(store, child, "Count them in the api.")
self.assertEqual(store.file(task, "Count more.", "", ()), Failure(f"{task} is neither the epic {store.epic_key} nor one of its child issues, so nothing can be filed under it"))
self.assertEqual(len(self.issues(store)), 2)
def test_refuses_to_file_under_an_issue_outside_the_epic(self) -> None:
store = self.new_store()
for parent in ("ZZZ-1", "not a key"):
with self.subTest(parent=parent):
self.assertIsInstance(store.file(parent, "Count more.", "", ()), Failure)
self.assertEqual(self.issues(store), [])
def test_refuses_a_label_with_white_space_and_a_summary_of_two_lines(self) -> None:
store = self.new_store()
self.assertEqual(store.file(store.epic_key, "Count the runs.", "", ("two words",)), Failure("the label 'two words' is empty or holds white space"))
self.assertEqual(store.file(store.epic_key, "Count\nthe runs.", "", ()), Failure("the summary must be one line of text"))
self.assertEqual(self.issues(store), [])