Improve coverage for LastFM (#93661)

* Improve coverage for LastFM

* Improve tests

* Improve tests
This commit is contained in:
Joost Lekkerkerker 2023-06-08 22:55:16 +02:00 committed by GitHub
parent ca936d0b38
commit b3a001996d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 291 additions and 134 deletions

View file

@ -1,22 +1,92 @@
"""Tests for the lastfm sensor."""
from unittest.mock import patch
from pylast import Track
from pylast import WSError
from homeassistant.components.lastfm.const import DOMAIN, STATE_NOT_SCROBBLING
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 CONF_DATA, MockNetwork, patch_fetch_user
from . import API_KEY, USERNAME_1, MockUser
from .conftest import ComponentSetup
from tests.common import MockConfigEntry
LEGACY_CONFIG = {
Platform.SENSOR: [
{CONF_PLATFORM: DOMAIN, CONF_API_KEY: API_KEY, CONF_USERS: [USERNAME_1]}
]
}
async def test_update_not_playing(hass: HomeAssistant) -> None:
"""Test update when no playing song."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options=CONF_DATA)
entry.add_to_hass(hass)
with patch_fetch_user(None):
await hass.config_entries.async_setup(entry.entry_id)
async def test_legacy_migration(hass: HomeAssistant) -> None:
"""Test migration from yaml to config flow."""
with patch("pylast.User", return_value=None):
assert await async_setup_component(hass, Platform.SENSOR, LEGACY_CONFIG)
await hass.async_block_till_done()
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].state is ConfigEntryState.LOADED
issue_registry = ir.async_get(hass)
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)
@ -24,15 +94,20 @@ async def test_update_not_playing(hass: HomeAssistant) -> None:
assert state.state == STATE_NOT_SCROBBLING
async def test_update_playing(hass: HomeAssistant) -> None:
async def test_update_playing(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
default_user: MockUser,
) -> None:
"""Test update when playing a song."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options=CONF_DATA)
entry.add_to_hass(hass)
with patch_fetch_user(Track("artist", "title", MockNetwork("test"))):
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
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