From 2573d74597ea2fda1fd5e32653b5817b5c96c51c Mon Sep 17 00:00:00 2001 From: Marlon Vermeulen Date: Thu, 13 Apr 2023 13:31:33 +0200 Subject: [PATCH] #511 resolve issue regarding failing test linked to union of timmodel and forcingmodel --- hydrolib/core/dflowfm/tim/parser.py | 8 +++++ tests/data/input/tim/bc_file_is_incorrect.bc | 23 ++++++++++++++ ...le_data_for_timeseries_with_empty_data.tim | 3 ++ tests/dflowfm/test_tim.py | 31 +++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 tests/data/input/tim/bc_file_is_incorrect.bc create mode 100644 tests/data/input/tim/triple_data_for_timeseries_with_empty_data.tim diff --git a/hydrolib/core/dflowfm/tim/parser.py b/hydrolib/core/dflowfm/tim/parser.py index 24175864e..6ad8c158b 100644 --- a/hydrolib/core/dflowfm/tim/parser.py +++ b/hydrolib/core/dflowfm/tim/parser.py @@ -82,8 +82,11 @@ def _read_time_series_data( time, *values = line.split() + TimParser._raise_error_if_values_empty(values, line_index) + timrecord = {"time": time, "data": values} timeseries.append(timrecord) + return timeseries @staticmethod @@ -92,3 +95,8 @@ def _raise_error_if_contains_comment(line: str, line_index: int) -> None: raise ValueError( f"Line {line_index}: comments are only supported at the start of the file, before the time series data." ) + + @staticmethod + def _raise_error_if_values_empty(values: List[str], line_index: int) -> None: + if len(values) == 0: + raise ValueError(f"Line {line_index}: Time series cannot be empty.") \ No newline at end of file diff --git a/tests/data/input/tim/bc_file_is_incorrect.bc b/tests/data/input/tim/bc_file_is_incorrect.bc new file mode 100644 index 000000000..9dddb3cf5 --- /dev/null +++ b/tests/data/input/tim/bc_file_is_incorrect.bc @@ -0,0 +1,23 @@ +[forcing] +Name = global +Function = timeseries +Time-interpolation = linear +Quantity = time +Unit = minutes since 2006-12-25 0:00:00 +Quantity = rainfall_rate +Unit = mm day-1 + 0.000000 0.0000000 + 120.000000 0.0000000 + 240.000000 10.0000000 + 360.000000 31.6000000 + 480.000000 100.0000000 + 600.000000 120.0000000 + 720.000000 100.0000000 + 840.000000 70.0000000 + 960.000000 0.0000000 + 960.000000 54.0000000 +1080.000000 70.0000000 +1200.000000 35.0000000 +1320.000000 0.0000000 +1440.000000 0.0000000 +3000.000000 0.0000000 diff --git a/tests/data/input/tim/triple_data_for_timeseries_with_empty_data.tim b/tests/data/input/tim/triple_data_for_timeseries_with_empty_data.tim new file mode 100644 index 000000000..8fd0c6f61 --- /dev/null +++ b/tests/data/input/tim/triple_data_for_timeseries_with_empty_data.tim @@ -0,0 +1,3 @@ +10 +20 +30 \ No newline at end of file diff --git a/tests/dflowfm/test_tim.py b/tests/dflowfm/test_tim.py index 731d73cf6..a4002f7f7 100644 --- a/tests/dflowfm/test_tim.py +++ b/tests/dflowfm/test_tim.py @@ -314,3 +314,34 @@ def test_parse_data_throws_exception_error_parsing_tim_file_comments_between_dat expected_error_msg = f"Line {5}: comments are only supported at the start of the file, before the time series data." assert expected_error_msg in str(error.value) + + + @pytest.mark.parametrize( + "input_path", + [ + pytest.param( + Path( + test_input_dir + / "tim" + / "triple_data_for_timeseries_with_empty_data.tim" + ), + id="triple_data_for_timeseries_with_empty_data", + ), + pytest.param( + Path( + test_input_dir + / "tim" + / "bc_file_is_incorrect.bc" + ), + id="bc_file_is_incorrect", + ), + ], + ) + def test_parse_data_throws_exception_error_parsing_tim_file_values_is_empty( + self, input_path + ): + with pytest.raises(ValueError) as error: + TimParser.parse(input_path) + + expected_error_msg = f"Line {0}: Time series cannot be empty." + assert expected_error_msg in str(error.value)