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 fyta tests #117661

Merged
merged 9 commits into from
May 23, 2024
Merged

Improve fyta tests #117661

merged 9 commits into from
May 23, 2024

Conversation

dontinelli
Copy link
Contributor

Proposed change

Add tests for init.py, remove file from .coveragerc

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 Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

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

- [ ] Documentation added/updated for www.home-assistant.io

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:

@dontinelli dontinelli requested a review from joostlek May 17, 2024 19:32
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Comment on lines 48 to 57
with (
patch(
"homeassistant.components.fyta.FytaConnector",
return_value=mock_fyta_connector,
),
patch(
"homeassistant.components.fyta.config_flow.FytaConnector",
return_value=mock_fyta_connector,
),
):
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 check analytics insights on how we do it there? That also makes sure the object is actually completely specced to the class it's mocking, which makes it a bit better to use and raises errors if something is not mocked properly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

    with (
        patch(
            "homeassistant.components.analytics_insights.HomeassistantAnalyticsClient",
            autospec=True,
        ) as mock_client,
        patch(
            "homeassistant.components.analytics_insights.config_flow.HomeassistantAnalyticsClient",
            new=mock_client,
        ),
    ):

do you only mean autospec=True?

Copy link
Member

Choose a reason for hiding this comment

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

and new=.., but because the mock_client comes from as mock_client, the mock is automatically autospecced

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For my understanding: new=mock_client you use, if you refer to as mock_client above. However, if you define mock_client before the with/patch-clause, one uses return_value=mock_client (which makes new= obsolete). Correct?

Comment on lines 61 to 71
with patch(
"homeassistant.components.fyta.config_flow.FytaConfigFlow.async_step_reauth",
return_value={
"type": FlowResultType.ABORT,
"flow_id": "mock_flow",
"step_id": "reauth_confirm",
"reason": "reauth_successful",
},
) as mock_async_step_reauth:
await setup_platform(hass, mock_config_entry, [SENSOR_DOMAIN])
await hass.async_block_till_done()
Copy link
Member

Choose a reason for hiding this comment

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

We should not patch the config flow, we should just assert this sets the state of the entry to SETUP_ERROR and starts a new flow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got this from

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR.
How do I test if a new flow is started? Or is the first sufficient?

@frenck frenck changed the title Add tests for init.py Add fyta tests for init.py May 18, 2024
@dontinelli dontinelli requested a review from joostlek May 22, 2024 12:05
tests/components/fyta/test_init.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft May 22, 2024 12:16
@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.

@dontinelli dontinelli marked this pull request as ready for review May 22, 2024 12:44
@home-assistant home-assistant bot requested a review from joostlek May 22, 2024 12:44
@joostlek joostlek changed the title Add fyta tests for init.py Improve fyta tests May 23, 2024
@joostlek joostlek merged commit 6682244 into home-assistant:dev May 23, 2024
24 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2024
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