Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve set volume tests #40

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/linkplay/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ async def set_play_mode(self, mode: PlayingMode) -> None:
@property
def muted(self) -> bool:
"""Returns if the player is muted."""
return self.properties.get(PlayerAttribute.MUTED, MuteMode.UNMUTED) == MuteMode.MUTED
return (
self.properties.get(PlayerAttribute.MUTED, MuteMode.UNMUTED)
== MuteMode.MUTED
)

@property
def title(self) -> str:
Expand Down Expand Up @@ -186,32 +189,46 @@ def total_length(self) -> int:
@property
def status(self) -> PlayingStatus:
"""Returns the current playing status."""
return PlayingStatus(self.properties.get(PlayerAttribute.PLAYING_STATUS, PlayingStatus.STOPPED))
return PlayingStatus(
self.properties.get(PlayerAttribute.PLAYING_STATUS, PlayingStatus.STOPPED)
)

@property
def equalizer_mode(self) -> EqualizerMode:
"""Returns the current equalizer mode."""
return EqualizerMode(self.properties.get(PlayerAttribute.EQUALIZER_MODE, EqualizerMode.CLASSIC))
return EqualizerMode(
self.properties.get(PlayerAttribute.EQUALIZER_MODE, EqualizerMode.CLASSIC)
)

@property
def speaker_type(self) -> SpeakerType:
"""Returns the current speaker the player is playing on."""
return SpeakerType(self.properties.get(PlayerAttribute.SPEAKER_TYPE, SpeakerType.MAIN_SPEAKER))
return SpeakerType(
self.properties.get(PlayerAttribute.SPEAKER_TYPE, SpeakerType.MAIN_SPEAKER)
)

@property
def channel_type(self) -> ChannelType:
"""Returns the channel the player is playing on."""
return ChannelType(self.properties.get(PlayerAttribute.CHANNEL_TYPE, ChannelType.STEREO))
return ChannelType(
self.properties.get(PlayerAttribute.CHANNEL_TYPE, ChannelType.STEREO)
)

@property
def play_mode(self) -> PlayingMode:
"""Returns the current playing mode of the player."""
return PlayingMode(self.properties.get(PlayerAttribute.PLAYBACK_MODE, PlayingMode.IDLE))
return PlayingMode(
self.properties.get(PlayerAttribute.PLAYBACK_MODE, PlayingMode.IDLE)
)

@property
def loop_mode(self) -> LoopMode:
"""Returns the current playlist mode."""
return LoopMode(self.properties.get(PlayerAttribute.PLAYLIST_MODE, LoopMode.CONTINUOUS_PLAYBACK))
return LoopMode(
self.properties.get(
PlayerAttribute.PLAYLIST_MODE, LoopMode.CONTINUOUS_PLAYBACK
)
)


class LinkPlayBridge:
Expand Down Expand Up @@ -292,7 +309,9 @@ async def remove_follower(self, follower: LinkPlayBridge) -> None:

async def set_volume(self, value: int) -> None:
"""Sets the volume for the multiroom group."""
assert 0 < value <= 100
if not 0 <= value <= 100:
raise ValueError("Volume must be between 0 and 100")

str_vol = str(value)
await self.leader.request(LinkPlayCommand.MULTIROOM_VOL.format(str_vol)) # type: ignore[str-format]

Expand Down
37 changes: 31 additions & 6 deletions tests/linkplay/test_bridge.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Test bridge functionality."""

from typing import Any
from unittest.mock import AsyncMock

import pytest

from linkplay.bridge import (
LinkPlayBridge,
LinkPlayDevice,
Expand Down Expand Up @@ -166,14 +169,25 @@ async def test_player_toggle():
bridge.request.assert_called_once_with(LinkPlayCommand.TOGGLE)


async def test_player_set_volume():
@pytest.mark.parametrize("volume", range(0, 101))
async def test_player_set_volume(volume: int):
"""Tests if the player set volume is correctly called."""
bridge = AsyncMock()
player = LinkPlayPlayer(bridge)

await player.set_volume(100)
await player.set_volume(volume)

bridge.request.assert_called_once_with(LinkPlayCommand.VOLUME.format(volume))


@pytest.mark.parametrize("volume", [-1, 101])
async def test_player_set_volume_raises_value_error(volume: Any):
"""Tests if the player set volume is correctly called."""
bridge = AsyncMock()
player = LinkPlayPlayer(bridge)

bridge.request.assert_called_once_with(LinkPlayCommand.VOLUME.format(100))
with pytest.raises(ValueError):
await player.set_volume(volume)


async def test_player_set_equalizer_mode():
Expand Down Expand Up @@ -297,11 +311,22 @@ async def test_multiroom_unmute():
leader.request.assert_called_once_with(LinkPlayCommand.MULTIROOM_UNMUTE)


async def test_multiroom_set_volume():
@pytest.mark.parametrize("volume", range(0, 101))
async def test_multiroom_set_volume(volume: int):
"""Tests if multiroom set volume is correctly called on the leader."""
leader = AsyncMock()
multiroom = LinkPlayMultiroom(leader)

await multiroom.set_volume(100)
await multiroom.set_volume(volume)

leader.request.assert_called_once_with(LinkPlayCommand.MULTIROOM_VOL.format(volume))


@pytest.mark.parametrize("volume", [-1, 101])
async def test_multiroom_set_volume_raises_value_error(volume: int):
"""Tests if multiroom set volume is correctly called on the leader."""
leader = AsyncMock()
multiroom = LinkPlayMultiroom(leader)

leader.request.assert_called_once_with(LinkPlayCommand.MULTIROOM_VOL.format(100))
with pytest.raises(ValueError):
await multiroom.set_volume(volume)