Skip to content

Commit

Permalink
Add tests for CubeList.combine/combine_cubes.
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-mo committed Oct 28, 2024
1 parent f17f5ad commit 8986330
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions lib/iris/tests/unit/cube/test_CubeList.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the `iris.cube.CubeList` class."""

import pytest

# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests # isort:skip
Expand Down Expand Up @@ -714,5 +716,60 @@ def test__repr_html_():
]


class Test_combine__apis:
"""Confirm that CubeList.combine/combine_cube just call combine_cubes."""

def mock_combine_cubes(self):
def mock_call(cubes, options=None, merge_require_unique=False, **kwargs):
self.call_params = [cubes, options, merge_require_unique, kwargs]
return cubes # used to test effect of different cases

return mock_call

def test_combine(self):
cubelist = CubeList([])
args = [
mock.sentinel.options,
mock.sentinel.merge_unique,
]
kwargs = dict(
key_test_1=1,
key_test_2=2,
)
with mock.patch("iris.io.loading.combine_cubes", self.mock_combine_cubes()):
result = cubelist.combine(*args, **kwargs)
assert self.call_params == [cubelist] + args + [kwargs]
assert result == cubelist

@pytest.mark.parametrize("ncubes", [0, 1, 2], ids=["nocubes", "onecube", "ncubes"])
def test_combine_cube(self, ncubes):
"""In this case, also check behaviour for result of <1 =1 >1 cubes."""
cubelist = CubeList(
[Cube([0], long_name=f"cube_{i_cube})") for i_cube in range(ncubes)]
)
args = [
mock.sentinel.options,
mock.sentinel.merge_unique,
]
kwargs = dict(
key_test_1=1,
key_test_2=2,
)
if ncubes == 1:
with mock.patch("iris.io.loading.combine_cubes", self.mock_combine_cubes()):
result = cubelist.combine_cube(*args, **kwargs)
assert self.call_params == [cubelist] + args + [kwargs]
assert result == cubelist[0]
else:
if ncubes == 0:
msg = "'combine' returned no cubes"
else:
msg = "'combine' result is not a single cube"

with mock.patch("iris.io.loading.combine_cubes", self.mock_combine_cubes()):
with pytest.raises(ValueError, match=msg):
result = cubelist.combine_cube(*args, **kwargs)


if __name__ == "__main__":
tests.main()

0 comments on commit 8986330

Please sign in to comment.