-
-
Notifications
You must be signed in to change notification settings - Fork 35.8k
Refactor tests in GIOS #155756
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
base: dev
Are you sure you want to change the base?
Refactor tests in GIOS #155756
Conversation
|
Hey there @bieniu, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
| result = await hass.config_entries.flow.async_configure( | ||
| result["flow_id"], CONFIG | ||
| ) | ||
| await hass.async_block_till_done() |
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.
It is not needed here.
| return_value={}, | ||
| ), | ||
| ): | ||
| flow = config_flow.GiosFlowHandler() |
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.
This looked weird to me, so I used the hass.config_entries.flow.async_init method that creates this object.
| assert state.state == "good" | ||
|
|
||
|
|
||
| async def test_invalid_indexes(hass: HomeAssistant) -> None: |
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.
This doesn't look like a test of our code, but a test of the gios library.
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.
I checked this again and it was reason of decrease of test coverage.
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.
Pull Request Overview
This PR modernizes the GIOS integration test suite by refactoring it to use modern pytest fixtures and patterns instead of manually loading JSON fixtures and patching API calls. The changes improve test maintainability and align with current Home Assistant testing best practices.
Key changes:
- Introduces a centralized
conftest.pywith reusable fixtures (mock_gios,mock_config_entry,init_integration) - Replaces JSON fixture files with in-code test data using
gios.modeldataclasses - Migrates tests to use modern mocking patterns with
MagicMockand cleaner setup/teardown - Adds
freezegunfor time-based testing instead of manual time manipulation
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
tests/components/gios/conftest.py |
New file introducing shared fixtures with mocked GIOS client and test data |
tests/components/gios/__init__.py |
Replaced init_integration helper with simpler setup_integration function |
tests/components/gios/test_sensor.py |
Refactored tests to use fixtures and split test_availability into separate test functions |
tests/components/gios/test_init.py |
Updated all tests to use new fixture pattern, removed manual mocking |
tests/components/gios/test_config_flow.py |
Simplified by using mock_gios fixture and removing JSON loading |
tests/components/gios/test_diagnostics.py |
Updated to use new fixture pattern |
tests/components/gios/fixtures/*.json |
Removed obsolete JSON fixture files |
| data={"station_id": 123, "name": "Home"}, | ||
| entry_id="86129426118ae32020417a53712d6eef", | ||
| ) | ||
| from tests.common import MockConfigEntry |
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.
Test coverage has decreased
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
homeassistant/components/gios/__init__.py 43 1 98% 33
homeassistant/components/gios/config_flow.py 37 0 100%
homeassistant/components/gios/const.py 20 0 100%
homeassistant/components/gios/coordinator.py 28 0 100%
homeassistant/components/gios/diagnostics.py 8 0 100%
homeassistant/components/gios/sensor.py 58 1 98% 210
homeassistant/components/gios/system_health.py 10 0 100%
------------------------------------------------------------------------------
TOTAL 204 2 99%
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.
Fixed.
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
homeassistant/components/gios/__init__.py 43 0 100%
homeassistant/components/gios/config_flow.py 37 0 100%
homeassistant/components/gios/const.py 20 0 100%
homeassistant/components/gios/coordinator.py 28 0 100%
homeassistant/components/gios/diagnostics.py 8 0 100%
homeassistant/components/gios/sensor.py 58 0 100%
homeassistant/components/gios/system_health.py 10 0 100%
------------------------------------------------------------------------------
TOTAL 204 0 100%
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.
Two problems:
- Refactor tests in GIOS #155756 (comment)
- and missing tests for unique_id int -> str migration
| ) | ||
|
|
||
| assert result["type"] is FlowResultType.FORM | ||
| assert result["errors"] == {"base": "cannot_connect"} |
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.
Here it is also necessary to prove that recovery flow after an error is possible.
| @pytest.fixture(autouse=True) | ||
| def override_platforms() -> Generator[None]: | ||
| """Override PLATFORMS.""" | ||
| with patch("homeassistant.components.gios.PLATFORMS", [Platform.SENSOR]): | ||
| yield |
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 patch PLATFORMS if the integration only supports the SENSOR platform?
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.
This is old code and I extended it to cover the entire module.
core/tests/components/gios/test_sensor.py
Line 27 in 4cdfa3b
| with patch("homeassistant.components.gios.PLATFORMS", [Platform.SENSOR]): |
|
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Proposed change
This refactoring makes the tests mock the whole
Giosobject instead of individual methods, as requested in https://github.com/home-assistant/core/pull/155603/files#r2483768993. I also created fixtures for tests to reduce duplication.Type 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: