diff --git a/tests/components/lastfm/conftest.py b/tests/components/lastfm/conftest.py index 119d4796f57..8b8548ad1f9 100644 --- a/tests/components/lastfm/conftest.py +++ b/tests/components/lastfm/conftest.py @@ -2,7 +2,7 @@ from collections.abc import Awaitable, Callable from unittest.mock import patch -from pylast import Track +from pylast import Track, WSError import pytest 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: """Return first time mock user.""" 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")) diff --git a/tests/components/lastfm/snapshots/test_sensor.ambr b/tests/components/lastfm/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..a28e085c104 --- /dev/null +++ b/tests/components/lastfm/snapshots/test_sensor.ambr @@ -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': , + 'entity_id': 'sensor.testaccount1', + 'last_changed': , + 'last_updated': , + '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': , + 'entity_id': 'sensor.testaccount1', + 'last_changed': , + 'last_updated': , + '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': , + 'entity_id': 'sensor.testaccount1', + 'last_changed': , + 'last_updated': , + 'state': 'unavailable', + }) +# --- diff --git a/tests/components/lastfm/test_sensor.py b/tests/components/lastfm/test_sensor.py index e46cf99ffdc..ab9358be1d3 100644 --- a/tests/components/lastfm/test_sensor.py +++ b/tests/components/lastfm/test_sensor.py @@ -1,15 +1,12 @@ """Tests for the lastfm sensor.""" from unittest.mock import patch -from pylast import WSError +import pytest +from syrupy.assertion import SnapshotAssertion from homeassistant.components.lastfm.const import ( - ATTR_LAST_PLAYED, - ATTR_PLAY_COUNT, - ATTR_TOP_PLAYED, CONF_USERS, DOMAIN, - STATE_NOT_SCROBBLING, ) from homeassistant.config_entries import ConfigEntryState 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.setup import async_setup_component -from . import API_KEY, USERNAME_1, MockUser +from . import API_KEY, USERNAME_1 from .conftest import ComponentSetup from tests.common import MockConfigEntry @@ -41,73 +38,28 @@ async def test_legacy_migration(hass: HomeAssistant) -> None: 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, setup_integration: ComponentSetup, config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, + fixture: str, + request: pytest.FixtureRequest, ) -> None: - """Test update when user can't be fetched.""" - await setup_integration( - config_entry, - MockUser(thrown_error=WSError("network", "status", "User not found")), - ) + """Test sensors.""" + user = request.getfixturevalue(fixture) + await setup_integration(config_entry, user) entity_id = "sensor.testaccount1" state = hass.states.get(entity_id) - assert state.state == "unavailable" - - -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 + assert state == snapshot