Improve code coverage for LastFM (#97012)
* Improve code coverage for LastFM * Revert introduced bug
This commit is contained in:
parent
5158461dec
commit
dd6cd0096a
2 changed files with 38 additions and 11 deletions
|
@ -36,6 +36,20 @@ def mock_config_entry() -> MockConfigEntry:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="imported_config_entry")
|
||||||
|
def mock_imported_config_entry() -> MockConfigEntry:
|
||||||
|
"""Create LastFM entry in Home Assistant."""
|
||||||
|
return MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={},
|
||||||
|
options={
|
||||||
|
CONF_API_KEY: API_KEY,
|
||||||
|
CONF_MAIN_USER: None,
|
||||||
|
CONF_USERS: [USERNAME_1, USERNAME_2],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="setup_integration")
|
@pytest.fixture(name="setup_integration")
|
||||||
async def mock_setup_integration(
|
async def mock_setup_integration(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -54,6 +68,17 @@ async def mock_setup_integration(
|
||||||
@pytest.fixture(name="default_user")
|
@pytest.fixture(name="default_user")
|
||||||
def mock_default_user() -> MockUser:
|
def mock_default_user() -> MockUser:
|
||||||
"""Return default mock user."""
|
"""Return default mock user."""
|
||||||
|
return MockUser(
|
||||||
|
now_playing_result=Track("artist", "title", MockNetwork("lastfm")),
|
||||||
|
top_tracks=[Track("artist", "title", MockNetwork("lastfm"))],
|
||||||
|
recent_tracks=[Track("artist", "title", MockNetwork("lastfm"))],
|
||||||
|
friends=[MockUser()],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="default_user_no_friends")
|
||||||
|
def mock_default_user_no_friends() -> MockUser:
|
||||||
|
"""Return default mock user without friends."""
|
||||||
return MockUser(
|
return MockUser(
|
||||||
now_playing_result=Track("artist", "title", MockNetwork("lastfm")),
|
now_playing_result=Track("artist", "title", MockNetwork("lastfm")),
|
||||||
top_tracks=[Track("artist", "title", MockNetwork("lastfm"))],
|
top_tracks=[Track("artist", "title", MockNetwork("lastfm"))],
|
||||||
|
|
|
@ -139,10 +139,12 @@ async def test_flow_friends_invalid_username(
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_friends_no_friends(
|
async def test_flow_friends_no_friends(
|
||||||
hass: HomeAssistant, default_user: MockUser
|
hass: HomeAssistant, default_user_no_friends: MockUser
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test options is empty when user has no friends."""
|
"""Test options is empty when user has no friends."""
|
||||||
with patch("pylast.User", return_value=default_user), patch_setup_entry():
|
with patch(
|
||||||
|
"pylast.User", return_value=default_user_no_friends
|
||||||
|
), patch_setup_entry():
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
context={"source": SOURCE_USER},
|
context={"source": SOURCE_USER},
|
||||||
|
@ -177,11 +179,11 @@ async def test_import_flow_success(hass: HomeAssistant, default_user: MockUser)
|
||||||
async def test_import_flow_already_exist(
|
async def test_import_flow_already_exist(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
setup_integration: ComponentSetup,
|
||||||
config_entry: MockConfigEntry,
|
imported_config_entry: MockConfigEntry,
|
||||||
default_user: MockUser,
|
default_user: MockUser,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test import of yaml already exist."""
|
"""Test import of yaml already exist."""
|
||||||
await setup_integration(config_entry, default_user)
|
await setup_integration(imported_config_entry, default_user)
|
||||||
|
|
||||||
with patch("pylast.User", return_value=default_user):
|
with patch("pylast.User", return_value=default_user):
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
@ -275,12 +277,12 @@ async def test_options_flow_incorrect_username(
|
||||||
async def test_options_flow_from_import(
|
async def test_options_flow_from_import(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
setup_integration: ComponentSetup,
|
||||||
config_entry: MockConfigEntry,
|
imported_config_entry: MockConfigEntry,
|
||||||
default_user: MockUser,
|
default_user_no_friends: MockUser,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test updating options gained from import."""
|
"""Test updating options gained from import."""
|
||||||
await setup_integration(config_entry, default_user)
|
await setup_integration(imported_config_entry, default_user_no_friends)
|
||||||
with patch("pylast.User", return_value=default_user):
|
with patch("pylast.User", return_value=default_user_no_friends):
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
@ -294,11 +296,11 @@ async def test_options_flow_without_friends(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
setup_integration: ComponentSetup,
|
setup_integration: ComponentSetup,
|
||||||
config_entry: MockConfigEntry,
|
config_entry: MockConfigEntry,
|
||||||
default_user: MockUser,
|
default_user_no_friends: MockUser,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test updating options for someone without friends."""
|
"""Test updating options for someone without friends."""
|
||||||
await setup_integration(config_entry, default_user)
|
await setup_integration(config_entry, default_user_no_friends)
|
||||||
with patch("pylast.User", return_value=default_user):
|
with patch("pylast.User", return_value=default_user_no_friends):
|
||||||
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||||
result = await hass.config_entries.options.async_init(entry.entry_id)
|
result = await hass.config_entries.options.async_init(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
Loading…
Add table
Reference in a new issue