Skip to content

Commit 62e5567

Browse files
add new unit tests for errors and warnings in stitchee.py
1 parent ca235ac commit 62e5567

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

concatenator/stitchee.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _load_and_sort_datatrees(
164164
if sorting_variable:
165165
try:
166166
sort_value = datatree[sorting_variable].values.flatten()[0]
167-
except Exception as err:
167+
except KeyError as err:
168168
logger.error(
169169
f"Cannot extract sorting value from '{sorting_variable}' in {filepath}: {err}"
170170
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""Tests for concatenation errors."""
2+
3+
# pylint: disable=C0116
4+
5+
import os
6+
7+
import pytest
8+
9+
from concatenator.stitchee import stitchee
10+
11+
12+
def test_no_files_to_concatenate(temp_output_dir):
13+
with pytest.raises(ValueError, match="files_to_concat cannot be empty"):
14+
stitchee([], output_file=os.path.join(temp_output_dir, "blank.nc"))
15+
16+
17+
def test_invalid_concat_method(temp_output_dir, toy_null_dataset):
18+
with pytest.raises(ValueError, match="Unexpected concatenation method"):
19+
stitchee(
20+
[toy_null_dataset],
21+
output_file=os.path.join(temp_output_dir, "blank.nc"),
22+
concat_method="unknown-concat",
23+
)
24+
25+
26+
def test_concat_dim_error_not_supplied_when_required(temp_output_dir, toy_null_dataset):
27+
with pytest.raises(
28+
ValueError, match="concat_dim is required when using 'xarray-concat' method"
29+
):
30+
stitchee(
31+
[toy_null_dataset],
32+
output_file=os.path.join(temp_output_dir, "blank.nc"),
33+
concat_method="xarray-concat",
34+
)
35+
36+
37+
def test_concat_dim_warning_when_supplied_but_not_used(temp_output_dir, toy_null_dataset):
38+
with pytest.warns(UserWarning, match="'concat_dim' was specified but will not be used"):
39+
stitchee(
40+
[toy_null_dataset],
41+
output_file=os.path.join(temp_output_dir, "blank.nc"),
42+
concat_method="xarray-combine",
43+
concat_dim="some_dim",
44+
)
45+
46+
47+
def test_concat_sorting_variable_not_present_in_data(
48+
temp_output_dir, ds_3dims_3vars_3coords_1group_part1, ds_3dims_3vars_3coords_1group_part2
49+
):
50+
with pytest.raises(KeyError):
51+
stitchee(
52+
[ds_3dims_3vars_3coords_1group_part1, ds_3dims_3vars_3coords_1group_part2],
53+
output_file=os.path.join(temp_output_dir, "blank.nc"),
54+
concat_dim="step",
55+
sorting_variable="unknown_variable",
56+
)

0 commit comments

Comments
 (0)