Skip to content

Commit

Permalink
Add is_single_stage to ChapterDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaasonen committed Dec 15, 2021
1 parent 243631f commit 8cdb728
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ebl/corpus/application/display_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def make_line(self, data: dict, **kwargs) -> LineDisplay:
class ChapterDisplaySchema(Schema):
id_ = fields.Nested(ChapterIdSchema, required=True, data_key="id")
text_name = fields.String(required=True, data_key="textName")
is_single_stage = fields.Boolean(required=True, data_key="isSingleStage")
title = fields.List(fields.Nested(OneOfNoteLinePartSchema), dump_only=True)
lines = fields.Nested(LineDisplaySchema, many=True, required=True)

Expand All @@ -40,5 +41,6 @@ def make_chapter(self, data: dict, **kwargs) -> ChapterDisplay:
return ChapterDisplay(
data["id_"],
data["text_name"],
data["is_single_stage"],
tuple(data["lines"]),
)
2 changes: 2 additions & 0 deletions ebl/corpus/domain/chapter_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def of_line(line: Line) -> "LineDisplay":
class ChapterDisplay:
id_: ChapterId
text_name: str
is_single_stage: bool
lines: Sequence[LineDisplay]

@property
Expand All @@ -62,5 +63,6 @@ def of_chapter(text: Text, chapter: Chapter) -> "ChapterDisplay":
return ChapterDisplay(
chapter.id_,
text.name,
not text.has_multiple_stages,
tuple(map(LineDisplay.of_line, chapter.lines)),
)
4 changes: 4 additions & 0 deletions ebl/corpus/domain/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ class Text:
@property
def id(self) -> TextId:
return TextId(self.genre, self.category, self.index)

@property
def has_multiple_stages(self) -> bool:
return len({chapter.stage for chapter in self.chapters}) > 1
6 changes: 5 additions & 1 deletion ebl/corpus/infrastructure/mongo_text_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ def find_chapter_for_display(self, id_: ChapterId) -> ChapterDisplay:
text = self.find(id_.text_id)
chapters = self._chapters.aggregate(aggregate_chapter_display(id_))
return ChapterDisplaySchema().load(
{**next(chapters), "textName": text.name}
{
**next(chapters),
"textName": text.name,
"isSingleStage": not text.has_multiple_stages,
}
)
except NotFoundError as error:
raise text_not_found(id_.text_id) from error
Expand Down
1 change: 1 addition & 0 deletions ebl/tests/corpus/test_chapter_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def test_chapter_display_of_chapter() -> None:
assert chapter_display == ChapterDisplay(
chapter.id_,
text.name,
not text.has_multiple_stages,
tuple(LineDisplay.of_line(line) for line in chapter.lines),
)
assert chapter_display.title == make_title(chapter.lines[0].translation)
2 changes: 2 additions & 0 deletions ebl/tests/corpus/test_chapter_display_schema
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def to_dict(chapter: ChapterDisplay) -> dict:
"index": chapter.id_.text_id.index,
},
},
"textName": chapter.text_name,
"isSingleStage": chapter.is_single_stage,
"lines": [
{
"number": OneOfLineNumberSchema().dump(line.number),
Expand Down
23 changes: 23 additions & 0 deletions ebl/tests/corpus/test_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from ebl.corpus.domain.stage import Stage
from ebl.tests.factories.corpus import ChapterListingFactory, TextFactory


@pytest.mark.parametrize( # pyre-ignore[56]
"chapters,expected",
[
(tuple(), False),
(ChapterListingFactory.build_batch(2, stage=Stage.NEO_ASSYRIAN), False),
(
[
ChapterListingFactory.build(stage=Stage.NEO_ASSYRIAN),
ChapterListingFactory.build(stage=Stage.OLD_ASSYRIAN),
],
True,
),
],
)
def test_has_multiple_stages(chapters, expected) -> None:
text = TextFactory.build(chapters=chapters)
assert text.has_multiple_stages == expected

0 comments on commit 8cdb728

Please sign in to comment.