From 7e482c78197d7789f7a27ae55d8591554d09ae21 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 24 Jul 2026 10:08:53 +0200 Subject: [PATCH 1/4] test: cover peak shaping strategies with data driven cases Peak shaping only picks between cost neutral schedules, so the objective value moves by less than the comparison tolerance. Strict cases compare the resulting schedule instead. --- AGENTS.md | 3 +- test_cases/023-attenuate-demand-peaks.json | 112 +++++++++++++++ test_cases/024-attenuate-feedin-peaks.json | 112 +++++++++++++++ test_cases/025-attenuate-grid-peaks.json | 160 +++++++++++++++++++++ tests/test_app.py | 11 ++ tests/testbench.py | 3 + 6 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 test_cases/023-attenuate-demand-peaks.json create mode 100644 test_cases/024-attenuate-feedin-peaks.json create mode 100644 test_cases/025-attenuate-grid-peaks.json diff --git a/AGENTS.md b/AGENTS.md index a40c12046..97c9cab80 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -56,7 +56,8 @@ The model is a maximization problem. Read these before changing it: - The suite runs with `uv run pytest`. - `test_cases/*.json` are loaded by `tests/test_app.py`. Each file holds a `request` and an optional `expected_response`, and the test asserts the optimizer status and, with `numpy.isclose`, the objective value. Add a scenario by dropping in a JSON file. -- For behavior the data driven harness cannot assert, such as a specific charge schedule, construct an `Optimizer` directly in a dedicated test module and assert on the returned dict. +- A test case with `"strict": true` additionally compares grid import, grid export, and the battery schedules with `numpy.allclose`. Use it for features that only pick between cost neutral alternatives, where the objective value barely moves. Do not set it on scenarios with several equally optimal schedules, the comparison would be arbitrary. +- Prefer a data driven case over a dedicated test module, so a failure points at the scenario rather than at a feature specific script. - Cover both the success and the limit violation paths. ## Writing Style diff --git a/test_cases/023-attenuate-demand-peaks.json b/test_cases/023-attenuate-demand-peaks.json new file mode 100644 index 000000000..1cd8e3d51 --- /dev/null +++ b/test_cases/023-attenuate-demand-peaks.json @@ -0,0 +1,112 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_demand_peaks" + }, + "batteries": [ + { + "charge_from_grid": true, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 4000 + ], + "c_min": 0, + "c_max": 8000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0 + ], + "ft": [ + 0, + 0, + 0, + 0 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.0, + 0.0, + 0.0, + 0.0 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": -1263.1579199999999, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 1052.6316, + 1052.6316, + 1052.6316, + 1052.6316 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 1000.0, + 2000.0, + 3000.0, + 4000.0 + ] + } + ], + "grid_import": [ + 1052.6316, + 1052.6316, + 1052.6316, + 1052.6316 + ], + "grid_export": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "flow_direction": [ + 0, + 0, + 0, + 0 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/test_cases/024-attenuate-feedin-peaks.json b/test_cases/024-attenuate-feedin-peaks.json new file mode 100644 index 000000000..252f336aa --- /dev/null +++ b/test_cases/024-attenuate-feedin-peaks.json @@ -0,0 +1,112 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_feedin_peaks" + }, + "batteries": [ + { + "charge_from_grid": false, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 4000 + ], + "c_min": 0, + "c_max": 8000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0 + ], + "ft": [ + 2000, + 6000, + 6000, + 2000 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.1, + 0.1, + 0.1, + 0.1 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": 1178.9473600000001, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 0.0, + 2105.2632, + 2105.2632, + 0.0 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 0.0, + 2000.0, + 4000.0, + 4000.0 + ] + } + ], + "grid_import": [ + 0.0, + 0.0, + 0.0, + 0.0 + ], + "grid_export": [ + 2000.0, + 3894.7368, + 3894.7368, + 2000.0 + ], + "flow_direction": [ + 1, + 1, + 1, + 1 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/test_cases/025-attenuate-grid-peaks.json b/test_cases/025-attenuate-grid-peaks.json new file mode 100644 index 000000000..df6eb6f3c --- /dev/null +++ b/test_cases/025-attenuate-grid-peaks.json @@ -0,0 +1,160 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_grid_peaks" + }, + "batteries": [ + { + "charge_from_grid": true, + "discharge_to_grid": false, + "s_capacity": 20000, + "s_min": 0, + "s_max": 20000, + "s_initial": 0, + "s_goal": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 12000 + ], + "c_min": 0, + "c_max": 2000, + "d_max": 0, + "p_a": 0 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600, + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "ft": [ + 0, + 0, + 0, + 3000, + 6000, + 6000, + 3000, + 0 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1, + 0.1 + ] + }, + "eta_c": 0.95, + "eta_d": 0.95 + }, + "expected_response": { + "status": "Optimal", + "objective_value": -389.47363999999993, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 1157.8947, + 1157.8947, + 1157.8947, + 2000.0, + 2000.0, + 2000.0, + 2000.0, + 1157.8947 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 1100.0, + 2200.0, + 3300.0, + 5200.0, + 7100.0, + 9000.0, + 10900.0, + 12000.0 + ] + } + ], + "grid_import": [ + 1157.8947, + 1157.8947, + 1157.8947, + 0.0, + 0.0, + 0.0, + 0.0, + 1157.8947 + ], + "grid_export": [ + 0.0, + 0.0, + 0.0, + 1000.0, + 4000.0, + 4000.0, + 1000.0, + 0.0 + ], + "flow_direction": [ + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 0 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file diff --git a/tests/test_app.py b/tests/test_app.py index eb3488628..f46b44d57 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -34,6 +34,17 @@ def test_optimizer(test_case: pathlib.Path): expected_objective_value, rtol=1e-05, atol=1e-08, equal_nan=False), \ f"objective value: {actual_objective_value}, expected was: {expected_objective_value}" + # cases marked strict also compare the schedule itself. needed where the feature under + # test only picks between cost neutral alternatives, which the objective value hides + if test_data.get("strict"): + for key in ("grid_import", "grid_export"): + assert numpy.allclose(response.json[key], expected_response[key], atol=1), \ + f"{key}: {response.json[key]}, expected was: {expected_response[key]}" + for i, battery in enumerate(expected_response["batteries"]): + for key in ("charging_power", "discharging_power"): + actual = response.json["batteries"][i][key] + assert numpy.allclose(actual, battery[key], atol=1), \ + f"battery {i} {key}: {actual}, expected was: {battery[key]}" def test_abort_returns_json_message(): diff --git a/tests/testbench.py b/tests/testbench.py index 4de724a32..8604db080 100644 --- a/tests/testbench.py +++ b/tests/testbench.py @@ -72,7 +72,10 @@ if response.status_code != 200: print(f"Request to optimizer returned with status {response.status_code}") sys.exit(1) + strict = test_case.get('strict', False) test_case = {} + if strict: + test_case['strict'] = strict test_case['request'] = request test_case['expected_response'] = response.get_json() json.dump(test_case, indent=4, fp=open(file_in, "w")) From fa715803bdb103d3b318aacf9ab2b9b6654f1f01 Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 24 Jul 2026 13:11:30 +0200 Subject: [PATCH 2/4] test: add case for leveling the feed-in profile below the peak The three strategy cases pin the absolute peak, but not the shape of the profile underneath it. Cover that separately: the surplus of the first step is far beyond c_max, so the export peak is pinned there and the remaining 170 Wh can be charged anywhere in the following steps without touching it. A schedule that only caps the peak therefore has a free choice, and the strict comparison pins the leveled one. Requires https://github.com/evcc-io/optimizer/pull/102: without the ramp penalty the optimizer returns grid_export [950, 50, 50, 50, 80] instead of [950, 57.5, 57.5, 57.5, 57.5]. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../026-attenuate-feedin-peaks-below-cap.json | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 test_cases/026-attenuate-feedin-peaks-below-cap.json diff --git a/test_cases/026-attenuate-feedin-peaks-below-cap.json b/test_cases/026-attenuate-feedin-peaks-below-cap.json new file mode 100644 index 000000000..b3e297c97 --- /dev/null +++ b/test_cases/026-attenuate-feedin-peaks-below-cap.json @@ -0,0 +1,117 @@ +{ + "strict": true, + "request": { + "strategy": { + "charging_strategy": "attenuate_feedin_peaks" + }, + "batteries": [ + { + "charge_from_grid": false, + "discharge_to_grid": false, + "s_capacity": 220, + "s_min": 0, + "s_max": 220, + "s_initial": 0, + "c_min": 0, + "c_max": 50, + "d_max": 0, + "p_a": 0.25 + } + ], + "time_series": { + "dt": [ + 3600, + 3600, + 3600, + 3600, + 3600 + ], + "gt": [ + 0, + 0, + 0, + 0, + 0 + ], + "ft": [ + 1000, + 100, + 100, + 100, + 100 + ], + "p_N": [ + 0.3, + 0.3, + 0.3, + 0.3, + 0.3 + ], + "p_E": [ + 0.08, + 0.08, + 0.08, + 0.08, + 0.08 + ] + }, + "eta_c": 1.0, + "eta_d": 1.0 + }, + "expected_response": { + "status": "Optimal", + "objective_value": 136.89999999999998, + "limit_violations": { + "grid_import_limit_exceeded": false, + "grid_export_limit_hit": false + }, + "batteries": [ + { + "charging_power": [ + 50.0, + 42.5, + 42.5, + 42.5, + 42.5 + ], + "discharging_power": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "state_of_charge": [ + 50.0, + 92.5, + 135.0, + 177.5, + 220.0 + ] + } + ], + "grid_import": [ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0 + ], + "grid_export": [ + 950.0, + 57.5, + 57.5, + 57.5, + 57.5 + ], + "flow_direction": [ + 1, + 1, + 1, + 1, + 1 + ], + "grid_import_overshoot": [], + "grid_export_overshoot": [] + } +} \ No newline at end of file From 9646d7f597445af1cc9f49658abfd86175249fad Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 25 Jul 2026 11:06:26 +0200 Subject: [PATCH 3/4] test: assert c_min compliance independently of stored output Add a structural invariant to test_optimizer: with c_min set, each step charges either nothing or at least c_min, except a sub-c_min top-off when the battery is essentially full. Independent of expected_response, so it catches regressions like #19 that a self-referential comparison misses. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/test_app.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index f46b44d57..371359c07 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -46,6 +46,24 @@ def test_optimizer(test_case: pathlib.Path): assert numpy.allclose(actual, battery[key], atol=1), \ f"battery {i} {key}: {actual}, expected was: {battery[key]}" + # independent invariant, not a comparison against stored output: with c_min set every step + # charges either nothing or at least c_min, except when the battery is essentially full and + # only a sub-c_min top-off remains. guards against regressions like #19, where p_demand let + # charging slip below c_min far from s_max. + if response.json["status"] == "Optimal": + dt = request["time_series"]["dt"] + for i, battery in enumerate(request["batteries"]): + c_min = battery.get("c_min", 0) + if not c_min: + continue + charging = response.json["batteries"][i]["charging_power"] + soc = response.json["batteries"][i]["state_of_charge"] + for t, charge in enumerate(charging): + step = c_min * dt[t] / 3600 + room = battery["s_max"] - soc[t] + assert charge <= 1 or charge >= step - 1 or room <= step + 1, \ + f"battery {i} t={t}: charges {charge} below c_min step {step} with {room} Wh room to s_max" + def test_abort_returns_json_message(): # message-only api.abort(400, ...) must return a JSON body, not an empty response From ebe10fd2cc8b0e9b27b5507d8f2fb9bcb7e31250 Mon Sep 17 00:00:00 2001 From: andig Date: Sat, 25 Jul 2026 11:47:37 +0200 Subject: [PATCH 4/4] test: renumber peak shaping cases after 023 collision on main --- ...ttenuate-demand-peaks.json => 024-attenuate-demand-peaks.json} | 0 ...ttenuate-feedin-peaks.json => 025-attenuate-feedin-peaks.json} | 0 ...25-attenuate-grid-peaks.json => 026-attenuate-grid-peaks.json} | 0 ...s-below-cap.json => 027-attenuate-feedin-peaks-below-cap.json} | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename test_cases/{023-attenuate-demand-peaks.json => 024-attenuate-demand-peaks.json} (100%) rename test_cases/{024-attenuate-feedin-peaks.json => 025-attenuate-feedin-peaks.json} (100%) rename test_cases/{025-attenuate-grid-peaks.json => 026-attenuate-grid-peaks.json} (100%) rename test_cases/{026-attenuate-feedin-peaks-below-cap.json => 027-attenuate-feedin-peaks-below-cap.json} (100%) diff --git a/test_cases/023-attenuate-demand-peaks.json b/test_cases/024-attenuate-demand-peaks.json similarity index 100% rename from test_cases/023-attenuate-demand-peaks.json rename to test_cases/024-attenuate-demand-peaks.json diff --git a/test_cases/024-attenuate-feedin-peaks.json b/test_cases/025-attenuate-feedin-peaks.json similarity index 100% rename from test_cases/024-attenuate-feedin-peaks.json rename to test_cases/025-attenuate-feedin-peaks.json diff --git a/test_cases/025-attenuate-grid-peaks.json b/test_cases/026-attenuate-grid-peaks.json similarity index 100% rename from test_cases/025-attenuate-grid-peaks.json rename to test_cases/026-attenuate-grid-peaks.json diff --git a/test_cases/026-attenuate-feedin-peaks-below-cap.json b/test_cases/027-attenuate-feedin-peaks-below-cap.json similarity index 100% rename from test_cases/026-attenuate-feedin-peaks-below-cap.json rename to test_cases/027-attenuate-feedin-peaks-below-cap.json