From 136b849cc99b48fe9374d49bb17c4de4d1d608f7 Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 09:23:27 +0200 Subject: [PATCH 1/8] fix formatting of error message --- decoimpact/business/utils/dataset_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/decoimpact/business/utils/dataset_utils.py b/decoimpact/business/utils/dataset_utils.py index ea6fdc7f..3f3b7fe3 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 From 1e907f0d244eacd7d7f5eba8ae521f236fb7c2a9 Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 09:55:07 +0200 Subject: [PATCH 2/8] add creation of not yet existing output folder --- decoimpact/data/entities/data_access_layer.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/decoimpact/data/entities/data_access_layer.py b/decoimpact/data/entities/data_access_layer.py index df1c3bb6..52ea32ba 100644 --- a/decoimpact/data/entities/data_access_layer.py +++ b/decoimpact/data/entities/data_access_layer.py @@ -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): message = f"""The path {path.parent} is not found. \ Make sure the output file location is valid.""" From 74ad6c7413ce9894700cd660dcf29e30714db66f Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 10:00:07 +0200 Subject: [PATCH 3/8] remove test since error message should no longer appear. --- tests/data/entities/test_data_access_layer.py | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/tests/data/entities/test_data_access_layer.py b/tests/data/entities/test_data_access_layer.py index 334a54cf..5bb0fb7e 100644 --- a/tests/data/entities/test_data_access_layer.py +++ b/tests/data/entities/test_data_access_layer.py @@ -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 From d8070414dbd1fdbd416122f4a7036356cab2c11d Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 10:38:05 +0200 Subject: [PATCH 4/8] fix unit test --- tests/business/utils/test_dataset_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/business/utils/test_dataset_utils.py b/tests/business/utils/test_dataset_utils.py index 76165cf8..7afdec15 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): From 90d81ac6b99cdcc55f5e4b44f3f00b3f053cee18 Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 13:59:48 +0200 Subject: [PATCH 5/8] change triple quotes to single --- decoimpact/business/utils/dataset_utils.py | 4 ++-- tests/business/utils/test_dataset_utils.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/decoimpact/business/utils/dataset_utils.py b/decoimpact/business/utils/dataset_utils.py index 3f3b7fe3..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/tests/business/utils/test_dataset_utils.py b/tests/business/utils/test_dataset_utils.py index 7afdec15..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): From feaf078c9315aae9629d43424f77938539a0e5b3 Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 14:01:21 +0200 Subject: [PATCH 6/8] ensure also is tested that a new folder can be created for the output data --- tests/data/entities/test_data_access_layer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/entities/test_data_access_layer.py b/tests/data/entities/test_data_access_layer.py index 5bb0fb7e..dded0a75 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" + "/results.nc") da_layer = DataAccessLayer(logger) data = [1] time = pd.date_range("2020-01-01", periods=1) From 36633f1b0e42ee424d1c5c0265f3486bc9da110f Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Tue, 13 Aug 2024 14:01:52 +0200 Subject: [PATCH 7/8] add nested folders --- tests/data/entities/test_data_access_layer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/entities/test_data_access_layer.py b/tests/data/entities/test_data_access_layer.py index dded0a75..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()) + "abc" + "/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) From f3e764ac44ca7f8dcc66b27d8214cc86e376ef2b Mon Sep 17 00:00:00 2001 From: mKlapwijk Date: Wed, 14 Aug 2024 13:55:33 +0200 Subject: [PATCH 8/8] move second check if path exist one indentation --- decoimpact/data/entities/data_access_layer.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/decoimpact/data/entities/data_access_layer.py b/decoimpact/data/entities/data_access_layer.py index 52ea32ba..c78148ae 100644 --- a/decoimpact/data/entities/data_access_layer.py +++ b/decoimpact/data/entities/data_access_layer.py @@ -145,10 +145,10 @@ def write_output_file( # 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 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. \