Use snapshot testing in LastFM (#97009)

This commit is contained in:
Joost Lekkerkerker 2023-07-22 12:47:26 +02:00 committed by GitHub
parent fb460d343e
commit 9b717cb84f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 68 deletions

View file

@ -2,7 +2,7 @@
from collections.abc import Awaitable, Callable from collections.abc import Awaitable, Callable
from unittest.mock import patch from unittest.mock import patch
from pylast import Track from pylast import Track, WSError
import pytest import pytest
from homeassistant.components.lastfm.const import CONF_MAIN_USER, CONF_USERS, DOMAIN from homeassistant.components.lastfm.const import CONF_MAIN_USER, CONF_USERS, DOMAIN
@ -65,3 +65,9 @@ def mock_default_user() -> MockUser:
def mock_first_time_user() -> MockUser: def mock_first_time_user() -> MockUser:
"""Return first time mock user.""" """Return first time mock user."""
return MockUser(now_playing_result=None, top_tracks=[], recent_tracks=[]) return MockUser(now_playing_result=None, top_tracks=[], recent_tracks=[])
@pytest.fixture(name="not_found_user")
def mock_not_found_user() -> MockUser:
"""Return not found mock user."""
return MockUser(thrown_error=WSError("network", "status", "User not found"))

View file

@ -0,0 +1,51 @@
# serializer version: 1
# name: test_sensors[default_user]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Last.fm',
'entity_picture': '',
'friendly_name': 'testaccount1',
'icon': 'mdi:radio-fm',
'last_played': 'artist - title',
'play_count': 1,
'top_played': 'artist - title',
}),
'context': <ANY>,
'entity_id': 'sensor.testaccount1',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'artist - title',
})
# ---
# name: test_sensors[first_time_user]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Last.fm',
'entity_picture': '',
'friendly_name': 'testaccount1',
'icon': 'mdi:radio-fm',
'last_played': None,
'play_count': 0,
'top_played': None,
}),
'context': <ANY>,
'entity_id': 'sensor.testaccount1',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'Not Scrobbling',
})
# ---
# name: test_sensors[not_found_user]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Last.fm',
'friendly_name': 'testaccount1',
'icon': 'mdi:radio-fm',
}),
'context': <ANY>,
'entity_id': 'sensor.testaccount1',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unavailable',
})
# ---

View file

@ -1,15 +1,12 @@
"""Tests for the lastfm sensor.""" """Tests for the lastfm sensor."""
from unittest.mock import patch from unittest.mock import patch
from pylast import WSError import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.lastfm.const import ( from homeassistant.components.lastfm.const import (
ATTR_LAST_PLAYED,
ATTR_PLAY_COUNT,
ATTR_TOP_PLAYED,
CONF_USERS, CONF_USERS,
DOMAIN, DOMAIN,
STATE_NOT_SCROBBLING,
) )
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_API_KEY, CONF_PLATFORM, Platform from homeassistant.const import CONF_API_KEY, CONF_PLATFORM, Platform
@ -17,7 +14,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry as ir from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from . import API_KEY, USERNAME_1, MockUser from . import API_KEY, USERNAME_1
from .conftest import ComponentSetup from .conftest import ComponentSetup
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -41,73 +38,28 @@ async def test_legacy_migration(hass: HomeAssistant) -> None:
assert len(issue_registry.issues) == 1 assert len(issue_registry.issues) == 1
async def test_user_unavailable( @pytest.mark.parametrize(
("fixture"),
[
("not_found_user"),
("first_time_user"),
("default_user"),
],
)
async def test_sensors(
hass: HomeAssistant, hass: HomeAssistant,
setup_integration: ComponentSetup, setup_integration: ComponentSetup,
config_entry: MockConfigEntry, config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
fixture: str,
request: pytest.FixtureRequest,
) -> None: ) -> None:
"""Test update when user can't be fetched.""" """Test sensors."""
await setup_integration( user = request.getfixturevalue(fixture)
config_entry, await setup_integration(config_entry, user)
MockUser(thrown_error=WSError("network", "status", "User not found")),
)
entity_id = "sensor.testaccount1" entity_id = "sensor.testaccount1"
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state == "unavailable" assert state == snapshot
async def test_first_time_user(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
first_time_user: MockUser,
) -> None:
"""Test first time user."""
await setup_integration(config_entry, first_time_user)
entity_id = "sensor.testaccount1"
state = hass.states.get(entity_id)
assert state.state == STATE_NOT_SCROBBLING
assert state.attributes[ATTR_LAST_PLAYED] is None
assert state.attributes[ATTR_TOP_PLAYED] is None
assert state.attributes[ATTR_PLAY_COUNT] == 0
async def test_update_not_playing(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
first_time_user: MockUser,
) -> None:
"""Test update when no playing song."""
await setup_integration(config_entry, first_time_user)
entity_id = "sensor.testaccount1"
state = hass.states.get(entity_id)
assert state.state == STATE_NOT_SCROBBLING
async def test_update_playing(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
default_user: MockUser,
) -> None:
"""Test update when playing a song."""
await setup_integration(config_entry, default_user)
entity_id = "sensor.testaccount1"
state = hass.states.get(entity_id)
assert state.state == "artist - title"
assert state.attributes[ATTR_LAST_PLAYED] == "artist - title"
assert state.attributes[ATTR_TOP_PLAYED] == "artist - title"
assert state.attributes[ATTR_PLAY_COUNT] == 1