Skip to content

Commit

Permalink
Improve test suite (#42)
Browse files Browse the repository at this point in the history
* Add command decorator

* Define more consts

* Remove asyncio mark

* Cache fixture loads

* Update group tests

* Return unknown command from mock device

* Add calls_commands decorator and simplify group tests

* Move items to common

* Clean-up player tests

* Update browse tests

* Update heos tests

* Simplify event writing

* Simplified matching classes

* Correct test names

* Add command under process support
  • Loading branch information
andrewsayre authored Jan 3, 2025
1 parent 7521c65 commit 4606314
Show file tree
Hide file tree
Showing 16 changed files with 1,101 additions and 895 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements.txt -r test-requirements.txt --upgrade --upgrade-strategy eager
- name: Run pytest on ${{ matrix.python-version }}
run: pytest --cov=./ --cov-report=xml
run: pytest --cov --cov-report=xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.0.1
with:
Expand Down
3 changes: 3 additions & 0 deletions pyheos/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ATTR_ALBUM: Final = "album"
ATTR_ARTIST: Final = "artist"
ATTR_AVAILABLE: Final = "available"
ATTR_COMMAND: Final = "command"
ATTR_CONTAINER: Final = "container"
ATTR_CONTAINER_ID: Final = "cid"
ATTR_COUNT: Final = "count"
Expand All @@ -24,13 +25,15 @@
ATTR_ERROR: Final = "error"
ATTR_ERROR_ID: Final = "eid"
ATTR_GROUP_ID: Final = "gid"
ATTR_HEOS: Final = "heos"
ATTR_ID: Final = "id"
ATTR_IMAGE_URL: Final = "image_url"
ATTR_INPUT: Final = "input"
ATTR_IP_ADDRESS: Final = "ip"
ATTR_LEVEL: Final = "level"
ATTR_LINE_OUT: Final = "lineout"
ATTR_MEDIA_ID: Final = "mid"
ATTR_MESSAGE: Final = "message"
ATTR_MODEL: Final = "model"
ATTR_MUTE: Final = "mute"
ATTR_NAME: Final = "name"
Expand Down
30 changes: 29 additions & 1 deletion pyheos/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Media:
source_id: int
name: str
type: const.MediaType
image_url: str
image_url: str = field(repr=False)
_heos: Optional["Heos"] = field(repr=False, hash=False, compare=False)


Expand Down Expand Up @@ -54,6 +54,18 @@ def from_data(
_heos=heos,
)

def clone(self) -> "MediaMusicSource":
"""Create a new instance from the current instance."""
return MediaMusicSource(
source_id=self.source_id,
name=self.name,
type=self.type,
image_url=self.image_url,
available=self.available,
service_username=self.service_username,
_heos=self._heos,
)

async def browse(self) -> "BrowseResult":
"""Browse the contents of this source.
Expand Down Expand Up @@ -141,6 +153,22 @@ async def play_media(
raise ValueError("Must be initialized with the 'heos' parameter to play")
await self._heos.play_media(player_id, self, add_criteria)

def clone(self) -> "MediaItem":
return MediaItem(
source_id=self.source_id,
name=self.name,
type=self.type,
image_url=self.image_url,
playable=self.playable,
browsable=self.browsable,
container_id=self.container_id,
media_id=self.media_id,
artist=self.artist,
album=self.album,
album_id=self.album_id,
_heos=self._heos,
)


@dataclass
class BrowseResult:
Expand Down
6 changes: 3 additions & 3 deletions pyheos/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def container(self) -> dict[str, Any]:
@cached_property
def heos(self) -> dict[str, Any]:
"""Get the HEOS section as a dictionary."""
return cast(dict[str, Any], self.container["heos"])
return cast(dict[str, Any], self.container[const.ATTR_HEOS])

@cached_property
def payload(self) -> dict[str, Any] | list[Any] | None:
Expand All @@ -82,12 +82,12 @@ def payload(self) -> dict[str, Any] | list[Any] | None:
@cached_property
def command(self) -> str:
"""Get the command the message is referring to."""
return str(self.heos["command"])
return str(self.heos[const.ATTR_COMMAND])

@cached_property
def message(self) -> dict[str, str]:
"""Get the message parameters as a dictionary. If not present in the message, an empty dictionary is returned."""
if raw_message := self.heos.get("message"):
if raw_message := self.heos.get(const.ATTR_MESSAGE):
return dict(parse_qsl(raw_message, keep_blank_values=True))
return {}

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ log_level = "DEBUG"
testpaths = ["tests"]
norecursedirs = ".git"
timeout = 60
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "function"

[tool.coverage.run]
Expand Down
Loading

0 comments on commit 4606314

Please sign in to comment.