Skip to content

Commit

Permalink
Merge pull request #428 from GermanZero-de/error-handling-make_entries
Browse files Browse the repository at this point in the history
Error handling make entries
  • Loading branch information
Jeniffere authored Jan 6, 2025
2 parents 6b29017 + c771899 commit d8274e6
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 9 deletions.
27 changes: 21 additions & 6 deletions commands/arguments.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import argparse
from typing import Any

from climatevision.generator import RefData
from climatevision.generator.years import (
YEAR_REF_DEFAULT,
YEAR_REF_CHOICES,
YEAR_REF_HELP,
YEAR_BASELINE_DEFAULT,
YEAR_BASELINE_CHOICES,
YEAR_BASELINE_DEFAULT,
YEAR_BASELINE_HELP,
YEAR_TARGET_DEFAULT,
YEAR_REF_CHOICES,
YEAR_REF_DEFAULT,
YEAR_REF_HELP,
YEAR_TARGET_CHOICES,
YEAR_TARGET_DEFAULT,
YEAR_TARGET_HELP,
)


class DynamicChoicesAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
year_ref = getattr(namespace, "year_ref", 2018) # Default to 2018 if not set
ags_master = RefData.load(int(year_ref)).ags_master()
if values not in ags_master:
raise argparse.ArgumentError(
self,
f"Invalid choice: {values!r}. Valid choices include: {', '.join(list(ags_master)[:10])}, ...",
)

setattr(namespace, self.dest, values)


def add_ags_argument(parser: Any):
parser.add_argument("-ags", default="03159016")
parser.add_argument("-ags", default="03159016", action=DynamicChoicesAction)


def add_year_ref_argument(parser: Any):
Expand Down
3 changes: 2 additions & 1 deletion src/climatevision/generator/makeentries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# pyright: strict

from .entries import Entries
from .utils import div
from .refdata import RefData, Row
from .utils import div
from .years import YEAR_BASELINE_CHOICES, YEAR_TARGET_CHOICES


Expand All @@ -11,6 +11,7 @@ def make_entries(
) -> Entries:
assert year_baseline in YEAR_BASELINE_CHOICES
assert year_target in YEAR_TARGET_CHOICES
assert ags in data.ags_master()

# ags identifies the community (Kommune)
ags_dis = ags[:5] # This identifies the administrative district (Landkreis)
Expand Down
106 changes: 104 additions & 2 deletions tests/test_devtool_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os

import pytest

from devtool import Devtool

filePath = "test.txt"
Expand All @@ -23,7 +25,19 @@ def test_cmd_run_with_parameters():
assert False, "file " + filePath + " already exists"

check_cmd(
["run", "-ags", "10000000", "-year_target", "2040", "-o", filePath],
[
"run",
"-ags",
"10000000",
"-year_target",
"2040",
"-year_ref",
"2021",
"-year_baseline",
"2022",
"-o",
filePath,
],
"run",
True,
)
Expand Down Expand Up @@ -51,6 +65,8 @@ def test_cmd_make_entries_with_parameters():
"2040",
"-year_ref",
"2021",
"-year_baseline",
"2022",
"-o",
filePath,
],
Expand All @@ -64,6 +80,92 @@ def test_cmd_make_entries_with_parameters():
assert False, "file " + filePath + " was not created"


def test_cmd_make_entries_with_invalid_ags():
with pytest.raises(SystemExit) as excinfo:
check_cmd(
[
"make",
"-ags",
"12345678", # Invalid AGS value
"-year_target",
"2040",
"-year_ref",
"2018",
"-o",
"dummy_output_path",
],
"make",
False,
)

assert excinfo.value.code == 2


def test_cmd_make_entries_with_invalid_target_year():
with pytest.raises(SystemExit) as excinfo:
check_cmd(
[
"make",
"-ags",
"10000000",
"-year_target",
"2100", # Invalid target year
"-year_ref",
"2018",
"-o",
filePath,
],
"make",
False,
)

assert excinfo.value.code == 2


def test_cmd_make_entries_with_invalid_ref_year():
with pytest.raises(SystemExit) as excinfo:
check_cmd(
[
"make",
"-ags",
"10000000",
"-year_target",
"2040",
"-year_ref",
"1999", # Invalid ref year
"-o",
filePath,
],
"make",
False,
)

assert excinfo.value.code == 2


def test_cmd_make_entries_with_invalid_baseline_year():
with pytest.raises(SystemExit) as excinfo:
check_cmd(
[
"make",
"-ags",
"10000000",
"-year_target",
"2040",
"-year_ref",
"2018",
"-year_baseline",
"1999", # Invalid baseline year
"-o",
filePath,
],
"make",
False,
)

assert excinfo.value.code == 2


def test_cmd_explorer():
check_cmd(
["explorer"],
Expand Down Expand Up @@ -124,7 +226,7 @@ def test_cmd_test_end_to_end_update_expectations():
check_cmd(
["test_end_to_end", "update_expectations"],
"update_expectations",
False # You shouldn't run this as it will update expectations.
False, # You shouldn't run this as it will update expectations.
# We only want people to do so when they have thought about it first.
)

Expand Down

0 comments on commit d8274e6

Please sign in to comment.