Skip to content

Commit

Permalink
Use AddEntryError inside Encyclopaedia.add_entry()
Browse files Browse the repository at this point in the history
  • Loading branch information
jsfehler committed Oct 14, 2024
1 parent 96caa16 commit 56f40a9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
13 changes: 9 additions & 4 deletions encyclopaedia/book/book_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,15 @@ def add_entry(self, entry: 'EncEntry') -> bool:
Raises:
AddEntryError
"""
if entry.parent is not None and entry.parent != self:
raise AddEntryError(
f"<{entry}> is already a page of {self.parent}",
)
if entry.parent is not None:
if entry.parent == self:
raise AddEntryError(
f"<{entry}> is already a page of <{self}>",
)
else:
raise AddEntryError(
f"<{entry}> already has a parent: <{entry.parent}>",
)

if entry.number is None:
raise AddEntryError(f"<{entry} does not have a number.")
Expand Down
7 changes: 4 additions & 3 deletions encyclopaedia/encyclopaedia_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ToggleShowLockedEntryAction,
)
from .entry_sorting_ren import push_locked_to_bottom
from .exceptions_ren import AddEntryError
from .eventemitter_ren import EventEmitter
from .constants_ren import Direction, SortMode
from .book import Book
Expand Down Expand Up @@ -253,14 +254,14 @@ def add_entry(self, entry: ENTRY_TYPE) -> None:
entry: The Entry to add to the Encyclopaedia
"""
if entry.parent is not None and entry.parent != self:
raise ValueError(
f"{entry} is already inside another Encyclopaedia",
raise AddEntryError(
f"<{entry}> already has a parent: <{entry.parent}>",
)

# When a new entry has a number, ensure it's not already used.
if entry.number is not None:
if any(i for i in self.all_entries if i.number == entry.number):
raise ValueError(f"{entry.number} is already taken.")
raise AddEntryError(f"{entry.number} is already taken.")

elif entry.number is None:
entry.number = self._find_closest_free_number()
Expand Down
2 changes: 1 addition & 1 deletion encyclopaedia/exceptions_ren.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"""

class AddEntryError(Exception):
"""Raised when adding an EncEntry to a parent fails."""
"""Raised when adding an entry to a parent fails."""
pass
6 changes: 3 additions & 3 deletions tests/test_encyclopaedia.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from encyclopaedia import Encyclopaedia
from encyclopaedia import EncEntry
from encyclopaedia import constants_ren

from encyclopaedia import exceptions_ren

def test_setting_entry_number(add_dummy_entries):
"""
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_duplicate_entry_numbers():
locked=False,
)

with pytest.raises(ValueError) as e:
with pytest.raises(exceptions_ren.AddEntryError) as e:
EncEntry(
parent=enc,
number=1,
Expand Down Expand Up @@ -444,7 +444,7 @@ def test_add_entry_already_in_page():
text=["Test Text"],
)

with pytest.raises(ValueError):
with pytest.raises(exceptions_ren.AddEntryError):
enc.add_entry(ee)


Expand Down

0 comments on commit 56f40a9

Please sign in to comment.