-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
171 additions
and
35 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
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,145 @@ | ||
import pytest | ||
from unittest.mock import Mock | ||
|
||
from modules.lg import bat, counter, device, inverter | ||
from modules.common.component_state import BatState, CounterState, InverterState | ||
from test_utils.mock_ramdisk import MockRamdisk | ||
|
||
|
||
@pytest.fixture | ||
def mock_ramdisk(monkeypatch): | ||
return MockRamdisk(monkeypatch) | ||
|
||
|
||
@pytest.fixture | ||
def dev(): | ||
device_config = device.get_default_config() | ||
device_config["configuration"]["ip_address"] = API_URL | ||
device_config["configuration"]["password"] = "some password" | ||
dev = device.Device(device_config) | ||
dev.session_key = "67567d76-0c83-11ea-8a59-d84fb802005a" | ||
return dev | ||
|
||
|
||
def assert_battery_state_correct(state: BatState): | ||
assert state.soc == 14.58333 | ||
assert state.power == -372 | ||
|
||
|
||
def assert_counter_state_correct(state: CounterState): | ||
assert state.power == 11 | ||
|
||
|
||
def assert_inverter_state_correct(state: InverterState): | ||
assert state.power == 0 | ||
|
||
|
||
def test_valid_login(monkeypatch, dev): | ||
# setup | ||
mock_bat_value_store = Mock() | ||
monkeypatch.setattr(bat, "get_bat_value_store", Mock(return_value=mock_bat_value_store)) | ||
mock_counter_value_store = Mock() | ||
monkeypatch.setattr(counter, "get_counter_value_store", Mock(return_value=mock_counter_value_store)) | ||
mock_inverter_value_store = Mock() | ||
monkeypatch.setattr(inverter, "get_inverter_value_store", Mock(return_value=mock_inverter_value_store)) | ||
monkeypatch.setattr(device.Device, "_request_data", Mock(return_value=sample_auth_key_valid)) | ||
component_config = bat.get_default_config() | ||
component_config["id"] = None | ||
dev.add_component(component_config) | ||
component_config = counter.get_default_config() | ||
component_config["id"] = 1 | ||
dev.add_component(component_config) | ||
component_config = inverter.get_default_config() | ||
component_config["id"] = 2 | ||
dev.add_component(component_config) | ||
|
||
# execution | ||
dev.update() | ||
|
||
# evaluation | ||
assert_battery_state_correct(mock_bat_value_store.set.call_args[0][0]) | ||
assert_counter_state_correct(mock_counter_value_store.set.call_args[0][0]) | ||
assert_inverter_state_correct(mock_inverter_value_store.set.call_args[0][0]) | ||
|
||
|
||
def test_update_session_key(monkeypatch, dev): | ||
# setup | ||
mock_bat_value_store = Mock() | ||
monkeypatch.setattr(bat, "get_bat_value_store", Mock(return_value=mock_bat_value_store)) | ||
mock_counter_value_store = Mock() | ||
monkeypatch.setattr(counter, "get_counter_value_store", Mock(return_value=mock_counter_value_store)) | ||
mock_inverter_value_store = Mock() | ||
monkeypatch.setattr(inverter, "get_inverter_value_store", Mock(return_value=mock_inverter_value_store)) | ||
monkeypatch.setattr(device.Device, "_update_session_key", Mock()) | ||
monkeypatch.setattr(device.Device, "_request_data", Mock( | ||
side_effect=[sample_auth_key_invalid, sample_auth_key_valid])) | ||
component_config = bat.get_default_config() | ||
component_config["id"] = None | ||
dev.add_component(component_config) | ||
component_config = counter.get_default_config() | ||
component_config["id"] = 1 | ||
dev.add_component(component_config) | ||
component_config = inverter.get_default_config() | ||
component_config["id"] = 2 | ||
dev.add_component(component_config) | ||
|
||
# execution | ||
dev.update() | ||
|
||
# evaluation | ||
assert_battery_state_correct(mock_bat_value_store.set.call_args[0][0]) | ||
assert_counter_state_correct(mock_counter_value_store.set.call_args[0][0]) | ||
assert_inverter_state_correct(mock_inverter_value_store.set.call_args[0][0]) | ||
|
||
|
||
API_URL = "https://sample-address" | ||
|
||
sample_session_key = { | ||
"status": "success", | ||
"auth_key": "67567d76-0c83-11ea-8a59-d84fb802005a", | ||
"regnum": "XXXXXXXXXXXX", | ||
"role": "installer" | ||
} | ||
|
||
sample_auth_key_invalid = { | ||
"auth": "auth_key failed" | ||
} | ||
|
||
sample_auth_key_valid = { | ||
"statistics": | ||
{ | ||
"pcs_pv_total_power": "0", | ||
"batconv_power": "372", | ||
"bat_use": "1", | ||
"bat_status": "2", | ||
"bat_user_soc": "14.58333", | ||
"load_power": "361", | ||
"load_today": "0.0", | ||
"grid_power": "11", | ||
"current_day_self_consumption": "94.8", | ||
"current_pv_generation_sum": "7415", | ||
"current_grid_feed_in_energy": "385" | ||
}, | ||
"direction": | ||
{ | ||
"is_direct_consuming_": "0", | ||
"is_battery_charging_": "0", | ||
"is_battery_discharging_": "1", | ||
"is_grid_selling_": "0", | ||
"is_grid_buying_": "0", | ||
"is_charging_from_grid_": "0" | ||
}, | ||
"operation": | ||
{ | ||
"status": "start", | ||
"mode": "1" | ||
}, | ||
"wintermode": | ||
{ | ||
"winter_status": "on" | ||
}, | ||
"pcs_fault": | ||
{ | ||
"pcs_status": "pcs_ok" | ||
} | ||
} |