|
| 1 | +import json |
| 2 | +from unittest.mock import Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from control import data |
| 6 | +from control.chargelog import chargelog |
| 7 | +from control.chargelog.chargelog import (ReferenceTime, _calc, _get_reference_entry, _get_reference_position, |
| 8 | + calculate_charge_cost, get_reference_time) |
| 9 | +from control.chargepoint.chargepoint import Chargepoint |
| 10 | +from control.general import General |
| 11 | +from control.optional import Optional |
| 12 | +from helpermodules import timecheck |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture(autouse=True) |
| 16 | +def data_module() -> None: |
| 17 | + data.data_init(Mock()) |
| 18 | + data.data.general_data = General() |
| 19 | + data.data.optional_data = Optional() |
| 20 | + data.data.cp_data["cp3"] = Chargepoint(3, Mock) |
| 21 | + |
| 22 | + |
| 23 | +EXPECTED_ENTRY_TODAYS_DAILY = {"timestamp": 1698880500, "date": "00:15", "cp": |
| 24 | + {"cp5": |
| 25 | + {"imported": 0, "exported": 0}, "cp3": |
| 26 | + {"imported": 17726.504, "exported": 0}, "cp4": |
| 27 | + {"imported": 0, "exported": 0}, "all": |
| 28 | + {"imported": 17726.504, "exported": 0}}, "ev": |
| 29 | + {"ev0": |
| 30 | + {"soc": 0}}, "counter": |
| 31 | + {"counter0": |
| 32 | + {"imported": 7144.779, "exported": 20.152, "grid": True}}, "pv": |
| 33 | + {"all": |
| 34 | + {"exported": 11574}, "pv1": |
| 35 | + {"exported": 11574}}, "bat": |
| 36 | + {"all": |
| 37 | + {"imported": 6.33, "exported": 2506.763, "soc": 0}, "bat2": |
| 38 | + {"imported": 6.33, "exported": 2506.763, "soc": 0}}, "sh": |
| 39 | + {}, "hc": |
| 40 | + {"all": |
| 41 | + {"imported": 111133.74099238854}}} |
| 42 | +EXPECTED_ENTRY_YESTERDAYS_DAILY = {"timestamp": 1698879000, "date": "23:50", "cp": |
| 43 | + {"cp5": |
| 44 | + {"imported": 0, "exported": 0}, "cp3": |
| 45 | + {"imported": 16767.105, "exported": 0}, "cp4": |
| 46 | + {"imported": 0, "exported": 0}, "all": |
| 47 | + {"imported": 16767.105, "exported": 0}}, "ev": |
| 48 | + {"ev0": |
| 49 | + {"soc": 0}}, "counter": |
| 50 | + {"counter0": |
| 51 | + {"imported": 6616.028, "exported": 20.152, "grid": True}}, "pv": |
| 52 | + {"all": |
| 53 | + {"exported": 10944}, "pv1": |
| 54 | + {"exported": 10944}}, "bat": |
| 55 | + {"all": |
| 56 | + {"imported": 6.33, "exported": 2506.763, "soc": 0}, "bat2": |
| 57 | + {"imported": 6.33, "exported": 2506.763, "soc": 0}}, "sh": |
| 58 | + {}, "hc": |
| 59 | + {"all": |
| 60 | + {"imported": 110943.62398798217}}} |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + "start_charging, create_log_entry, expected_timestamp", |
| 65 | + (pytest.param(1652679772, False, ReferenceTime.START, |
| 66 | + id="innerhalb der letzten Stunde angesteckt"), # "05/16/2022, 07:42:52" |
| 67 | + pytest.param(1652676052, False, ReferenceTime.MIDDLE, |
| 68 | + id="vor mehr als einer Stunde angesteckt"), # "05/16/2022, 06:40:52" |
| 69 | + pytest.param(1652676052, True, ReferenceTime.END, |
| 70 | + id="vor mehr als einer Stunde angesteckt, Ladevorgang beenden"), # "05/16/2022, 06:40:52" |
| 71 | + ) |
| 72 | +) |
| 73 | +def test_get_reference_position(start_charging: str, create_log_entry: bool, expected_timestamp: float): |
| 74 | + # setup |
| 75 | + # jetzt ist "05/16/2022, 08:40:52" |
| 76 | + cp = Chargepoint(0, Mock()) |
| 77 | + cp.data.set.log.timestamp_start_charging = start_charging |
| 78 | + |
| 79 | + # execution |
| 80 | + timestamp = _get_reference_position(cp, create_log_entry) |
| 81 | + |
| 82 | + # evaluation |
| 83 | + assert timestamp == expected_timestamp |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "reference_position, start_charging, expected_timestamp", |
| 88 | + (pytest.param(ReferenceTime.START, 1652679772, 1652679772, |
| 89 | + id="innerhalb der letzten Stunde angesteckt"), # "05/16/2022, 07:42:52" |
| 90 | + pytest.param(ReferenceTime.MIDDLE, 1652676052, 1652679652, |
| 91 | + id="vor mehr als einer Stunde angesteckt"), # "05/16/2022, 06:40:52" |
| 92 | + pytest.param(ReferenceTime.END, 1652676052, 1652680800, |
| 93 | + id="vor mehr als einer Stunde angesteckt, Ladevorgang beenden"), # "05/16/2022, 06:40:52" |
| 94 | + ) |
| 95 | +) |
| 96 | +def test_get_reference_time(reference_position: ReferenceTime, start_charging: str, expected_timestamp: float): |
| 97 | + # setup |
| 98 | + # jetzt ist "05/16/2022, 08:40:52" |
| 99 | + cp = Chargepoint(0, Mock()) |
| 100 | + cp.data.set.log.timestamp_start_charging = start_charging |
| 101 | + |
| 102 | + # execution |
| 103 | + timestamp = get_reference_time(cp, reference_position) |
| 104 | + |
| 105 | + # evaluation |
| 106 | + assert timestamp == expected_timestamp |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.parametrize( |
| 110 | + "reference_time, expected_entry", |
| 111 | + ( |
| 112 | + pytest.param(1698880731, EXPECTED_ENTRY_TODAYS_DAILY, id="Referenz-Eintrag im heutigen Log"), # 00:18 |
| 113 | + pytest.param(1698879171, EXPECTED_ENTRY_YESTERDAYS_DAILY, id="Referenz-Eintrag im gestrigen Log"), # 23:52 |
| 114 | + ) |
| 115 | +) |
| 116 | +def test_get_reference_entry(reference_time, expected_entry, monkeypatch): |
| 117 | + # setup |
| 118 | + with open("packages/control/chargelog/sample_daily_today.json", "r") as json_file: |
| 119 | + entries_todays_daily_log = json.load(json_file)["entries"] |
| 120 | + with open("packages/control/chargelog/sample_daily_yesterday.json", "r") as json_file: |
| 121 | + content_yesterday = json.load(json_file) |
| 122 | + monkeypatch.setattr(chargelog, "_get_yesterdays_daily_log", Mock(return_value=content_yesterday)) |
| 123 | + |
| 124 | + # execution |
| 125 | + entry = _get_reference_entry(entries_todays_daily_log, reference_time) |
| 126 | + |
| 127 | + # evaluation |
| 128 | + assert entry == expected_entry |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize("et_active, expected_costs", |
| 132 | + ( |
| 133 | + pytest.param(True, 1.1649, id="Et aktiv"), |
| 134 | + pytest.param(False, 2.5953, id="Et inaktiv"), |
| 135 | + )) |
| 136 | +def test_calc(et_active, expected_costs, monkeypatch): |
| 137 | + # setup |
| 138 | + mock_et_get_current_price = Mock(return_value=0.00008) |
| 139 | + monkeypatch.setattr(data.data.optional_data, "et_get_current_price", mock_et_get_current_price) |
| 140 | + |
| 141 | + # execution |
| 142 | + costs = _calc({'bat': 0.24, 'cp': 0.0, 'grid': 0.6502, 'pv': 0.1098}, 10000, et_active) |
| 143 | + |
| 144 | + # evaluation |
| 145 | + assert costs == expected_costs |
| 146 | + |
| 147 | + |
| 148 | +def test_calculate_charge_cost(monkeypatch): |
| 149 | + # integration test |
| 150 | + # setup |
| 151 | + data.data.cp_data["cp3"].data.set.log.timestamp_start_charging = 1698822760 |
| 152 | + data.data.cp_data["cp3"].data.set.log.imported_since_plugged = 1000 |
| 153 | + data.data.cp_data["cp3"].data.set.log.imported_since_mode_switch = 1000 |
| 154 | + # Mock today() to values in log-file |
| 155 | + # Thu Nov 02 2023 07:00:51 |
| 156 | + mock_today_timestamp = Mock(return_value=1698904851) |
| 157 | + monkeypatch.setattr(timecheck, "create_timestamp", mock_today_timestamp) |
| 158 | + with open("packages/control/chargelog/sample_daily_yesterday.json", "r") as json_file: |
| 159 | + content_yesterday = json.load(json_file) |
| 160 | + monkeypatch.setattr(chargelog, "_get_yesterdays_daily_log", Mock(return_value=content_yesterday)) |
| 161 | + with open("packages/control/chargelog/sample_daily_today.json", "r") as json_file: |
| 162 | + content_today = json.load(json_file) |
| 163 | + monkeypatch.setattr(chargelog, "get_todays_daily_log", Mock(return_value=content_today)) |
| 164 | + |
| 165 | + # execution |
| 166 | + calculate_charge_cost(data.data.cp_data["cp3"]) |
| 167 | + |
| 168 | + # evaluation |
| 169 | + # charged energy 2.3kWh, 45,45% Grid, 54,55% PV, Grid 0,3ct/kWh, Pv 0,15ct/kWh |
| 170 | + assert data.data.cp_data["cp3"].data.set.log.costs == 0.5023 |
0 commit comments