Skip to content

Commit 6e5eb70

Browse files
committed
Update tests for CESM2
1 parent d56664d commit 6e5eb70

2 files changed

Lines changed: 36 additions & 119 deletions

File tree

tests/integration/cmor/_fixes/cmip6/test_cesm2.py

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for the fixes of CESM2."""
22

3-
import os
4-
import unittest.mock
3+
from __future__ import annotations
4+
5+
from collections.abc import Sequence
6+
from typing import TYPE_CHECKING
57

68
import iris
79
import iris.cube
@@ -27,7 +29,10 @@
2729
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
2830
from esmvalcore.cmor._fixes.fix import GenericFix
2931
from esmvalcore.cmor.fix import Fix
30-
from esmvalcore.cmor.table import get_var_info
32+
from esmvalcore.cmor.table import VariableInfo, get_var_info
33+
34+
if TYPE_CHECKING:
35+
from pathlib import Path
3136

3237

3338
def test_get_cl_fix():
@@ -66,11 +71,7 @@ def test_get_cl_fix():
6671
)
6772

6873

69-
@unittest.mock.patch(
70-
"esmvalcore.cmor._fixes.cmip6.cesm2.Fix.get_fixed_filepath",
71-
autospec=True,
72-
)
73-
def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
74+
def test_cl_fix_file(tmp_path: Path, test_data_path: Path) -> None:
7475
"""Test ``fix_file`` for ``cl``."""
7576
nc_path = test_data_path / "cesm2_cl.nc"
7677
cubes = iris.load(str(nc_path))
@@ -89,24 +90,17 @@ def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
8990
assert not raw_cube.coords("air_pressure")
9091

9192
# Apply fix
92-
mock_get_filepath.return_value = os.path.join(
93-
tmp_path,
94-
"fixed_cesm2_cl.nc",
95-
)
96-
fix = Cl(None)
97-
fixed_file = fix.fix_file(nc_path, tmp_path)
98-
mock_get_filepath.assert_called_once_with(
99-
tmp_path,
100-
nc_path,
101-
add_unique_suffix=False,
102-
)
103-
fixed_cubes = iris.load(fixed_file)
104-
assert len(fixed_cubes) == 2
105-
var_names = [cube.var_name for cube in fixed_cubes]
106-
assert "cl" in var_names
107-
assert "ps" in var_names
108-
fixed_cl_cube = fixed_cubes.extract_cube(
109-
"cloud_area_fraction_in_atmosphere_layer",
93+
vardef = get_var_info("CMIP6", "Amon", "cl")
94+
assert isinstance(vardef, VariableInfo)
95+
fix = Cl(vardef)
96+
fixed_cubes = fix.fix_file(nc_path, tmp_path)
97+
assert isinstance(fixed_cubes, Sequence)
98+
assert len(fixed_cubes) == 1
99+
fixed_cl_cube = fixed_cubes[0]
100+
assert fixed_cl_cube.var_name == "cl"
101+
assert (
102+
fixed_cl_cube.standard_name
103+
== "cloud_area_fraction_in_atmosphere_layer"
110104
)
111105
fixed_air_pressure_coord = fixed_cl_cube.coord("air_pressure")
112106
assert fixed_air_pressure_coord.points is not None
@@ -121,80 +115,6 @@ def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
121115
)
122116

123117

124-
@pytest.fixture
125-
def cl_cubes():
126-
"""``cl`` cube."""
127-
time_coord = iris.coords.DimCoord(
128-
[0.0, 1.0],
129-
var_name="time",
130-
standard_name="time",
131-
units="days since 1850-01-01 00:00:00",
132-
)
133-
a_coord = iris.coords.AuxCoord(
134-
[0.1, 0.2, 0.1],
135-
bounds=[[0.0, 0.15], [0.15, 0.25], [0.25, 0.0]],
136-
var_name="a",
137-
units="1",
138-
)
139-
b_coord = iris.coords.AuxCoord(
140-
[0.9, 0.3, 0.1],
141-
bounds=[[1.0, 0.8], [0.8, 0.25], [0.25, 0.0]],
142-
var_name="b",
143-
units="1",
144-
)
145-
lev_coord = iris.coords.DimCoord(
146-
[999.0, 99.0, 9.0],
147-
var_name="lev",
148-
standard_name="atmosphere_hybrid_sigma_pressure_coordinate",
149-
units="hPa",
150-
attributes={"positive": "up"},
151-
)
152-
lat_coord = iris.coords.DimCoord(
153-
[0.0, 1.0],
154-
var_name="lat",
155-
standard_name="latitude",
156-
units="degrees",
157-
)
158-
lon_coord = iris.coords.DimCoord(
159-
[0.0, 1.0],
160-
var_name="lon",
161-
standard_name="longitude",
162-
units="degrees",
163-
)
164-
coord_specs = [
165-
(time_coord, 0),
166-
(lev_coord, 1),
167-
(lat_coord, 2),
168-
(lon_coord, 3),
169-
]
170-
cube = iris.cube.Cube(
171-
np.arange(2 * 3 * 2 * 2).reshape(2, 3, 2, 2),
172-
var_name="cl",
173-
standard_name="cloud_area_fraction_in_atmosphere_layer",
174-
units="%",
175-
dim_coords_and_dims=coord_specs,
176-
aux_coords_and_dims=[(a_coord, 1), (b_coord, 1)],
177-
)
178-
return iris.cube.CubeList([cube])
179-
180-
181-
def test_cl_fix_metadata(cl_cubes):
182-
"""Test ``fix_metadata`` for ``cl``."""
183-
vardef = get_var_info("CMIP6", "Amon", "cl")
184-
fix = Cl(vardef)
185-
out_cubes = fix.fix_metadata(cl_cubes)
186-
out_cube = out_cubes.extract_cube(
187-
"cloud_area_fraction_in_atmosphere_layer",
188-
)
189-
lev_coord = out_cube.coord(var_name="lev")
190-
assert lev_coord.units == "1"
191-
np.testing.assert_allclose(lev_coord.points, [1.0, 0.5, 0.2])
192-
np.testing.assert_allclose(
193-
lev_coord.bounds,
194-
[[1.0, 0.95], [0.95, 0.5], [0.5, 0.0]],
195-
)
196-
197-
198118
def test_get_cli_fix():
199119
"""Test getting of fix."""
200120
fix = Fix.get_fixes("CMIP6", "CESM2", "Amon", "cli")

tests/integration/cmor/_fixes/cmip6/test_cesm2_waccm.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Tests for the fixes of CESM2-WACCM."""
22

