refactor: integrate get_latest_haiku_or_default into load_haiku

Refs: OPS-65
This commit is contained in:
Christoph J. Scherr 2025-03-23 14:14:27 +01:00
parent cf48ac8164
commit 8f3ccb94d1
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB
2 changed files with 7 additions and 13 deletions

View file

@ -35,10 +35,12 @@ class StoreManager:
def _save(self, data: dict) -> int: def _save(self, data: dict) -> int:
return self._db.insert(data) return self._db.insert(data)
def load_haiku(self, key: int) -> Optional[Haiku]: def load_haiku(self, key: Optional[int]) -> Haiku:
if key is None:
return DEFAULT_HAIKU
raw_haiku: dict | None = self._load(key) raw_haiku: dict | None = self._load(key)
if raw_haiku is None: if raw_haiku is None:
return None return DEFAULT_HAIKU
h = Haiku(**raw_haiku) h = Haiku(**raw_haiku)
return h return h
@ -52,12 +54,3 @@ class StoreManager:
except IndexError as e: except IndexError as e:
self.logger.error(f"The database seems to be empty: {e}") self.logger.error(f"The database seems to be empty: {e}")
return None return None
def get_latest_haiku_or_default(self) -> Haiku:
id = self.get_id_of_latest_haiku()
if id is None:
return DEFAULT_HAIKU
haiku = self.load_haiku(id)
if haiku is None:
return DEFAULT_HAIKU
return haiku

View file

@ -37,6 +37,7 @@ def test_save_and_load_haiku(store_manager: StoreManager):
but should have" but should have"
assert h == h_loaded assert h == h_loaded
assert h != DEFAULT_HAIKU
def test_load_latest_with_empty_store(temp_data_dir): def test_load_latest_with_empty_store(temp_data_dir):
@ -47,7 +48,7 @@ def test_load_latest_with_empty_store(temp_data_dir):
def test_load_latest_or_default_with_empty(temp_data_dir): def test_load_latest_or_default_with_empty(temp_data_dir):
store = StoreManager(temp_data_dir / "load_or_default_empty.json") store = StoreManager(temp_data_dir / "load_or_default_empty.json")
haiku = store.get_latest_haiku_or_default() haiku = store.load_haiku(store.get_id_of_latest_haiku())
assert haiku == DEFAULT_HAIKU assert haiku == DEFAULT_HAIKU
@ -55,6 +56,6 @@ def test_load_latest_or_default_with_non_empty(temp_data_dir):
store = StoreManager(temp_data_dir / "load_or_default_not_empty.json") store = StoreManager(temp_data_dir / "load_or_default_not_empty.json")
nonsense_test_haiku = Haiku(["nonsense", "test", "haiku"]) nonsense_test_haiku = Haiku(["nonsense", "test", "haiku"])
store.save_haiku(nonsense_test_haiku) store.save_haiku(nonsense_test_haiku)
haiku = store.get_latest_haiku_or_default() haiku = store.load_haiku(store.get_id_of_latest_haiku())
assert haiku != DEFAULT_HAIKU assert haiku != DEFAULT_HAIKU
assert haiku == nonsense_test_haiku assert haiku == nonsense_test_haiku