Skip to content

Commit

Permalink
Create func for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
charlotte-avery committed Dec 2, 2024
1 parent 2748fb4 commit c5a4cf6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
38 changes: 29 additions & 9 deletions simulation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ def check_string_is_isoformat_datetime(string) -> str:
return parser.parse_args(args)


def check_parsed_target_heat_pump_awareness(
campaigns: dict, initial_awareness: float
) -> bool:
"""Determine whether target heat pump awareness increases over the model horizon
Args:
campaigns (dict): modelled heat pump awareness campaigns
initial_awareness (float): initial (t=0) heat pump awareness
Returns:
bool: True if target awareness values are set to increase over model horizon
"""

campaigns_date_ordered = sorted(campaigns)
_, awareness_factors = zip(*campaigns_date_ordered)
awareness_factors = list(awareness_factors)
awareness_factors.insert(0, initial_awareness)

increasing_awareness = all(
[
awareness_factors[i - 1] < awareness_factors[i]
for i in range(1, len(awareness_factors))
]
)
return increasing_awareness


def validate_args(args):
if args.gas_oil_boiler_ban_announce_date > args.gas_oil_boiler_ban_date:
raise ValueError(
Expand All @@ -197,15 +224,8 @@ def validate_args(args):

if args.campaign_target_heat_pump_awareness_date is not None:
# Check that target awareness inputs increase over the model horizon
campaigns = sorted(args.campaign_target_heat_pump_awareness_date)
_, awareness_factors = zip(*campaigns)
awareness_factors = list(awareness_factors)
awareness_factors.insert(0, args.heat_pump_awareness)
increasing_awareness = all(
[
awareness_factors[i - 1] < awareness_factors[i]
for i in range(1, len(awareness_factors))
]
increasing_awareness = check_parsed_target_heat_pump_awareness(
args.campaign_target_heat_pump_awareness_date, args.heat_pump_awareness
)
if not increasing_awareness:
raise ValueError(
Expand Down
12 changes: 11 additions & 1 deletion simulation/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

import simulation.__main__
from abm import read_jsonlines
from simulation.__main__ import parse_args, validate_args
from simulation.__main__ import (
check_parsed_target_heat_pump_awareness,
parse_args,
validate_args,
)
from simulation.constants import InterventionType


Expand Down Expand Up @@ -305,5 +309,11 @@ def test_campaign_target_less_than_heat_pump_awareness_raises_value_error(
"0.5",
]
)

increasing_awareness = check_parsed_target_heat_pump_awareness(
args.campaign_target_heat_pump_awareness_date, args.heat_pump_awareness
)
assert not increasing_awareness

with pytest.raises(ValueError):
validate_args(args)

0 comments on commit c5a4cf6

Please sign in to comment.