Improve code coverage for LastFM (#97012)

* Improve code coverage for LastFM

* Revert introduced bug
pull/97098/head
Joost Lekkerkerker 2023-07-23 20:09:16 +02:00 committed by GitHub
parent 5158461dec
commit dd6cd0096a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 11 deletions

View File

@ -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")
async def mock_setup_integration(
hass: HomeAssistant,
@ -54,6 +68,17 @@ async def mock_setup_integration(
@pytest.fixture(name="default_user")
def mock_default_user() -> MockUser:
"""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(
now_playing_result=Track("artist", "title", MockNetwork("lastfm")),
top_tracks=[Track("artist", "title", MockNetwork("lastfm"))],

View File

@ -139,10 +139,12 @@ async def test_flow_friends_invalid_username(
async def test_flow_friends_no_friends(
hass: HomeAssistant, default_user: MockUser
hass: HomeAssistant, default_user_no_friends: MockUser
) -> None:
"""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(
DOMAIN,
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(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
imported_config_entry: MockConfigEntry,
default_user: MockUser,
) -> None:
"""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):
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(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
default_user: MockUser,
imported_config_entry: MockConfigEntry,
default_user_no_friends: MockUser,
) -> None:
"""Test updating options gained from import."""
await setup_integration(config_entry, default_user)
with patch("pylast.User", return_value=default_user):
await setup_integration(imported_config_entry, default_user_no_friends)
with patch("pylast.User", return_value=default_user_no_friends):
entry = hass.config_entries.async_entries(DOMAIN)[0]
result = await hass.config_entries.options.async_init(entry.entry_id)
await hass.async_block_till_done()
@ -294,11 +296,11 @@ async def test_options_flow_without_friends(
hass: HomeAssistant,
setup_integration: ComponentSetup,
config_entry: MockConfigEntry,
default_user: MockUser,
default_user_no_friends: MockUser,
) -> None:
"""Test updating options for someone without friends."""
await setup_integration(config_entry, default_user)
with patch("pylast.User", return_value=default_user):
await setup_integration(config_entry, default_user_no_friends)
with patch("pylast.User", return_value=default_user_no_friends):
entry = hass.config_entries.async_entries(DOMAIN)[0]
result = await hass.config_entries.options.async_init(entry.entry_id)
await hass.async_block_till_done()