Skip to content

Commit

Permalink
Merge pull request #127 from Deltares/feature/DEI-241-create-non-exis…
Browse files Browse the repository at this point in the history
…ting-output-folder

Feature/dei 241 create non existing output folder
  • Loading branch information
mKlapwijk authored Aug 14, 2024
2 parents 609a4ee + f3e764a commit 50d62f1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
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
10 changes: 7 additions & 3 deletions decoimpact/data/entities/data_access_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,13 @@ def write_output_file(
self._logger.log_info(f"Writing model output data to {path}")

if not Path.exists(path.parent):
message = f"""The path {path.parent} is not found. \
Make sure the output file location is valid."""
raise FileExistsError(message)
# try to make intermediate folders
Path(path.parent).mkdir(parents=True, exist_ok=True)

if not Path.exists(path.parent):
message = f"""The path {path.parent} is not found. \
Make sure the output file location is valid."""
raise FileExistsError(message)

if Path(path).suffix != ".nc":
message = f"""The file {path} is not supported. \
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():
"""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

0 comments on commit 50d62f1

Please sign in to comment.