Skip to content

Commit

Permalink
Merge pull request #135 from elliot-100/113-raise-keyerror-for-failed…
Browse files Browse the repository at this point in the history
…-lookup-by-id

Changed: raise `KeyError` instead of `IndexError` for all failed lookup methods
  • Loading branch information
elliot-100 authored Jun 26, 2024
2 parents d9c6869 + 5263e15 commit 76a63ca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
20 changes: 12 additions & 8 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,16 @@ async def get_group(self, uid: str) -> dict:
Raises
------
IndexError if no group is matched.
KeyError if no group is matched.
"""

if not self.groups:
await self.get_groups()
for group in self.groups:
if group["id"] == uid:
return group
errmsg = f"No group with id='{uid}'"
raise IndexError(errmsg)
errmsg = f"No group with id='{uid}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def get_person(self, user: str) -> dict:
Expand All @@ -88,6 +87,10 @@ async def get_person(self, user: str) -> dict:
Returns
-------
Member or guardian's details.
Raises
------
KeyError if no person is matched.
"""
if not self.groups:
await self.get_groups()
Expand All @@ -113,7 +116,8 @@ async def get_person(self, user: str) -> dict:
)
):
return guardian
raise IndexError
errmsg = f"No person matched with identifier '{user}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def get_messages(self) -> list[dict]:
Expand Down Expand Up @@ -299,16 +303,16 @@ async def get_event(self, uid: str) -> dict:
Raises
------
IndexError if no event is matched.
KeyError if no event is matched.
"""
if not self.events:
await self.get_events()
for event in self.events:
if event["id"] == uid:
return event
errmsg = f"No event with id='{uid}'"
raise IndexError(errmsg)
errmsg = f"No event with id='{uid}'."
raise KeyError(errmsg)

@_SpondBase.require_authentication
async def update_event(self, uid: str, updates: dict):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ async def test_get_event__happy_path(mock_events, mock_token):

@pytest.mark.asyncio
async def test_get_event__no_match_raises_exception(mock_events, mock_token):
"""Test that a non-matched `id` raises IndexError."""
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_event("ID3")


@pytest.mark.asyncio
async def test_get_event__blank_id_match_raises_exception(mock_events, mock_token):
"""Test that a blank `id` raises IndexError."""
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_event("")


Expand All @@ -119,25 +119,25 @@ async def test_get_group__happy_path(mock_groups, mock_token):

@pytest.mark.asyncio
async def test_get_group__no_match_raises_exception(mock_groups, mock_token):
"""Test that a non-matched `id` raises IndexError."""
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_group("ID3")


@pytest.mark.asyncio
async def test_get_group__blank_id_raises_exception(mock_groups, mock_token):
"""Test that a blank `id` raises IndexError."""
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(IndexError):
with pytest.raises(KeyError):
await s.get_group("")


Expand Down

0 comments on commit 76a63ca

Please sign in to comment.