Files
core/tests/components/lastfm/__init__.py
Joost Lekkerkerker a96215bf2e Lastfm config flow (#92299)
* Move constant values to separate file

* Move constant values to separate file

* Add config flow to lastfm

* Add tests

* Add config flow to lastfm

* Add tests

* Add tests

* Add tests

* Add extra form for main user and autofill with friends

* Add extra form for main user and autofill with friends

* Add extra form for main user and autofill with friends

* Add extra form for main user and autofill with friends

* Add OptionsFlow

* Add tests

* Fix feedback

* Fix feedback

* Fix feedback

* Fix feedback

* Fix test

* Apply suggestions from code review

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Update config_flow.py

* Update config_flow.py

* Update config_flow.py

* Update homeassistant/components/lastfm/config_flow.py

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Add tests

* Cleanup

* Update config_flow.py

* Update config_flow.py

* Update config_flow.py

* Fix test

* Fix feedback

* Codeowner lastfm

* Fix feedback

* Fix feedback

* Parametrize errors

* Parametrize errors

* Parametrize errors

* Finish tests

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
2023-05-25 14:48:16 +02:00

88 lines
2.3 KiB
Python

"""The tests for lastfm."""
from unittest.mock import patch
from pylast import Track, WSError
from homeassistant.components.lastfm.const import CONF_MAIN_USER, CONF_USERS
from homeassistant.const import CONF_API_KEY
API_KEY = "asdasdasdasdasd"
USERNAME_1 = "testaccount1"
USERNAME_2 = "testaccount2"
CONF_DATA = {
CONF_API_KEY: API_KEY,
CONF_MAIN_USER: USERNAME_1,
CONF_USERS: [USERNAME_1, USERNAME_2],
}
CONF_USER_DATA = {CONF_API_KEY: API_KEY, CONF_MAIN_USER: USERNAME_1}
CONF_FRIENDS_DATA = {CONF_USERS: [USERNAME_2]}
class MockNetwork:
"""Mock _Network object for pylast."""
def __init__(self, username: str):
"""Initialize the mock."""
self.username = username
class MockUser:
"""Mock User object for pylast."""
def __init__(self, now_playing_result, error, has_friends, username):
"""Initialize the mock."""
self._now_playing_result = now_playing_result
self._thrown_error = error
self._has_friends = has_friends
self.name = username
def get_name(self, capitalized: bool) -> str:
"""Get name of the user."""
return self.name
def get_playcount(self):
"""Get mock play count."""
if self._thrown_error:
raise self._thrown_error
return 1
def get_image(self):
"""Get mock image."""
def get_recent_tracks(self, limit):
"""Get mock recent tracks."""
return []
def get_top_tracks(self, limit):
"""Get mock top tracks."""
return []
def get_now_playing(self):
"""Get mock now playing."""
return self._now_playing_result
def get_friends(self):
"""Get mock friends."""
if self._has_friends is False:
raise WSError("network", "status", "Page not found")
return [MockUser(None, None, True, USERNAME_2)]
def patch_fetch_user(
now_playing: Track | None = None,
thrown_error: Exception | None = None,
has_friends: bool = True,
username: str = USERNAME_1,
) -> MockUser:
"""Patch interface."""
return patch(
"pylast.User",
return_value=MockUser(now_playing, thrown_error, has_friends, username),
)
def patch_setup_entry() -> bool:
"""Patch interface."""
return patch("homeassistant.components.lastfm.async_setup_entry", return_value=True)