8000 Modernize tests for smhi by gjohansson-ST · Pull Request #139334 · home-assistant/core · GitHub
[go: up one dir, main page]

Skip to content

Modernize tests for smhi #139334

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

Merged
merged 9 commits into from
May 28, 2025
Merged

Modernize tests for smhi #139334

merged 9 commits into from
May 28, 2025

Conversation

gjohansson-ST
Copy link
Member

Proposed change

Bring the smhi tests up to date

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:

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.

To help with the load of incoming pull requests:

Copy link

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
If you are the author of this PR, please leave a comment if you want to keep it open. Also, please rebase your PR onto the latest dev branch to ensure that it's up to date with the latest changes.
Thank you for your contribution!

@github-actions github-actions bot added the stale label Apr 27, 2025
@github-actions github-actions bot closed this May 4, 2025
@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2025
@gjohansson-ST gjohansson-ST reopened this May 19, 2025
@home-assistant home-assistant unlocked this conversation May 19, 2025
@github-actions github-actions bot removed the stale label May 19, 2025
@gjohansson-ST gjohansson-ST marked this pull request as ready for review May 21, 2025 18:01
Comment on lines 66 to 103
10000
@pytest.fixture(name="mock_client")
async def get_client(
hass: HomeAssistant,
get_data: tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]],
) -> AsyncGenerator[MagicMock]:
"""Mock SMHIPointForecast client."""

with patch(
"homeassistant.components.smhi.coordinator.SMHIPointForecast",
autospec=True,
) as mock_client:
client = mock_client.return_value
client.async_get_daily_forecast.return_value = get_data[0]
client.async_get_twice_daily_forecast.return_value = get_data[1]
client.async_get_hourly_forecast.return_value = get_data[2]
yield client


@pytest.fixture(name="get_data")
async def get_data_from_library(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
load_json: dict[str, Any],
) -> AsyncGenerator[tuple[list[SMHIForecast], list[SMHIForecast], list[SMHIForecast]]]:
"""Get data from api."""
client = SMHIPointForecast(
TEST_CONFIG[CONF_LOCATION][CONF_LONGITUDE],
TEST_CONFIG[CONF_LOCATION][CONF_LATITUDE],
aioclient_mock.create_session(hass.loop),
)
with patch.object(
client._api,
"async_get_data",
return_value=load_json,
):
data_daily = await client.async_get_daily_forecast()
data_twice_daily = await client.async_get_twice_daily_forecast()
data_hourly = await client.async_get_hourly_forecast()
Copy link
Member

Choose a reason for hiding this comment

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

So wait, we patch something in the library to make sure it processes 3 files we load via JSON?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it processes the same file (first file below).
The other files are only loaded so we can use them later in the tests when we need to switch to another json file.

Copy link
Member

Choose a reason for hiding this comment

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

Why dont we load them when we need them? And why do we patch the library get request thingy

Copy link
Member Author

Choose a reason for hiding this comment

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

Because that creates additional overhead than to just load them once and then we have them ready for tests that needs them.
Not sure what you mean by the other comment but we patch the get request to input our json instead. then the other calls which are returned are manipulating the data.
It would be much more effort to patch the returns of those instead of the data collection.

8000
@home-assistant home-assistant bot marked this pull request as draft May 21, 2025 20:26
@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.

@gjohansson-ST gjohansson-ST marked this pull request as ready for review May 23, 2025 13:08
@home-assistant home-assistant bot requested a review from joostlek May 23, 2025 13:08
@home-assistant home-assistant bot marked this pull request as draft May 26, 2025 13:10
@gjohansson-ST gjohansson-ST marked this pull request as ready for review May 27, 2025 17:41
@home-assistant home-assistant bot requested a review from joostlek May 27, 2025 17:41
@joostlek joostlek merged commit 4858b21 into dev May 28, 2025
34 checks passed
@joostlek joostlek deleted the gj-20250225-03 branch May 28, 2025 08:56
@github-actions github-actions bot locked and limited conversation to collaborators May 29, 2025
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.

2 participants
0