|
| 1 | +import re |
| 2 | +from typing import Callable, Tuple |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from modules.common.component_state import ChargepointState |
| 7 | +from modules.internal_chargepoint_handler.internal_chargepoint_handler_config import InternalChargepoint |
| 8 | +from modules.internal_chargepoint_handler.pro_plus import ProPlus |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True) |
| 12 | +def setup_pro_plus(monkeypatch) -> Tuple[ProPlus, Mock]: |
| 13 | + pro_plus = ProPlus(0, InternalChargepoint(), 1) |
| 14 | + mock_store_set = Mock() |
| 15 | + monkeypatch.setattr(pro_plus.store, "set", mock_store_set) |
| 16 | + monkeypatch.setattr(pro_plus.store, "update", lambda: None) |
| 17 | + monkeypatch.setattr(pro_plus.store_internal, "set", lambda x: None) |
| 18 | + monkeypatch.setattr(pro_plus.store_internal, "update", lambda: None) |
| 19 | + return pro_plus, mock_store_set |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture() |
| 23 | +def chargepoint_state() -> ChargepointState: |
| 24 | + return ChargepointState(currents=[0, 0, 0], powers=[0, 0, 0], voltages=[ |
| 25 | + 229.4, 229.4, 229.4], imported=0, exported=0, power=0, phases_in_use=2, charge_state=False, plug_state=True) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.parametrize( |
| 29 | + "request_values_return, expected_chargepoint_state", |
| 30 | + [pytest.param(lambda: chargepoint_state, chargepoint_state, id="Normalfall"), |
| 31 | + pytest.param(Mock(side_effect=Exception(ProPlus.NO_CONNECTION_TO_INTERNAL_CP)), |
| 32 | + chargepoint_state, id="Fehler, aber Timer noch nicht abgelaufen")]) |
| 33 | +def test_get_values(request_values_return: ChargepointState, |
| 34 | + expected_chargepoint_state: ChargepointState, |
| 35 | + setup_pro_plus: Callable[[], Tuple[ProPlus, Mock]], |
| 36 | + monkeypatch): |
| 37 | + # setup |
| 38 | + pro_plus, mock_store_set = setup_pro_plus |
| 39 | + pro_plus.old_chargepoint_state = expected_chargepoint_state |
| 40 | + monkeypatch.setattr(pro_plus, "request_values", request_values_return) |
| 41 | + monkeypatch.setattr(pro_plus.client_error_context, "error_counter_exceeded", lambda: False) |
| 42 | + |
| 43 | + # execution |
| 44 | + chargepoint_state = pro_plus.get_values(False, None) |
| 45 | + |
| 46 | + # evalutation |
| 47 | + assert chargepoint_state == expected_chargepoint_state |
| 48 | + assert mock_store_set.call_args.args[0].__dict__ == expected_chargepoint_state.__dict__ |
| 49 | + |
| 50 | + |
| 51 | +def test_get_values_no_data_since_boot(setup_pro_plus: Callable[[], Tuple[ProPlus, Mock]], monkeypatch): |
| 52 | + # setup |
| 53 | + pro_plus = setup_pro_plus[0] |
| 54 | + monkeypatch.setattr(pro_plus, "request_values", Mock(side_effect=Exception(ProPlus.NO_CONNECTION_TO_INTERNAL_CP))) |
| 55 | + monkeypatch.setattr(pro_plus.client_error_context, "error_counter_exceeded", lambda: False) |
| 56 | + |
| 57 | + # execution |
| 58 | + with pytest.raises(Exception, match=re.escape(ProPlus.NO_DATA_SINCE_BOOT)): |
| 59 | + pro_plus.get_values(False, None) |
| 60 | + |
| 61 | + |
| 62 | +def test_get_values_error_timer_exceed(setup_pro_plus: Callable[[], Tuple[ProPlus, Mock]], monkeypatch): |
| 63 | + # Exception werfen und Ladepunkt-Status zurücksetzen |
| 64 | + # setup |
| 65 | + pro_plus, mock_store_set = setup_pro_plus |
| 66 | + monkeypatch.setattr(pro_plus, "request_values", Mock(side_effect=Exception(ProPlus.NO_CONNECTION_TO_INTERNAL_CP))) |
| 67 | + monkeypatch.setattr(pro_plus.client_error_context, "error_counter_exceeded", lambda: True) |
| 68 | + |
| 69 | + # execution |
| 70 | + with pytest.raises(Exception, match=re.escape(ProPlus.NO_CONNECTION_TO_INTERNAL_CP)): |
| 71 | + pro_plus.get_values(False, None) |
| 72 | + |
| 73 | + assert mock_store_set.call_args.args[0].__dict__ == ChargepointState( |
| 74 | + plug_state=False, charge_state=False, imported=None, exported=None, |
| 75 | + phases_in_use=0, power=0, currents=[0]*3).__dict__ |
0 commit comments