Skip to content
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

Improve coverage for LastFM #93661

Merged
merged 8 commits into from Jun 8, 2023
Merged

Conversation

joostlek
Copy link
Member

Proposed change

Add tests for the sensor and migration from yaml.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:


async def test_user_unavailable(hass: HomeAssistant) -> None:
"""Test update when user can't be fetched."""
entry = MockConfigEntry(domain=DOMAIN, data={}, options=CONF_DATA)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a fixture

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you maybe have some good example about how to use fixtures in a place where the mock needs to handle different situations? I just tried to give it a shot but I think I am very much over engineering this and making it more complicated than I think should be useful

entry = MockConfigEntry(domain=DOMAIN, data={}, options=CONF_DATA)
entry.add_to_hass(hass)
with patch_fetch_user(thrown_error=WSError("network", "status", "User not found")):
await hass.config_entries.async_setup(entry.entry_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await hass.config_entries.async_setup(entry.entry_id)
assert await hass.config_entries.async_setup(entry.entry_id)

@@ -52,11 +69,15 @@ def get_image(self):

def get_recent_tracks(self, limit):
"""Get mock recent tracks."""
return []
if self._first_time:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit hacky doing it like this. What about making recent_tracks and top_tracks variables that can be passed in or else default to something. You can then make a fixture for a first time and regular user.

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few tiny changes.

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft May 28, 2023 00:57
@joostlek
Copy link
Member Author

joostlek commented Jun 5, 2023

Although it were tiny changes, reworked the tests to make use of fixtures and try to remove some duplication. Please let me know if there are possible improvement points, so I can get better on unit testing for HA :)

@joostlek joostlek marked this pull request as ready for review June 5, 2023 19:58
@home-assistant home-assistant bot requested a review from balloob June 5, 2023 19:58
@balloob balloob merged commit b3a0019 into home-assistant:dev Jun 8, 2023
24 checks passed
@joostlek joostlek deleted the lastfm_coverage branch June 8, 2023 21:03
@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants