Skip to content

tests: use mocker fixture for mode limit tests#359

Merged
MaStr merged 1 commit intoMaStr:mainfrom
filiplajszczak:tests-core-mocker-fixture
May 1, 2026
Merged

tests: use mocker fixture for mode limit tests#359
MaStr merged 1 commit intoMaStr:mainfrom
filiplajszczak:tests-core-mocker-fixture

Conversation

@filiplajszczak
Copy link
Copy Markdown
Contributor

Refactor the mode-limit core tests to use a shared mocker fixture instead of repeated @patch stacks.

@MaStr MaStr requested a review from Copilot May 1, 2026 17:32
@MaStr MaStr merged commit a010d20 into MaStr:main May 1, 2026
11 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_dependencies pytest fixture to patch core factory dependencies and return a mocked inverter.
  • Update mode-limit tests to use core_dependencies (and mocker for MQTT API mocks) instead of repeated @patch stacks.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants