diff --git a/.github/workflows/divert_sim.yaml b/.github/workflows/divert_sim.yaml index c7946f1f..9ed93a28 100644 --- a/.github/workflows/divert_sim.yaml +++ b/.github/workflows/divert_sim.yaml @@ -94,7 +94,12 @@ jobs: with: name: test_results path: | - ${{ github.ref_name }}_test_results.pdf - ${{ github.ref_name }}_test_results.png OpenEVSE_WiFi/divert_sim/output OpenEVSE_WiFi/divert_sim/view.html + + - name: Publish Unit Test Results + uses: EnricoMi/publish-unit-test-result-action/composite@v2 + if: always() + with: + junit_files: output/*.xml + check_name: Test Results diff --git a/divert_sim/test_divert.py b/divert_sim/test_divert.py index f710275e..601eef5a 100644 --- a/divert_sim/test_divert.py +++ b/divert_sim/test_divert.py @@ -2,6 +2,7 @@ # PYTHON_ARGCOMPLETE_OK from os import path +import os from subprocess import PIPE, Popen from datetime import datetime @@ -22,7 +23,15 @@ KWH_ROUNDING = 2 -def run_test_with_dataset(summary_file, dataset: str, +def setup(): + """Create the output directory""" + print("Setting up test environment") + if not path.exists('output'): + os.mkdir('output') + 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') + +def run_test_with_dataset(dataset: str, expected_solar_kwh: float, expected_ev_kwh : float, expected_kwh_from_solar : float, @@ -129,7 +138,8 @@ def run_test_with_dataset(summary_file, dataset: str, kwh_from_solar=wh_from_solar / 1000 kwh_from_grid=wh_from_grid / 1000 - 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') + with open(path.join('output', 'summary.csv'), 'a', encoding="utf-8") as summary_file: + 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 @@ -140,43 +150,84 @@ def run_test_with_dataset(summary_file, dataset: str, assert max_time_charging == expected_max_time_charging assert total_time_charging == expected_total_time_charging - -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') - 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}') +def test_divert_almostperfect() -> None: + """Run the divert test with the almostperfect dataset""" + run_test_with_dataset('almostperfect', + 21.08, 17.24, 17.05, 0.19, 1, 30060, 30060, 30060) + +def test_divert_CloudyMorning() -> None: + """Run the divert test with the CloudyMorning dataset""" + run_test_with_dataset('CloudyMorning', + 16.64, 13.03, 12.67, 0.36, 1, 22200, 22200, 22200) + +def test_divert_day1() -> None: + """Run the divert test with the day1 dataset""" + run_test_with_dataset('day1', + 10.12, 7.48, 6.71, 0.77, 5, 660, 10080, 13740) + +def test_divert_day2() -> None: + """Run the divert test with the day2 dataset""" + run_test_with_dataset('day2', + 12.35, 9.88, 9.86, 0.02, 1, 19920, 19920, 19920) + +def test_divert_day3() -> None: + """Run the divert test with the day3 dataset""" + run_test_with_dataset('day3', + 5.09, 2.22, 1.60, 0.62, 5, 660, 2400, 5340) + +def test_divert_day1_grid_ie() -> None: + """Run the divert test with the day1_grid_ie dataset""" + run_test_with_dataset('day1_grid_ie', + 15.13, 8.83, 8.47, 0.36, 5, 660, 7800, 19860, + grid_ie_col=2) + +def test_divert_day2_grid_ie() -> None: + """Run the divert test with the day2_grid_ie dataset""" + run_test_with_dataset('day2_grid_ie', + 10.85, 7.66, 6.16, 1.50, 10, 420, 7980, 16260, + grid_ie_col=2) + +def test_divert_day3_grid_ie() -> None: + """Run the divert test with the day3_grid_ie dataset""" + run_test_with_dataset('day3_grid_ie', + 12.13, 6.32, 6.27, 0.05, 2, 3660, 9840, 13500, + grid_ie_col=2) + +def test_divert_solar_vrms() -> None: + """Run the divert test with the solar-vrms dataset""" + run_test_with_dataset('solar-vrms', + 13.85, 12.26, 12.10, 0.17, 1, 22080, 22080, 22080, + voltage_col=2) + +def test_divert_Energy_and_Power_Day_2020_03_22() -> None: + """Run the divert test with the Energy_and_Power_Day_2020-03-22 dataset""" + run_test_with_dataset('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}') + +def test_divert_Energy_and_Power_Day_2020_03_31() -> None: + """Run the divert test with the Energy_and_Power_Day_2020-03-31 dataset""" + run_test_with_dataset('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}') + +def test_divert_Energy_and_Power_Day_2020_04_01() -> None: + """Run the divert test with the Energy_and_Power_Day_2020-04-01 dataset""" + run_test_with_dataset('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 - test_divert() + test_divert_almostperfect() + test_divert_CloudyMorning() + test_divert_day1() + test_divert_day2() + test_divert_day3() + test_divert_day1_grid_ie() + test_divert_day2_grid_ie() + test_divert_day3_grid_ie() + test_divert_solar_vrms() + test_divert_Energy_and_Power_Day_2020_03_22() + test_divert_Energy_and_Power_Day_2020_03_31() + test_divert_Energy_and_Power_Day_2020_04_01()