From 209a61f5ed71ae6ef031905ffc0f3bddc35fde22 Mon Sep 17 00:00:00 2001 From: Joshua Fehler Date: Sun, 20 Oct 2024 22:15:35 -0400 Subject: [PATCH] fix: Set viewed status of first page in Book when Book is set --- encyclopaedia/actions_ren.py | 18 +++++++---- tests/actions/test_actions.py | 40 ----------------------- tests/actions/test_set_entry.py | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 46 deletions(-) create mode 100644 tests/actions/test_set_entry.py diff --git a/encyclopaedia/actions_ren.py b/encyclopaedia/actions_ren.py index ee87ef9..d9581a4 100644 --- a/encyclopaedia/actions_ren.py +++ b/encyclopaedia/actions_ren.py @@ -59,12 +59,18 @@ def set_entry(self) -> None: # The active entry is set to whichever list position was found. self.enc.active = self.entry - if self.enc.active.locked is False and not isinstance(self.entry, Book): - if self.entry.viewed is False: - # Run the callback, if provided. - self.entry.emit("viewed") - # Mark the entry as viewed. - self.enc.active.viewed = True + if self.enc.active.locked is False: + if not isinstance(self.entry, Book): + if self.entry.viewed is False: + # Run the callback, if provided. + self.entry.emit("viewed") + # Mark the entry as viewed. + self.enc.active.viewed = True + + # When setting a Book, set the first page to viewed, not the Book. + elif isinstance(self.entry, Book): + self.entry.active.viewed = True + self.entry.active.emit("viewed") # When sorting by Unread, setting an entry marks is as read. # Thus we have to resort the entries to ensure they appear in the diff --git a/tests/actions/test_actions.py b/tests/actions/test_actions.py index 8a3cdbd..2f740e4 100644 --- a/tests/actions/test_actions.py +++ b/tests/actions/test_actions.py @@ -132,46 +132,6 @@ def test_reset_sub_page(): assert e.current_page == e -def test_set_entry(add_dummy_entries): - """Test Actions through their implementation in Encyclopaedia.""" - - enc = Encyclopaedia() - - entries = add_dummy_entries(enc, 5) - - add_dummy_entries(enc, 5, locked=True) - - e = entries[-1] - - # Use the last unlocked Entry created for the test. - enc.SetEntry(e)() - - assert e == enc.active - assert 4 == enc.current_position - - -def test_set_entry_sorting_mode_unread(add_dummy_entries): - """ - When the sorting mode is by unread entries, - Then the Encyclopaedia should resort when an entry is set - And the newly read entry is in the correct position - """ - - enc = Encyclopaedia(sorting_mode=4) - - entries = add_dummy_entries(enc, 5) - - e = entries[1] - - assert e == enc.current_entries[1] - - enc.SetEntry(e)() - - assert e == enc.active - - # entry should be moved from 2nd position to the last - assert e == enc.current_entries[-1] - def test_filter_by_subject(): """Test Actions through their implementation in Encyclopaedia.""" diff --git a/tests/actions/test_set_entry.py b/tests/actions/test_set_entry.py new file mode 100644 index 0000000..42378d2 --- /dev/null +++ b/tests/actions/test_set_entry.py @@ -0,0 +1,57 @@ +from encyclopaedia import Encyclopaedia +from encyclopaedia import EncEntry +from encyclopaedia.book import Book + + +def test_set_entry(add_dummy_entries): + """Test Actions through their implementation in Encyclopaedia.""" + + enc = Encyclopaedia() + + entries = add_dummy_entries(enc, 5) + + add_dummy_entries(enc, 5, locked=True) + + e = entries[-1] + + # Use the last unlocked Entry created for the test. + enc.SetEntry(e)() + + assert e == enc.active + assert 4 == enc.current_position + + +def test_set_entry_sorting_mode_unread(add_dummy_entries): + """ + When the sorting mode is by unread entries, + Then the Encyclopaedia should resort when an entry is set + And the newly read entry is in the correct position + """ + + enc = Encyclopaedia(sorting_mode=4) + + entries = add_dummy_entries(enc, 5) + + e = entries[1] + + assert e == enc.current_entries[1] + + enc.SetEntry(e)() + + assert e == enc.active + + # entry should be moved from 2nd position to the last + assert e == enc.current_entries[-1] + + +def test_set_entry_book_viewed(): + enc = Encyclopaedia() + + book = Book(parent=enc, title="Greek Gods", subject="Mythology") + EncEntry(parent=book, number=0, name="Zeus") + EncEntry(parent=book, number=1, name="Hades") + + enc.SetEntry(book)() + + assert book.viewed is False + assert book.active.viewed is True