Skip to content

Commit 21d4422

Browse files
committed
Updated to use pytest to check the summary matches the expected values
1 parent b6dedd1 commit 21d4422

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

divert_sim/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest

divert_sim/run_tests.py renamed to divert_sim/test_divert.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# PYTHON_ARGCOMPLETE_OK
33

4-
from os import EX_OK, path
4+
from os import path
55
from subprocess import PIPE, Popen
66
from datetime import datetime
77

@@ -20,7 +20,18 @@
2020
OPENEVSE_STATE_SLEEPING = 4
2121
OPENEVSE_STATE_DISABLED = 5
2222

23-
def divert_test(summary_file, dataset: str, config: bool = False, grid_ie_col: bool = False,
23+
KWH_ROUNDING = 2
24+
25+
def run_test_with_dataset(summary_file, dataset: str,
26+
expected_solar_kwh: float,
27+
expected_ev_kwh : float,
28+
expected_kwh_from_solar : float,
29+
expected_kwh_from_grid : float,
30+
expected_number_of_charges: int,
31+
expected_min_time_charging: int,
32+
expected_max_time_charging: int,
33+
expected_total_time_charging: int,
34+
config: bool = False, grid_ie_col: bool = False,
2435
solar_col: bool = False, voltage_col: bool = False,
2536
separator: str = ',', is_kw: bool = False) -> None:
2637
"""Run the divert_sim process on the given dataset and return the results"""
@@ -120,29 +131,52 @@ def divert_test(summary_file, dataset: str, config: bool = False, grid_ie_col: b
120131

121132
summary_file.write(f'"{dataset}",{solar_kwh},{ev_kwh},{kwh_from_solar},{kwh_from_grid},{number_of_charges},{min_time_charging},{max_time_charging},{total_time_charging}\n')
122133

134+
assert round(solar_kwh, KWH_ROUNDING) == expected_solar_kwh
135+
assert round(ev_kwh, KWH_ROUNDING) == expected_ev_kwh
136+
assert round(kwh_from_solar, KWH_ROUNDING) == expected_kwh_from_solar
137+
assert round(kwh_from_grid, KWH_ROUNDING) == expected_kwh_from_grid
138+
assert number_of_charges == expected_number_of_charges
139+
assert min_time_charging == expected_min_time_charging
140+
assert max_time_charging == expected_max_time_charging
141+
assert total_time_charging == expected_total_time_charging
123142

124-
def main() -> int:
143+
144+
def test_divert() -> None:
125145
"""Run the divert_sim process on all the datasets in the data directory"""
126146
with open(path.join('output', 'summary.csv'), 'w', encoding="utf-8") as summary_file:
127147
summary_file.write('"Dataset","Total Solar (kWh)","Total EV Charge (kWh)","Charge from solar (kWh)","Charge from grid (kWh)","Number of charges","Min time charging","Max time charging","Total time charging"\n')
128-
divert_test(summary_file, 'almostperfect')
129-
divert_test(summary_file, 'CloudyMorning')
130-
divert_test(summary_file, 'day1')
131-
divert_test(summary_file, 'day2')
132-
divert_test(summary_file, 'day3')
133-
divert_test(summary_file, 'day1_grid_ie', grid_ie_col=2)
134-
divert_test(summary_file, 'day2_grid_ie', grid_ie_col=2)
135-
divert_test(summary_file, 'day3_grid_ie', grid_ie_col=2)
136-
divert_test(summary_file, 'solar-vrms', voltage_col=2)
137-
divert_test(summary_file, 'Energy_and_Power_Day_2020-03-22', separator=';',
138-
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
139-
divert_test(summary_file, 'Energy_and_Power_Day_2020-03-31', separator=';',
140-
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
141-
divert_test(summary_file, 'Energy_and_Power_Day_2020-04-01', separator=';',
142-
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
143-
144-
return EX_OK
148+
run_test_with_dataset(summary_file, 'almostperfect',
149+
21.08, 17.24, 17.05, 0.19, 1, 30060, 30060, 30060)
150+
run_test_with_dataset(summary_file, 'CloudyMorning',
151+
16.64, 13.03, 12.67, 0.36, 1, 22200, 22200, 22200)
152+
run_test_with_dataset(summary_file, 'day1',
153+
10.12, 7.48, 6.71, 0.77, 5, 660, 10080, 13740)
154+
run_test_with_dataset(summary_file, 'day2',
155+
12.35, 9.88, 9.86, 0.02, 1, 19920, 19920, 19920)
156+
run_test_with_dataset(summary_file, 'day3',
157+
5.09, 2.22, 1.60, 0.62, 5, 660, 2400, 5340)
158+
run_test_with_dataset(summary_file, 'day1_grid_ie',
159+
15.13, 8.83, 8.47, 0.36, 5, 660, 7800, 19860,
160+
grid_ie_col=2)
161+
run_test_with_dataset(summary_file, 'day2_grid_ie',
162+
10.85, 7.66, 6.16, 1.50, 10, 420, 7980, 16260,
163+
grid_ie_col=2)
164+
run_test_with_dataset(summary_file, 'day3_grid_ie',
165+
12.13, 6.32, 6.27, 0.05, 2, 3660, 9840, 13500,
166+
grid_ie_col=2)
167+
run_test_with_dataset(summary_file, 'solar-vrms',
168+
13.85, 12.26, 12.10, 0.17, 1, 22080, 22080, 22080,
169+
voltage_col=2)
170+
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-03-22',
171+
41.87, 38.52, 38.41, 0.11, 1, 28800, 28800, 28800,
172+
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
173+
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-03-31',
174+
23.91, 18.78, 18.66, 0.12, 1, 22500, 22500, 22500,
175+
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
176+
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-04-01',
177+
38.89, 36.72, 36.41, 0.32, 1, 27000, 27000, 27000,
178+
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
145179

146180
if __name__ == '__main__':
147181
# Run the script
148-
exit(main())
182+
test_divert()

0 commit comments

Comments
 (0)