8000 Use snapshot testing in LastFM by joostlek · Pull Request #97009 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use snapshot testing in LastFM #97009

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

Merged
merged 2 commits into from
Jul 22, 2023
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
< 8000 div class="select-menu-blankslate select-menu-error"> Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tests/components/lastfm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
51 changes: 51 additions & 0 deletions tests/components/lastfm/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
@@ -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',
})
# ---
86 changes: 19 additions & 67 deletions tests/components/lastfm/test_sensor.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
"""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
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
Expand All @@ -41,73 +38,28 @@ async def test_legacy_migration(hass: HomeAssistant) -> None:
assert len(issue_registry.issues) == 1


async def test_user_unavailable(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
) -> None:
"""Test update when user can't be fetched."""
await setup_integration(
config_entry,
MockUser(thrown_error=WSError("network", "status", "User not found")),
)

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(
@pytest.mark.parametrize(
("fixture"),
[
("not_found_user"),
("first_time_user"),
("default_user"),
],
)
async def test_sensors(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
default_user: MockUser,
snapshot: SnapshotAssertion,
fixture: str,
request: pytest.FixtureRequest,
) -> None:
"""Test update when playing a song."""
await setup_integration(config_entry, default_user)
"""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 == "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
0