tests: use mocker fixture for mode limit tests#359
Merged
MaStr merged 1 commit intoMaStr:mainfrom May 1, 2026
Merged
Conversation
MaStr
approved these changes
May 1, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors the core “mode 8 / limit battery charge rate” tests to reduce repeated patch decorator stacks by introducing a shared core_dependencies fixture that patches Batcontrol’s provider factories.
Changes:
- Add
core_dependenciespytest fixture to patch core factory dependencies and return a mocked inverter. - Update mode-limit tests to use
core_dependencies(andmockerfor MQTT API mocks) instead of repeated@patchstacks.
Comment on lines
88
to
90
| bc = Batcontrol(mock_config) | ||
|
|
||
| # Test setting limit within bounds | ||
| bc.limit_battery_charge_rate(2000) |
Comment on lines
99
to
101
| bc = Batcontrol(mock_config) | ||
|
|
||
| # Try to set limit above max_pv_charge_rate | ||
| bc.limit_battery_charge_rate(5000) |
Comment on lines
109
to
111
| bc = Batcontrol(mock_config) | ||
|
|
||
| # Try to set limit below min_pv_charge_rate | ||
| bc.limit_battery_charge_rate(50) |
Comment on lines
120
to
122
| bc = Batcontrol(mock_config) | ||
|
|
||
| # Set limit to 0 | ||
| bc.limit_battery_charge_rate(0) |
Comment on lines
142
to
144
| bc = Batcontrol(mock_config) | ||
|
|
||
| # Set a valid limit first (otherwise default -1 will fall back to mode 10) | ||
| bc._limit_battery_charge_rate = 2000 |
Comment on lines
167
to
170
| bc = Batcontrol(mock_config) | ||
| bc.mqtt_api = MagicMock() | ||
| bc.mqtt_api = mocker.MagicMock() | ||
|
|
||
| # Set mode to 8 first | ||
| bc.limit_battery_charge_rate(1000) |
Comment on lines
+50
to
+78
| @pytest.fixture | ||
| def core_dependencies(self, mocker): | ||
| """Patch Batcontrol dependencies and return the mocked inverter.""" | ||
| mock_inverter = mocker.MagicMock() | ||
| mock_inverter.max_pv_charge_rate = 3000 | ||
| mock_inverter.set_mode_limit_battery_charge = mocker.MagicMock() | ||
| mock_inverter.get_max_capacity = mocker.MagicMock(return_value=10000) | ||
|
|
||
| mocker.patch( | ||
| 'batcontrol.core.tariff_factory.create_tarif_provider', | ||
| autospec=True, | ||
| return_value=mocker.MagicMock(), | ||
| ) | ||
| mocker.patch( | ||
| 'batcontrol.core.inverter_factory.create_inverter', | ||
| autospec=True, | ||
| return_value=mock_inverter, | ||
| ) | ||
| mocker.patch( | ||
| 'batcontrol.core.solar_factory.create_solar_provider', | ||
| autospec=True, | ||
| return_value=mocker.MagicMock(), | ||
| ) | ||
| mocker.patch( | ||
| 'batcontrol.core.consumption_factory.create_consumption', | ||
| autospec=True, | ||
| return_value=mocker.MagicMock(), | ||
| ) | ||
| return mock_inverter |
Comment on lines
131
to
133
| bc = Batcontrol(mock_config) | ||
|
|
||
| # min_pv_charge_rate should have been clamped to max_pv_charge_rate at init | ||
| assert bc.min_pv_charge_rate == 3000 |
Comment on lines
154
to
157
| bc = Batcontrol(mock_config) | ||
| bc.mqtt_api = MagicMock() | ||
| bc.mqtt_api = mocker.MagicMock() | ||
|
|
||
| # Call api_set_limit_battery_charge_rate | ||
| bc.api_set_limit_battery_charge_rate(2500) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactor the mode-limit core tests to use a shared
mockerfixture instead of repeated@patchstacks.