Skip to content

Commit

Permalink
Improve set volume tests (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeofphilberg authored Aug 22, 2024
1 parent 9fd2674 commit 8b15737
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
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)

0 comments on commit 8b15737

Please sign in to comment.