Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IceBotYT committed Aug 19, 2024
1 parent 8b0c583 commit f7b5297
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/nice_go/nice_go_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ def _dispatch(self, event: str, data: dict[str, Any] | None = None) -> None:
"""
method = f"on_{event}"

try:
coros = self._events.get(method, [])
except AttributeError:
pass
else:
_LOGGER.debug("Dispatching event %s", event)
for coro in coros:
self._schedule_event(coro, method, data)
coros = self._events.get(method, [])

if not coros:
_LOGGER.debug("No listeners for event %s", event)
return

_LOGGER.debug("Dispatching event %s", event)
for coro in coros:
self._schedule_event(coro, method, data)

async def authenticate_refresh(
self,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_nice_go_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async def test_schedule_event(mock_api: NiceGOApi) -> None:

async def test_dispatch_event(mock_api: NiceGOApi) -> None:
coro = AsyncMock()
mock_api.on_test_event = coro # type: ignore[attr-defined]
mock_api._events["on_test_event"] = [coro]
mock_api._dispatch("test_event", {"key": "value"})
await asyncio.sleep(0) # Allow the event loop to run
coro.assert_called_once()
Expand Down Expand Up @@ -172,8 +172,8 @@ async def test_event_decorator(mock_api: NiceGOApi) -> None:
async def on_test_event(data: dict[str, Any]) -> None:
pass

assert "on_test_event" in dir(mock_api)
assert mock_api.on_test_event == on_test_event # type: ignore[attr-defined]
assert "on_test_event" in mock_api._events
assert mock_api._events["on_test_event"][0] == on_test_event
mock_api._dispatch("test_event", {})


Expand All @@ -191,7 +191,7 @@ def on_test_event(data: dict[str, Any]) -> None:
async def test_run_event_errors(mock_api: NiceGOApi, error: Exception) -> None:
coro = AsyncMock()
coro.side_effect = error
mock_api.on_test_event = coro # type: ignore[attr-defined]
mock_api._events["on_test_event"] = [coro]
mock_api._dispatch("test_event", {})
await asyncio.sleep(0) # Allow the event loop to run
coro.assert_called_once()
Expand Down

0 comments on commit f7b5297

Please sign in to comment.