From df42529284677fdcad1f7b93f9d84162febdfec7 Mon Sep 17 00:00:00 2001 From: G L Date: Sun, 28 Jul 2024 19:01:13 +0200 Subject: [PATCH] Remove content str from digests db --- echopages/application/services.py | 15 +++++---- echopages/domain/model.py | 15 +-------- echopages/infrastructure/database/file_db.py | 11 ++----- tests/integration/test_file_services.py | 10 +++--- tests/unit/test_services.py | 34 ++++++++++---------- 5 files changed, 35 insertions(+), 50 deletions(-) diff --git a/echopages/application/services.py b/echopages/application/services.py index 5dbb46c..44fdd0e 100644 --- a/echopages/application/services.py +++ b/echopages/application/services.py @@ -63,12 +63,13 @@ def deliver_digest( digest_delivery_system: model.DigestDeliverySystem, uow: repositories.UnitOfWork, digest_id: int, + digest_repr: model.DigestRepr, ) -> None: with uow: digest = uow.digest_repo.get_by_id(digest_id) assert digest is not None - digest_delivery_system.deliver_digest(digest.digest_repr) + digest_delivery_system.deliver_digest(digest_repr) digest.mark_as_sent() uow.digest_repo.update(digest) uow.commit() @@ -90,11 +91,8 @@ def format_digest( contents.append(content) digest_repr = digest_formatter.format(contents) - digest.store_repr(digest_repr) - uow.digest_repo.update(digest) - uow.commit() - return digest_repr.title, digest_repr.contents_str + return digest_repr.title, digest_repr.contents_str def delivery_service( @@ -110,6 +108,11 @@ def delivery_service( digest_title, digest_content_str = format_digest( uow, digest_formatter, digest_id ) - deliver_digest(digest_delivery_system, uow, digest_id) + deliver_digest( + digest_delivery_system, + uow, + digest_id, + model.DigestRepr(digest_title, digest_content_str), + ) return digest_title, digest_content_str diff --git a/echopages/domain/model.py b/echopages/domain/model.py index 378d0b7..b411e4e 100644 --- a/echopages/domain/model.py +++ b/echopages/domain/model.py @@ -1,6 +1,6 @@ import abc from dataclasses import dataclass -from typing import Any, Callable, Dict, List, NewType, Optional +from typing import Callable, List, NewType, Optional class Content: @@ -35,27 +35,14 @@ def __init__( id: Optional[int] = None, content_ids: List[int] = [], sent: bool = False, - digest_repr: DigestRepr = DigestRepr(DigestTitle(""), DigestContentStr("")), ) -> None: self.id = id self.content_ids = content_ids self.sent = sent - self.digest_repr = digest_repr def mark_as_sent(self) -> None: self.sent = True - def store_repr(self, digest_repr: DigestRepr) -> None: - self.digest_repr = digest_repr - - def to_dict(self) -> Dict[str, Any]: - return { - "id": self.id, - "content_ids": self.content_ids, - "sent": self.sent, - "digest_repr": self.digest_repr.__dict__, - } - class ContentSampler(abc.ABC): @abc.abstractmethod diff --git a/echopages/infrastructure/database/file_db.py b/echopages/infrastructure/database/file_db.py index 9cbcbde..d94f6a7 100644 --- a/echopages/infrastructure/database/file_db.py +++ b/echopages/infrastructure/database/file_db.py @@ -5,7 +5,7 @@ from typing import List, Optional from echopages.domain import repositories -from echopages.domain.model import Content, Digest, DigestRepr +from echopages.domain.model import Content, Digest class FileContentRepository(repositories.ContentRepository): @@ -58,16 +58,11 @@ def __init__(self, filepath: str): def _load(self) -> None: with open(self.filepath, "r") as file: - self.digests = [] - for digest_dict in json.load(file): - digest_dict["digest_repr"] = DigestRepr( - **digest_dict.get("digest_repr", {}) - ) - self.digests.append(Digest(**digest_dict)) + self.digests = [Digest(**digest_dict) for digest_dict in json.load(file)] def save(self) -> None: with open(self.filepath, "w") as file: - json.dump([digest.to_dict() for digest in self.digests], file, indent=2) + json.dump([digest.__dict__ for digest in self.digests], file, indent=2) def get_by_id(self, digest_id: int) -> Optional[Digest]: self._load() diff --git a/tests/integration/test_file_services.py b/tests/integration/test_file_services.py index b488ac4..f24a5e7 100644 --- a/tests/integration/test_file_services.py +++ b/tests/integration/test_file_services.py @@ -38,7 +38,7 @@ def test_trigger_digest() -> None: # When: A digest with 3 contents is triggered n_samples = 3 - services.delivery_service( + _, digest_content_str = services.delivery_service( uow, content_sampler, n_samples, @@ -50,8 +50,8 @@ def test_trigger_digest() -> None: with uow: digest = uow.digest_repo.get_all()[0] for content in contents: - assert content["source"] in digest.digest_repr.contents_str - assert content["author"] in digest.digest_repr.contents_str - assert content["location"] in digest.digest_repr.contents_str - assert content["text"] in digest.digest_repr.contents_str + assert content["source"] in digest_content_str + assert content["author"] in digest_content_str + assert content["location"] in digest_content_str + assert content["text"] in digest_content_str assert digest.sent diff --git a/tests/unit/test_services.py b/tests/unit/test_services.py index a6a7107..968faed 100644 --- a/tests/unit/test_services.py +++ b/tests/unit/test_services.py @@ -127,7 +127,7 @@ def test_deliver_digest() -> None: delivery_system = FakeDigestDeliverySystem() digest = model.Digest(id=1, content_ids=[content.id for content in content_objects]) # type: ignore - digest.digest_repr = model.DigestRepr( + digest_repr = model.DigestRepr( title=model.DigestTitle("source 1"), contents_str=model.DigestContentStr( "content unit 1,content unit 2,content unit 3" @@ -135,7 +135,7 @@ def test_deliver_digest() -> None: ) uow.digest_repo.add(digest) - services.deliver_digest(delivery_system, uow, 1) + services.deliver_digest(delivery_system, uow, 1, digest_repr) assert delivery_system.sent_contents == [ ("source 1", "content unit 1,content unit 2,content unit 3") @@ -158,13 +158,13 @@ def test_format_digest_1_content() -> None: digest = model.Digest(id=1, content_ids=[1]) uow.digest_repo.add(digest) - services.format_digest(uow, digest_formatter, 1) + digest_title, digest_content_str = services.format_digest(uow, digest_formatter, 1) assert ( - digest.digest_repr.contents_str + digest_content_str == "{'id': 1, 'text': 'content unit 1', 'source': 'source 1', 'author': 'author 1', 'location': 'location 1'}" ) - assert digest.digest_repr.title == "Daily Digest: source 1" + assert digest_title == "Daily Digest: source 1" def test_format_digest_2_contents() -> None: @@ -183,14 +183,14 @@ def test_format_digest_2_contents() -> None: digest = model.Digest(id=1, content_ids=[1, 2]) uow.digest_repo.add(digest) - services.format_digest(uow, digest_formatter, 1) + digest_title, digest_content_str = services.format_digest(uow, digest_formatter, 1) for idx in range(1, 3): - assert f"source {idx}" in digest.digest_repr.contents_str - assert f"author {idx}" in digest.digest_repr.contents_str - assert f"location {idx}" in digest.digest_repr.contents_str - assert f"content unit {idx}" in digest.digest_repr.contents_str - assert digest.digest_repr.title == "Daily Digest: source 1, source 2" + assert f"source {idx}" in digest_content_str + assert f"author {idx}" in digest_content_str + assert f"location {idx}" in digest_content_str + assert f"content unit {idx}" in digest_content_str + assert digest_title == "Daily Digest: source 1, source 2" def test_trigger_digest() -> None: @@ -207,7 +207,7 @@ def test_trigger_digest() -> None: # When: A digest with 3 contents is triggered n_samples = 3 - services.delivery_service( + _, digest_content_str = services.delivery_service( uow, content_sampler, n_samples, @@ -216,12 +216,12 @@ def test_trigger_digest() -> None: ) # Then: A digest with 3 contents is generated and stored - digest = uow.digest_repo.get_all()[0] for content in contents: - assert content["source"] in digest.digest_repr.contents_str - assert content["author"] in digest.digest_repr.contents_str - assert content["location"] in digest.digest_repr.contents_str - assert content["text"] in digest.digest_repr.contents_str + assert content["source"] in digest_content_str + assert content["author"] in digest_content_str + assert content["location"] in digest_content_str + assert content["text"] in digest_content_str + digest = uow.digest_repo.get_all()[0] assert digest.sent