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

Implement test_fix_pressure_levels_reverse_pressure() #108

Open
truth-quark opened this issue Sep 24, 2024 · 0 comments
Open

Implement test_fix_pressure_levels_reverse_pressure() #108

truth-quark opened this issue Sep 24, 2024 · 0 comments

Comments

@truth-quark
Copy link
Collaborator

There is a testing gap from early work on #92, implenting test_fix_pressure_levels_reverse_pressure(). Problems occurred with testing at the boundary of um2netcdf.py & iris.util.reverse(). Instead of blocking other tests from merging, the team agreed to defer the fix to this issue.

Testing didn't cover this code branch calling an iris function:

if pressure.points[0] < pressure.points[-1]:
# Flip to get pressure decreasing as per CMIP6 standard
# NOTE: returns a new cube!
# TODO: add an iris.util.monotonic() check here?
return iris.util.reverse(cube, 'pressure')

Problems are listed in the test code (see also #94):

@pytest.mark.skip
def test_fix_pressure_levels_reverse_pressure(get_fake_cube_coords):
# TODO: test is broken due to fiddly mocking problems (see below)
m_pressure = mock.Mock()
# m_pressure.ndim = 1
_add_attrs_points(m_pressure, [0.000001, 1.000001])
extra = {"pressure": m_pressure}
cube = get_fake_cube_coords(extra)
# cube.ndim = 3
# TODO: testing gets odd here at the um2nc & iris "boundary":
# * A mock reverse() needs to flip pressure.points & return a modified cube.
# Creating a mock to verifying these attributes is unproductive.
# * Using the real reverse() requires several additional cube attributes
# (see commented out ndim etc above). It requires __getitem__() for
# https://github.com/SciTools/iris/blob/main/lib/iris/util.py#L612
#
# TODO: this leaves a few options:
# * ignore unit testing this branch (rely on integration testing?)
# * replace iris with an adapter?
# * fix/refactor the function later?
#
# The test is disabled awaiting a solution...
with mock.patch("iris.util.reverse"):
mod_cube = um2nc.fix_pressure_levels(cube)
assert mod_cube is not None
assert mod_cube != cube
c_pressure = mod_cube.coord('pressure')
assert c_pressure.attributes["positive"] == "down"
assert all(c_pressure.points == [1.0, 0.0])

The main task is determining how to implement testing without lots of fiddly mocking.

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