diff --git a/decoimpact/business/utils/dataset_utils.py b/decoimpact/business/utils/dataset_utils.py index ea6fdc7f..6443c976 100644 --- a/decoimpact/business/utils/dataset_utils.py +++ b/decoimpact/business/utils/dataset_utils.py @@ -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 diff --git a/decoimpact/data/entities/data_access_layer.py b/decoimpact/data/entities/data_access_layer.py index df1c3bb6..c78148ae 100644 --- a/decoimpact/data/entities/data_access_layer.py +++ b/decoimpact/data/entities/data_access_layer.py @@ -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. \ diff --git a/tests/business/utils/test_dataset_utils.py b/tests/business/utils/test_dataset_utils.py index 76165cf8..5c19823f 100644 --- a/tests/business/utils/test_dataset_utils.py +++ b/tests/business/utils/test_dataset_utils.py @@ -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): diff --git a/tests/data/entities/test_data_access_layer.py b/tests/data/entities/test_data_access_layer.py index 334a54cf..215a2828 100644 --- a/tests/data/entities/test_data_access_layer.py +++ b/tests/data/entities/test_data_access_layer.py @@ -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) @@ -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