Skip to content

Commit

Permalink
fix(xpansion): catch Exception when not sensitvity folder in xpansion
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Feb 29, 2024
1 parent c491baf commit f6d5696
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 66 deletions.
2 changes: 1 addition & 1 deletion antarest/study/business/xpansion_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def get_xpansion_settings(self, study: Study) -> GetXpansionSettings:
logger.info(f"Getting xpansion settings for study '{study.id}'")
file_study = self.study_storage_service.get_storage(study).get_raw(study)
config_obj = file_study.tree.get(["user", "expansion", "settings"])
with contextlib.suppress(KeyError):
with contextlib.suppress(ChildNotFoundError):
config_obj["sensitivity_config"] = file_study.tree.get(
["user", "expansion", "sensitivity", "sensitivity_in"]
)
Expand Down
101 changes: 36 additions & 65 deletions tests/storage/business/test_xpansion_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def make_link_and_areas(empty_study: FileStudy) -> None:
make_link(empty_study)


def set_up_xpansion_manager(tmp_path: Path) -> t.Tuple[FileStudy, RawStudy, XpansionManager]:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=str(empty_study.config.study_path), version="810")
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
return empty_study, study, xpansion_manager


@pytest.mark.unit_test
@pytest.mark.parametrize(
"version, expected_output",
Expand Down Expand Up @@ -117,7 +125,7 @@ def test_create_configuration(tmp_path: Path, version: int, expected_output: JSO
Test the creation of a configuration.
"""
empty_study = make_empty_study(tmp_path, version)
study = RawStudy(id="1", path=empty_study.config.study_path, version=version)
study = RawStudy(id="1", path=str(empty_study.config.study_path), version=str(version))
xpansion_manager = make_xpansion_manager(empty_study)

with pytest.raises(ChildNotFoundError):
Expand All @@ -135,7 +143,7 @@ def test_delete_xpansion_configuration(tmp_path: Path) -> None:
Test the deletion of a configuration.
"""
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
study = RawStudy(id="1", path=str(empty_study.config.study_path), version="810")
xpansion_manager = make_xpansion_manager(empty_study)

with pytest.raises(ChildNotFoundError):
Expand Down Expand Up @@ -183,7 +191,7 @@ def test_get_xpansion_settings(tmp_path: Path, version: int, expected_output: JS
"""

empty_study = make_empty_study(tmp_path, version)
study = RawStudy(id="1", path=empty_study.config.study_path, version=version)
study = RawStudy(id="1", path=str(empty_study.config.study_path), version=str(version))
xpansion_manager = make_xpansion_manager(empty_study)

xpansion_manager.create_xpansion_configuration(study)
Expand All @@ -197,12 +205,7 @@ def test_update_xpansion_settings(tmp_path: Path) -> None:
"""
Test the retrieval of the xpansion settings.
"""

empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)

xpansion_manager.create_xpansion_configuration(study)
_, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

new_settings_obj = {
"optimality_gap": 4.0,
Expand Down Expand Up @@ -246,10 +249,7 @@ def test_update_xpansion_settings(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_add_candidate(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

actual = empty_study.tree.get(["user", "expansion", "candidates"])
assert actual == {}
Expand Down Expand Up @@ -298,10 +298,7 @@ def test_add_candidate(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_get_candidate(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

assert empty_study.tree.get(["user", "expansion", "candidates"]) == {}

Expand Down Expand Up @@ -334,10 +331,7 @@ def test_get_candidate(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_get_candidates(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

assert empty_study.tree.get(["user", "expansion", "candidates"]) == {}

Expand Down Expand Up @@ -372,10 +366,7 @@ def test_get_candidates(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_update_candidates(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

assert empty_study.tree.get(["user", "expansion", "candidates"]) == {}

Expand Down Expand Up @@ -406,10 +397,7 @@ def test_update_candidates(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_delete_candidate(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

assert empty_study.tree.get(["user", "expansion", "candidates"]) == {}

Expand Down Expand Up @@ -442,10 +430,7 @@ def test_delete_candidate(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_update_constraints(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

with pytest.raises(XpansionFileNotFoundError):
xpansion_manager.update_xpansion_constraints_settings(study=study, constraints_file_name="non_existent_file")
Expand All @@ -464,10 +449,7 @@ def test_update_constraints(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_add_resources(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "constraints1.txt"
filename2 = "constraints2.txt"
Expand Down Expand Up @@ -520,10 +502,8 @@ def test_add_resources(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_list_root_resources(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

constraints_file_content = b"0"
constraints_file_name = "unknownfile.txt"

Expand All @@ -533,10 +513,7 @@ def test_list_root_resources(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_get_single_constraints(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

constraints_file_content = b"0"
constraints_file_name = "constraints.txt"
Expand All @@ -549,12 +526,18 @@ def test_get_single_constraints(tmp_path: Path) -> None:
)


@pytest.mark.unit_test
def test_get_settings_without_sensitivity(tmp_path: Path) -> None:
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

empty_study.tree.delete(["user", "expansion", "sensitivity"])
# should not fail even if the folder doesn't exist as it's optional
xpansion_manager.get_xpansion_settings(study)


@pytest.mark.unit_test
def test_get_all_constraints(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
_, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "constraints1.txt"
filename2 = "constraints2.txt"
Expand All @@ -576,10 +559,7 @@ def test_get_all_constraints(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_add_capa(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "capa1.txt"
filename2 = "capa2.txt"
Expand Down Expand Up @@ -610,10 +590,7 @@ def test_add_capa(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_delete_capa(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
empty_study, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "capa1.txt"
filename2 = "capa2.txt"
Expand All @@ -636,10 +613,7 @@ def test_delete_capa(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_get_single_capa(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
_, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "capa1.txt"
filename2 = "capa2.txt"
Expand All @@ -664,10 +638,7 @@ def test_get_single_capa(tmp_path: Path) -> None:

@pytest.mark.unit_test
def test_get_all_capa(tmp_path: Path) -> None:
empty_study = make_empty_study(tmp_path, 810)
study = RawStudy(id="1", path=empty_study.config.study_path, version=810)
xpansion_manager = make_xpansion_manager(empty_study)
xpansion_manager.create_xpansion_configuration(study)
_, study, xpansion_manager = set_up_xpansion_manager(tmp_path)

filename1 = "capa1.txt"
filename2 = "capa2.txt"
Expand Down

0 comments on commit f6d5696

Please sign in to comment.