diff --git a/encyclopaedia/encyclopaedia_ren.py b/encyclopaedia/encyclopaedia_ren.py index 3059bd4..d17b505 100644 --- a/encyclopaedia/encyclopaedia_ren.py +++ b/encyclopaedia/encyclopaedia_ren.py @@ -328,14 +328,19 @@ def _change_entry(self, direction: Direction) -> bool: # Update the active entry. self.active = self.current_entry - if self.active.locked is False and not isinstance(self.active, Book): - # Run the callback, if provided. - self.active.emit("viewed") - - # Mark the entry as viewed. + if self.active.locked is False: if not isinstance(self.active, Book): + # Run the callback, if provided. + self.active.emit("viewed") + + # Mark the entry as viewed. self.active.viewed = True + # When setting a Book, set the first page to viewed, not the Book. + elif isinstance(self.active, Book): + self.active.active.viewed = True + self.active.active.emit("viewed") + # When changing an entry, the current entry page number is reset. self.active._unlocked_page_index = 0 diff --git a/tests/encyclopaedia/test_change_entry.py b/tests/encyclopaedia/test_change_entry.py new file mode 100644 index 0000000..eb96343 --- /dev/null +++ b/tests/encyclopaedia/test_change_entry.py @@ -0,0 +1,74 @@ +from encyclopaedia import Encyclopaedia +from encyclopaedia import EncEntry +from encyclopaedia import constants_ren +from encyclopaedia.book import Book + + +def test_change_entry_boundary_forward(): + enc = Encyclopaedia() + + EncEntry( + parent=enc, + name="Test Name", + text=["Test Text"], + ) + + EncEntry( + parent=enc, + name="Test Name 2", + text=["Test Text"], + ) + + result = enc._change_entry(constants_ren.Direction.FORWARD) + assert result + + result = enc._change_entry(constants_ren.Direction.FORWARD) + assert not result + + +def test_change_entry_boundary_back(): + enc = Encyclopaedia() + + EncEntry( + parent=enc, + name="Test Name", + text=["Test Text"], + ) + + result = enc._change_entry(constants_ren.Direction.BACKWARD) + assert not result + +def test_change_entry_book(): + """ + When I change entry to a Book + Then only the first page is marked as viewed + """ + enc = Encyclopaedia() + + EncEntry( + parent=enc, + name="Test Name", + text=["Test Text"], + ) + + book = Book(parent=enc, title="Greek Gods", subject="Mythology") + page_0 = EncEntry( + parent=book, + number=0, + name="Zeus", + text="", + ) + page_1 = EncEntry( + parent=book, + number=1, + name="Hades", + text="", + ) + + assert page_0.viewed is False + + enc._change_entry(constants_ren.Direction.FORWARD) + + assert page_0.viewed is True + assert page_1.viewed is False + assert book.viewed is False diff --git a/tests/test_encyclopaedia.py b/tests/encyclopaedia/test_encyclopaedia.py similarity index 89% rename from tests/test_encyclopaedia.py rename to tests/encyclopaedia/test_encyclopaedia.py index 79f4e3c..93b5fdc 100644 --- a/tests/test_encyclopaedia.py +++ b/tests/encyclopaedia/test_encyclopaedia.py @@ -448,41 +448,6 @@ def test_add_entry_already_in_page(): enc.add_entry(ee) -def test_change_entry_boundary_forward(): - enc = Encyclopaedia() - - EncEntry( - parent=enc, - name="Test Name", - text=["Test Text"], - ) - - EncEntry( - parent=enc, - name="Test Name 2", - text=["Test Text"], - ) - - result = enc._change_entry(constants_ren.Direction.FORWARD) - assert result - - result = enc._change_entry(constants_ren.Direction.FORWARD) - assert not result - - -def test_change_entry_boundary_back(): - enc = Encyclopaedia() - - EncEntry( - parent=enc, - name="Test Name", - text=["Test Text"], - ) - - result = enc._change_entry(constants_ren.Direction.BACKWARD) - assert not result - - def test_repr(): enc = Encyclopaedia(name="Dummy Enc Name")