Skip to content

Commit

Permalink
Remove content str from digests db
Browse files Browse the repository at this point in the history
  • Loading branch information
G L committed Jul 28, 2024
1 parent 01a7c55 commit df42529
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
15 changes: 9 additions & 6 deletions echopages/application/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand All @@ -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
15 changes: 1 addition & 14 deletions echopages/domain/model.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand Down
11 changes: 3 additions & 8 deletions echopages/infrastructure/database/file_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/test_file_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
34 changes: 17 additions & 17 deletions tests/unit/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ 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"
),
)
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")
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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,
Expand All @@ -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


Expand Down

0 comments on commit df42529

Please sign in to comment.