3-
import os
4-
import unittest.mock
3+
from __future__ import annotations
4+
5+
from collections.abc import Sequence
6+
from typing import TYPE_CHECKING
57

68
import iris
79
import numpy as np
@@ -28,6 +30,10 @@
2830
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
2931
from esmvalcore.cmor._fixes.fix import GenericFix
3032
from esmvalcore.cmor.fix import Fix
33+
from esmvalcore.cmor.table import VariableInfo, get_var_info
34+
35+
if TYPE_CHECKING:
36+
from pathlib import Path
3137

3238

3339
def test_get_cl_fix():
@@ -41,25 +47,16 @@ def test_cl_fix():
4147
assert issubclass(Cl, BaseCl)
4248

4349

44-
@unittest.mock.patch(
45-
"esmvalcore.cmor._fixes.cmip6.cesm2.Fix.get_fixed_filepath",
46-
autospec=True,
47-
)
48-
def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
50+
def test_cl_fix_file(tmp_path: Path, test_data_path: Path) -> None:
4951
"""Test ``fix_file`` for ``cl``."""
5052
nc_path = test_data_path / "cesm2_waccm_cl.nc"
51-
mock_get_filepath.return_value = os.path.join(
52-
tmp_path,
53-
"fixed_cesm2_waccm_cl.nc",
54-
)
55-
fix = Cl(None)
56-
fixed_file = fix.fix_file(nc_path, tmp_path)
57-
mock_get_filepath.assert_called_once_with(
58-
tmp_path,
59-
nc_path,
60-
add_unique_suffix=False,
61-
)
62-
fixed_cube = iris.load_cube(fixed_file)
53+
vardef = get_var_info("CMIP6", "Amon", "cl")
54+
assert isinstance(vardef, VariableInfo)
55+
fix = Cl(vardef)
56+
fixed_cubes = fix.fix_file(nc_path, tmp_path)
57+
assert isinstance(fixed_cubes, Sequence)
58+
assert len(fixed_cubes) == 1
59+
fixed_cube = fixed_cubes[0]
6360
lev_coord = fixed_cube.coord(var_name="lev")
6461
a_coord = fixed_cube.coord(var_name="a")
6562
b_coord = fixed_cube.coord(var_name="b")

0 commit comments

Comments
 (0)