Skip to content

Commit

Permalink
fix: Setting viewed status of Book when changing entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Oct 21, 2024
1 parent 8e38235 commit 1364bfa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 40 deletions.
15 changes: 10 additions & 5 deletions encyclopaedia/encyclopaedia_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
74 changes: 74 additions & 0 deletions tests/encyclopaedia/test_change_entry.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 1364bfa

Please sign in to comment.