Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dei 241 create non existing output folder #127

Merged
merged 8 commits into from
Aug 14, 2024
Merged
4 changes: 2 additions & 2 deletions decoimpact/business/utils/dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def get_dummy_variable_in_ugrid(dataset: _xr.Dataset) -> list:

if len(dummy) == 0:
raise ValueError(
"""No dummy variable defined and therefore input dataset does
not comply with UGrid convention."""
"No dummy variable defined and therefore input dataset does "
"not comply with UGrid convention."
)

return dummy
Expand Down
4 changes: 4 additions & 0 deletions decoimpact/data/entities/data_access_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def write_output_file(
"""
self._logger.log_info(f"Writing model output data to {path}")

if not Path.exists(path.parent):
# try to make intermediate folders
Path(path.parent).mkdir(parents=True, exist_ok=True)

if not Path.exists(path.parent):
mKlapwijk marked this conversation as resolved.
Show resolved Hide resolved
message = f"""The path {path.parent} is not found. \
Make sure the output file location is valid."""
Expand Down
4 changes: 2 additions & 2 deletions tests/business/utils/test_dataset_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ def test_get_dummy_variable_fails(self):
# Assert
assert (
error.value.args[0]
== """No dummy variable defined and therefore input dataset does
not comply with UGrid convention."""
== "No dummy variable defined and therefore input dataset does "
"not comply with UGrid convention."
)

def test_get_dummy_and_dependent_var_list(self):
Expand Down
26 changes: 1 addition & 25 deletions tests/data/entities/test_data_access_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_dataset_data_write_output_file_should_write_file():

# Arrange
logger = Mock(ILogger)
path = Path(str(get_test_data_path()) + "/results.nc")
path = Path(str(get_test_data_path()) + "abc/def/ghi" + "/results.nc")
da_layer = DataAccessLayer(logger)
data = [1]
time = pd.date_range("2020-01-01", periods=1)
Expand All @@ -117,30 +117,6 @@ def test_dataset_data_write_output_file_should_write_file():
assert path.is_file()


def test_dataset_data_write_output_file_should_check_if_path_exists():
mKlapwijk marked this conversation as resolved.
Show resolved Hide resolved
"""When calling write_output_file the provided path
needs to be checked if it exists"""

# Arrange
logger = Mock(ILogger)
path = Path("./non_existing_dir/results.nc")
da_layer = DataAccessLayer(logger)
dataset = Mock(_xr.Dataset)
application_version = "0.0.0"
application_name = "D-EcoImpact"

# Act
with pytest.raises(FileExistsError) as exc_info:
settings = OutputFileSettings(application_name, application_version)
da_layer.write_output_file(dataset, path, settings)

exception_raised = exc_info.value

# Assert
exc = exception_raised.args[0]
assert exc.endswith("Make sure the output file location is valid.")


def test_dataset_data_write_output_file_should_check_if_extension_is_correct():
"""When calling write_output_file the provided path
extension needs to be checked if it matches
Expand Down