"""What every record backend must hold, as tests. A backend's test case inherits RecordStoreContract and unittest.TestCase, and builds its store in new_store.""" import typing from record_store.held import Held from record_store.stores import RecordStore class RecordStoreContract: """Runs against the store new_store returns: one that holds no record yet.""" assertEqual: typing.Callable[..., None] assertIsNone: typing.Callable[..., None] assertIsInstance: typing.Callable[..., None] assertTrue: typing.Callable[..., None] def new_store(self) -> RecordStore: raise NotImplementedError def test_a_record_never_written_reads_empty(self) -> None: self.assertEqual(self.new_store().read(), "") def test_reads_back_what_it_wrote_to_the_character(self) -> None: store = self.new_store() text = "# Decisions\n\n- Å, 🔴 and a | pipe\n\n\n" self.assertEqual(store.write(text, None), store.held()) self.assertEqual(store.read(), text) def test_a_write_replaces_the_whole_record(self) -> None: store = self.new_store() first = store.write("first\nsecond\n", None) assert isinstance(first, Held), first # A write over the version the last one left replaces the record, as a later save does. self.assertIsInstance(store.write("third\n", first.version), Held) self.assertEqual(store.read(), "third\n") def test_holds_its_text_with_the_version_a_write_over_it_names(self) -> None: store = self.new_store() store.write("text\n", None) held = store.held() assert isinstance(held, Held), held self.assertEqual(held.text, "text\n") def test_names_itself_for_a_message(self) -> None: self.assertTrue(self.new_store().label.strip())