Coverage for tests/test_residential_demand_flex.py: 100%
26 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-07-28 21:49 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-07-28 21:49 +0000
1"""Tests for residential demand flexibility topology helpers."""
3import pandas as pd
4import pytest
5from prepare_times_nz.stage_4.baseyear.residential import (
6 add_demand_flex_intermediate_outputs,
7 get_commodity_demand,
8 get_demand_flex_topology,
9 get_intermediate_commodity_name,
10 get_model_switch,
11 parse_switch_value,
12)
15def test_get_intermediate_commodity_name_uses_region_and_tech_detail():
16 """Intermediate commodity names should retain the useful tech detail."""
17 tech_name = "RES-DD-ELC-HWATER_C-WH_LOW"
19 assert get_intermediate_commodity_name(tech_name) == "DD-HWATER_C-WH_LOW"
22def test_get_intermediate_commodity_name_handles_space_heating_heat_pumps():
23 """Residential heat pump space heating should route through HPSH detail."""
24 tech_name = "RES-JD-ELC-HPSH-S_HEAT"
26 assert get_intermediate_commodity_name(tech_name) == "JD-HPSH-S_HEAT"
29def test_demand_flex_topology_keeps_final_demand_on_original_commodity():
30 """Flexible demand topology should not move final demand to intermediates."""
31 df = pd.DataFrame(
32 {
33 "TechName": [
34 "RES-DD-ELC-HWATER_C-WH_LOW",
35 "RES-DD-ELC-HWATER_C-WH_LOW",
36 "RES-DD-LPG-HWATER_G-WH_LOW",
37 ],
38 "Comm-OUT": ["DD-WH_LOW", "DD-WH_LOW", "DD-WH_LOW"],
39 "Region": ["NI", "SI", "NI"],
40 "ACT_BND": [1.0, 2.0, 3.0],
41 }
42 )
44 topology = get_demand_flex_topology(df, {"RES-DD-ELC-HWATER_C-WH_LOW"})
45 rewritten = add_demand_flex_intermediate_outputs(df, topology)
46 demand = get_commodity_demand(df)
48 assert topology.to_dict("records") == [
49 {
50 "TechName": "RES-DD-ELC-HWATER_C-WH_LOW",
51 "Comm-OUT": "DD-WH_LOW",
52 "Comm-IN": "DD-HWATER_C-WH_LOW",
53 }
54 ]
55 assert set(rewritten["Comm-OUT"]) == {"DD-HWATER_C-WH_LOW", "DD-WH_LOW"}
56 assert "DD-HWATER_C-WH_LOW" not in set(demand["CommName"])
59def test_get_model_switch_reads_enabled_value(tmp_path):
60 """User switch CSV should control residential demand-flex intermediates."""
61 switch_file = tmp_path / "model_switches.csv"
62 switch_file.write_text(
63 "Switch,Enabled\nResidentialDemandFlexIntermediates,false\n",
64 encoding="utf-8",
65 )
67 assert not get_model_switch(
68 "ResidentialDemandFlexIntermediates", filepath=switch_file
69 )
72def test_get_model_switch_uses_default_for_missing_file(tmp_path):
73 """Missing switch files should preserve the current enabled behaviour."""
74 assert get_model_switch(
75 "ResidentialDemandFlexIntermediates", filepath=tmp_path / "missing.csv"
76 )
79def test_parse_switch_value_rejects_unclear_values():
80 """Switch values should fail loudly if they are ambiguous."""
81 with pytest.raises(ValueError):
82 parse_switch_value("maybe")