Skip to content

Commit

Permalink
Updated to use pytest to check the summary matches the expected values
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypoulter committed May 6, 2023
1 parent b6dedd1 commit 21d4422
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
1 change: 1 addition & 0 deletions divert_sim/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
76 changes: 55 additions & 21 deletions divert_sim/run_tests.py → divert_sim/test_divert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

from os import EX_OK, path
from os import path
from subprocess import PIPE, Popen
from datetime import datetime

Expand All @@ -20,7 +20,18 @@
OPENEVSE_STATE_SLEEPING = 4
OPENEVSE_STATE_DISABLED = 5

def divert_test(summary_file, dataset: str, config: bool = False, grid_ie_col: bool = False,
KWH_ROUNDING = 2

def run_test_with_dataset(summary_file, dataset: str,
expected_solar_kwh: float,
expected_ev_kwh : float,
expected_kwh_from_solar : float,
expected_kwh_from_grid : float,
expected_number_of_charges: int,
expected_min_time_charging: int,
expected_max_time_charging: int,
expected_total_time_charging: int,
config: bool = False, grid_ie_col: bool = False,
solar_col: bool = False, voltage_col: bool = False,
separator: str = ',', is_kw: bool = False) -> None:
"""Run the divert_sim process on the given dataset and return the results"""
Expand Down Expand Up @@ -120,29 +131,52 @@ def divert_test(summary_file, dataset: str, config: bool = False, grid_ie_col: b

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')

assert round(solar_kwh, KWH_ROUNDING) == expected_solar_kwh
assert round(ev_kwh, KWH_ROUNDING) == expected_ev_kwh
assert round(kwh_from_solar, KWH_ROUNDING) == expected_kwh_from_solar
assert round(kwh_from_grid, KWH_ROUNDING) == expected_kwh_from_grid
assert number_of_charges == expected_number_of_charges
assert min_time_charging == expected_min_time_charging
assert max_time_charging == expected_max_time_charging
assert total_time_charging == expected_total_time_charging

def main() -> int:

def test_divert() -> None:
"""Run the divert_sim process on all the datasets in the data directory"""
with open(path.join('output', 'summary.csv'), 'w', encoding="utf-8") as summary_file:
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')
divert_test(summary_file, 'almostperfect')
divert_test(summary_file, 'CloudyMorning')
divert_test(summary_file, 'day1')
divert_test(summary_file, 'day2')
divert_test(summary_file, 'day3')
divert_test(summary_file, 'day1_grid_ie', grid_ie_col=2)
divert_test(summary_file, 'day2_grid_ie', grid_ie_col=2)
divert_test(summary_file, 'day3_grid_ie', grid_ie_col=2)
divert_test(summary_file, 'solar-vrms', voltage_col=2)
divert_test(summary_file, 'Energy_and_Power_Day_2020-03-22', separator=';',
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
divert_test(summary_file, 'Energy_and_Power_Day_2020-03-31', separator=';',
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
divert_test(summary_file, 'Energy_and_Power_Day_2020-04-01', separator=';',
is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')

return EX_OK
run_test_with_dataset(summary_file, 'almostperfect',
21.08, 17.24, 17.05, 0.19, 1, 30060, 30060, 30060)
run_test_with_dataset(summary_file, 'CloudyMorning',
16.64, 13.03, 12.67, 0.36, 1, 22200, 22200, 22200)
run_test_with_dataset(summary_file, 'day1',
10.12, 7.48, 6.71, 0.77, 5, 660, 10080, 13740)
run_test_with_dataset(summary_file, 'day2',
12.35, 9.88, 9.86, 0.02, 1, 19920, 19920, 19920)
run_test_with_dataset(summary_file, 'day3',
5.09, 2.22, 1.60, 0.62, 5, 660, 2400, 5340)
run_test_with_dataset(summary_file, 'day1_grid_ie',
15.13, 8.83, 8.47, 0.36, 5, 660, 7800, 19860,
grid_ie_col=2)
run_test_with_dataset(summary_file, 'day2_grid_ie',
10.85, 7.66, 6.16, 1.50, 10, 420, 7980, 16260,
grid_ie_col=2)
run_test_with_dataset(summary_file, 'day3_grid_ie',
12.13, 6.32, 6.27, 0.05, 2, 3660, 9840, 13500,
grid_ie_col=2)
run_test_with_dataset(summary_file, 'solar-vrms',
13.85, 12.26, 12.10, 0.17, 1, 22080, 22080, 22080,
voltage_col=2)
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-03-22',
41.87, 38.52, 38.41, 0.11, 1, 28800, 28800, 28800,
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-03-31',
23.91, 18.78, 18.66, 0.12, 1, 22500, 22500, 22500,
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')
run_test_with_dataset(summary_file, 'Energy_and_Power_Day_2020-04-01',
38.89, 36.72, 36.41, 0.32, 1, 27000, 27000, 27000,
separator=';', is_kw=True, config='{"divert_decay_smoothing_factor":0.4}')

if __name__ == '__main__':
# Run the script
exit(main())
test_divert()

0 comments on commit 21d4422

Please sign in to comment.