Skip to content

Commit

Permalink
Fix Event class (#173)
Browse files Browse the repository at this point in the history
* Fix Event class

* Add tests
  • Loading branch information
natekspencer authored Aug 29, 2023
1 parent 6dd342b commit 63d4122
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pylitterbot/event.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"""Event handling class for pylitterbot."""
from __future__ import annotations

from abc import ABC
from collections.abc import Callable
from dataclasses import dataclass, field
from typing import Any

EVENT_UPDATE = "update"


class Event(ABC):
@dataclass
class Event:
"""Abstract event class properties and methods."""

_listeners: dict[str, list[Callable]] = {}
_listeners: dict[str, list[Callable]] = field(default_factory=dict)

def emit(self, event_name: str, *args: Any, **kwargs: Any) -> None:
"""Run all callbacks for an event."""
Expand Down
1 change: 1 addition & 0 deletions pylitterbot/robot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Robot(Event):

def __init__(self, data: dict, account: Account) -> None:
"""Initialize a robot."""
super().__init__()
self._data: dict = {}
self._account = account

Expand Down
1 change: 1 addition & 0 deletions pylitterbot/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Session(Event, ABC):

def __init__(self, websession: ClientSession | None = None) -> None:
"""Initialize the session."""
super().__init__()
self._websession_provided = websession is not None
self._websession = websession

Expand Down
9 changes: 7 additions & 2 deletions tests/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Common test module."""
from __future__ import annotations

from collections.abc import Callable
from typing import Any
from unittest.mock import Mock, patch

Expand Down Expand Up @@ -269,15 +270,19 @@
}


async def get_account(logged_in: bool = False, load_robots: bool = False) -> Account:
async def get_account(
logged_in: bool = False,
load_robots: bool = False,
token_update_callback: Callable[[dict | None], None] | None = None,
) -> Account:
"""Get an account that has the underlying API patched."""
with patch(
"pylitterbot.session.ClientSession.ws_connect",
return_value=ClientWebSocketResponse(
Mock(), Mock(), Mock(), Mock(), Mock(), Mock(), Mock(), Mock()
),
):
account = Account()
account = Account(token_update_callback=token_update_callback)
if logged_in:
await account.connect(
username=USERNAME, password=PASSWORD, load_robots=load_robots
Expand Down
10 changes: 9 additions & 1 deletion tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from aiohttp.client_exceptions import ClientError
from aioresponses import aioresponses

from pylitterbot import FeederRobot, LitterRobot3
from pylitterbot.exceptions import LitterRobotException, LitterRobotLoginException
from pylitterbot.robot.litterrobot4 import LR4_ENDPOINT

Expand All @@ -33,9 +34,10 @@ async def test_account(
mock_aioresponse: aioresponses, caplog: pytest.LogCaptureFixture
) -> None:
"""Tests that an account is properly setup."""
account = await get_account()
account = await get_account(token_update_callback=lambda token: None)
assert account.user_id is None
assert account.robots == []
assert len(account.session._listeners) == 1 # pylint: disable=protected-access

with pytest.raises(LitterRobotLoginException):
await account.connect()
Expand All @@ -44,6 +46,12 @@ async def test_account(
assert account.user_id == USER_ID
assert len(account.robots) == ROBOT_COUNT

assert len(account.get_robots(LitterRobot3)) == 2
assert len(account.get_robots(FeederRobot)) == 1

for robot in account.robots:
assert len(robot._listeners) == 0 # pylint: disable=protected-access

mock_aioresponse.post(
LR4_ENDPOINT,
payload={"data": {"getLitterRobot4ByUser": [LITTER_ROBOT_4_DATA]}},
Expand Down

0 comments on commit 63d4122

Please sign in to comment.