"""What every plan backend must hold, as tests. A backend's test case inherits PlanStoreContract and unittest.TestCase, and builds its store in new_store. record_store.contract is its twin for records.""" import typing from plan.todo_table import BLOCKED_BY, Item, MOVES from failure import Failure from plan_store.plans import Plan, PlanStore, TAGLINE, Written from plan.release import release_of FIRST = {"Release": "0.1.0", TAGLINE: "Count the runs.", "L": "1", "E": "2", "I": "3", "S": "1", MOVES: "1 by 9"} SECOND = {"Release": "0.2.0", TAGLINE: "Ship the later thing.", "L": "2", "E": "1", "I": "1", "S": "2", MOVES: "1 by 8", "Exempt": "owner"} class PlanStoreContract: """Runs against the store new_store returns: a plan that holds no item yet. The project's goals file must hold a goal numbered 1, which the test items move.""" assertEqual: typing.Callable[..., None] assertIsInstance: typing.Callable[..., None] assertNotEqual: typing.Callable[..., None] assertTrue: typing.Callable[..., None] def new_store(self) -> PlanStore: raise NotImplementedError def filed(self, store: PlanStore, fields: dict[str, str], detail: str = "") -> int: written = store.add(fields, detail) assert isinstance(written, Written), written return written.id def items(self, store: PlanStore) -> dict[int, Item]: plan = store.read() assert isinstance(plan, Plan), plan return {item.id: item for item in plan.items} def test_a_plan_with_no_item_reads_empty(self) -> None: self.assertEqual(self.new_store().read(), Plan([])) def test_reads_back_every_field_of_an_item_it_filed(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST, "Why it counts.\n\nA second paragraph, with a | pipe.") item = self.items(store)[id_] self.assertEqual((item.release, item.exempt, item.tagline, item.moves, item.blocked_by), (release_of("0.1.0"), "", "**Count the runs.**", (("1", 9),), ())) self.assertEqual({name: item.scores[name] for name in "LEIS"}, {"L": 1, "E": 2, "I": 3, "S": 1}) self.assertEqual(item.detail, "Why it counts.\n\nA second paragraph, with a | pipe.") def test_files_each_item_under_its_own_id(self) -> None: store = self.new_store() first = self.filed(store, FIRST) second = self.filed(store, SECOND | {BLOCKED_BY: str(first)}) self.assertNotEqual(first, second) held = self.items(store) self.assertEqual(set(held), {first, second}) self.assertEqual((held[second].exempt, held[second].blocked_by, held[second].detail), ("owner", (first,), "")) def test_an_edit_sets_the_fields_given_and_leaves_the_rest(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST, "Why it counts.") self.assertEqual(store.edit(id_, {"L": "2", TAGLINE: "Count every run."}, None), Written(id_)) item = self.items(store)[id_] self.assertEqual((item.scores["L"], item.scores["E"], item.tagline, item.release, item.detail), (2, 2, "**Count every run.**", release_of("0.1.0"), "Why it counts.")) def test_an_edit_replaces_the_detail_and_an_empty_one_removes_it(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST, "Why it counts.") store.edit(id_, {}, "A new reason.") self.assertEqual(self.items(store)[id_].detail, "A new reason.") store.edit(id_, {}, "") self.assertEqual(self.items(store)[id_].detail, "") def test_an_edit_of_the_release_moves_the_item(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST) store.edit(id_, {"Release": "0.3.0"}, None) self.assertEqual(self.items(store)[id_].release, release_of("0.3.0")) self.assertEqual(self.items(store)[id_].detail, "") def test_an_edit_with_a_comment_sets_the_fields_as_one_without(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST, "Why it counts.") self.assertEqual(store.edit(id_, {"L": "2"}, None, "A small change to one module."), Written(id_)) item = self.items(store)[id_] self.assertEqual((item.scores["L"], item.detail), (2, "Why it counts.")) def test_finish_removes_the_item_and_keeps_the_others(self) -> None: store = self.new_store() first, second = self.filed(store, FIRST, "Why it counts."), self.filed(store, SECOND, "Later detail.") self.assertEqual(store.finish(first), Written(first)) held = self.items(store) self.assertEqual({id_: item.detail for id_, item in held.items()}, {second: "Later detail."}) def test_an_id_it_holds_no_item_for_fails_and_changes_nothing(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST, "Why it counts.") before = store.read() self.assertIsInstance(store.edit(id_ + 1, {"L": "2"}, None), Failure) self.assertIsInstance(store.finish(id_ + 1), Failure) self.assertEqual(store.read(), before) def test_a_field_out_of_format_fails_and_changes_nothing(self) -> None: store = self.new_store() id_ = self.filed(store, FIRST) before = store.read() self.assertIsInstance(store.add(FIRST | {"L": "9"}, ""), Failure) self.assertIsInstance(store.edit(id_, {"Release": "soon"}, None), Failure) self.assertEqual(store.read(), before) def test_names_itself_for_a_message(self) -> None: self.assertTrue(self.new_store().label.strip())