forked from snaptec/openWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use ConfigurableDevice for json module to shorten and simplify code
- Loading branch information
Showing
5 changed files
with
78 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ jobs: | |
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install flake8 pytest paho-mqtt requests-mock | ||
pip install flake8 pytest paho-mqtt requests-mock jq | ||
- name: Flake8 with annotations | ||
uses: TrueBrain/[email protected] | ||
with: | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
import requests_mock | ||
|
||
from modules.common.fault_state import FaultState | ||
from modules.json import bat, counter, inverter | ||
from modules.json.config import Json, JsonConfiguration, JsonBatSetup, JsonBatConfiguration, \ | ||
JsonInverterConfiguration, JsonInverterSetup, JsonCounterSetup, JsonCounterConfiguration | ||
from modules.json.device import create_device | ||
|
||
|
||
@pytest.fixture | ||
def mock_value_store(monkeypatch): | ||
mock_value_store = Mock() | ||
mock_value_store_factory = Mock(return_value=mock_value_store) | ||
monkeypatch.setattr(bat, "get_bat_value_store", mock_value_store_factory) | ||
monkeypatch.setattr(counter, "get_counter_value_store", mock_value_store_factory) | ||
monkeypatch.setattr(inverter, "get_inverter_value_store", mock_value_store_factory) | ||
return mock_value_store | ||
|
||
|
||
@pytest.mark.parametrize("component_config,expected_power", [ | ||
pytest.param(JsonBatSetup(configuration=JsonBatConfiguration(jq_power=".some_value")), 42.0, id="bat"), | ||
pytest.param(JsonCounterSetup(configuration=JsonCounterConfiguration(jq_power=".some_value")), 42.0, id="counter"), | ||
pytest.param(JsonInverterSetup(configuration=JsonInverterConfiguration(jq_power=".some_value")), -42.0, id="pv"), | ||
]) | ||
def test_device(monkeypatch, mock_value_store: Mock, requests_mock: requests_mock.Mocker, component_config, | ||
expected_power: float): | ||
# setup | ||
monkeypatch.setattr(FaultState, "store_error", Mock()) | ||
requests_mock.get("http://sample_host/sample_path", json={"some_value": 42}) | ||
device_config = Json(configuration=JsonConfiguration("http://sample_host/sample_path")) | ||
|
||
# execution | ||
device = create_device(device_config) | ||
device.add_component(component_config) | ||
device.update() | ||
|
||
# evaluation | ||
assert len(mock_value_store.set.mock_calls) == 1 | ||
assert mock_value_store.set.call_args[0][0].power == expected_power |