Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using iris cubes directly in unit tests #150

Open
blimlim opened this issue Nov 5, 2024 · 0 comments
Open

Using iris cubes directly in unit tests #150

blimlim opened this issue Nov 5, 2024 · 0 comments

Comments

@blimlim
Copy link
Collaborator

blimlim commented Nov 5, 2024

We currently rely on the DummyCube class to imitate iris cube objects in our unit tests. We've tried to imitate the cube methods that are relevant to the tests, however the real iris methods can be much more complicated than our implementations in the DummyCube class, leading to situations where our imitations don't properly match and producing tests that would fail with real cubes.

Additionally, a couple ofiris methods used in process_cubes() are too hard to try and imitate (such as iris.util.reverse). We've ended up mocking a couple of the process_cube() steps as a result and leaving some functionality untested .

It turns out that it's possible to create iris cubes from scratch:
https://github.com/SciTools/iris/blob/d1125eb613e3c49c4128e004284e572ad337cad1/lib/iris/cube.py#L1098-L1103, though with the caveat:

Not typically used - normally cubes are obtained by loading data (e.g. :func:iris.load) or from manipulating existing cubes.

Creating small iris cubes instead of DummyCubes would allow us to test previously untested functions, remove mocks, and also avoid situations where the DummyCubes don't match the real iris cube behaviour, e.g:

> test_data = np.array([1,2,3])
> test_coord = iris.coords.DimCoord(points = np.array([0,0.1,0.2]),
                              standard_name="longitude")
> test_cube = cube.Cube(data = test_data,
                   standard_name="air_temperature",
                   dim_coords_and_dims = [(test_coord,0)]
                  )

> iris.util.reverse(test_cube, 'longitude').data
array([3, 2, 1])

One downside is that the Cube appears like it's generally meant to be created by loading in data, rather than being created from scratch as above. On the other hand, iris appears to use the above approach, creating small cubes from scratch, in their unit tests: https://github.com/SciTools/iris/blob/d1125eb613e3c49c4128e004284e572ad337cad1/lib/iris/tests/unit/cube/test_Cube.py#L88-L93

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant