-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
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
Modernize tests for smhi #139334
Conversation
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. |
9609f96
to
65b730b
Compare
tests/components/smhi/conftest.py
Outdated
@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: | ||
10000 | 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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Proposed change
Bring the
smhi
tests up to dateType of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: