Skip to content

Commit

Permalink
Changes to tests from old versions
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-garciaperdomo committed Nov 28, 2024
1 parent 35c71c0 commit 15495c1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 100 deletions.
14 changes: 6 additions & 8 deletions tests/integration/cmor/_fixes/cmip6/test_cesm2.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,12 @@ def test_tasmin_fix_metadata(tasmin_cubes):
for cube in tasmin_cubes:
with pytest.raises(iris.exceptions.CoordinateNotFoundError):
cube.coord("height")
height_coord = iris.coords.AuxCoord(
2.0,
var_name="height",
standard_name="height",
long_name="height",
units=Unit("m"),
attributes={"positive": "up"},
)
height_coord = iris.coords.AuxCoord(2.0,
var_name="height",
standard_name="height",
long_name="height",
units=Unit("m"),
attributes={"positive": "up"})
vardef = get_var_info("CMIP6", "day", "tasmin")
fix = Tasmin(vardef)

Expand Down
123 changes: 53 additions & 70 deletions tests/integration/cmor/_fixes/cmip6/test_cesm2_waccm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import iris
import numpy as np
import pytest
import pandas as pd

from esmvalcore.cmor._fixes.cmip6.cesm2 import Cl as BaseCl
from esmvalcore.cmor._fixes.cmip6.cesm2 import Fgco2 as BaseFgco2
Expand All @@ -18,11 +19,12 @@
Omon,
Siconc,
Tas,
Tasmin,
Pr,
)
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
from esmvalcore.cmor._fixes.fix import GenericFix
from esmvalcore.cmor.fix import Fix
from esmvalcore.cmor.table import get_var_info


def test_get_cl_fix():
Expand Down Expand Up @@ -127,72 +129,53 @@ def test_tas_fix():
assert Tas is BaseTas


class TestTasmin(unittest.TestCase):
"""Test tasmin fixes."""

def setUp(self):
"""Prepare tests."""
wrong_time_coord = iris.coords.AuxCoord(
points=[1.0, 2.0, 1.0, 2.0, 3.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)

correct_time_coord = iris.coords.AuxCoord(
points=[1.0, 2.0, 3.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)

lat_coord = iris.coords.DimCoord(
[0.0],
standard_name="latitude",
var_name="lat",
)

lon_coord = iris.coords.DimCoord(
[0.0],
standard_name="longitude",
var_name="lon",
)

self.time_coord = correct_time_coord
self.wrong_cube = iris.cube.CubeList(
[iris.cube.Cube(np.ones((5, 1, 1)), var_name="tasmin", units="K")]
)
self.wrong_cube[0].add_aux_coord(wrong_time_coord, 0)
self.wrong_cube[0].add_dim_coord(lat_coord, 1)
self.wrong_cube[0].add_dim_coord(lon_coord, 2)
self.correct_cube = iris.cube.CubeList(
[iris.cube.Cube(np.ones(3), var_name="tasmin", units="K")]
)
self.correct_cube[0].add_aux_coord(correct_time_coord, 0)

self.fix = Tasmin(None)

def test_get(self):
"""Test fix get."""
self.assertListEqual(
Fix.get_fixes("CMIP6", "NCAR", "day", "tasmin"),
[Tasmin(None)],
)

def test_tasmin_fix_metadata(self):
"""Test metadata fix."""
out_wrong_cube = self.fix.fix_metadata(self.wrong_cube)
out_correct_cube = self.fix.fix_metadata(self.correct_cube)

time = out_wrong_cube[0].coord("time")
assert time == self.time_coord

time = out_correct_cube[0].coord("time")
assert time == self.time_coord

def test_tasmin_fix_metadata_no_time(self):
"""Test metadata fix with no time coord."""
self.correct_cube[0].remove_coord("time")
out_correct_cube = self.fix.fix_metadata(self.correct_cube)
with self.assertRaises(iris.exceptions.CoordinateNotFoundError):
out_correct_cube[0].coord("time")
@pytest.fixture
def pr_cubes():
correct_time_coord = iris.coords.DimCoord(
points=[1.0, 2.0, 3.0, 4.0, 5.0],
var_name="time",
standard_name="time",
units="days since 1850-01-01",
)

lat_coord = iris.coords.DimCoord(
[0.0], var_name="lat", standard_name="latitude"
)

lon_coord = iris.coords.DimCoord(
points=[0.0], var_name="lon", standard_name="longitude"
)

correct_coord_specs = [
(correct_time_coord, 0),
(lat_coord, 1),
(lon_coord, 2),
]

correct_pr_cube = iris.cube.Cube(
np.ones((5, 1, 1)),
var_name="pr",
units="kg m-2 s-1",
dim_coords_and_dims=correct_coord_specs,
)

scalar_cube = iris.cube.Cube(0.0, var_name="ps")

return iris.cube.CubeList([correct_pr_cube, scalar_cube])


def test_get_pr_fix():
"""Test pr fix."""
fix = Fix.get_fixes("CMIP6", "CESM2", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]


def test_pr_fix_metadata(pr_cubes):
"""Test metadata fix."""
vardef = get_var_info("CMIP6", "day", "pr")
fix = Pr(vardef)

out_cubes = fix.fix_metadata(pr_cubes)
assert out_cubes[0].var_name == "pr"
coord = out_cubes[0].coord("time")
assert pd.Series(coord.points).is_monotonic_increasing
35 changes: 13 additions & 22 deletions tests/integration/cmor/_fixes/cmip6/test_cesm2_waccm_fv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import iris
import numpy as np
import pytest
import pandas as pd

from esmvalcore.cmor._fixes.cmip6.cesm2 import Fgco2 as BaseFgco2
from esmvalcore.cmor._fixes.cmip6.cesm2 import Tas as BaseTas
Expand All @@ -20,6 +21,7 @@
from esmvalcore.cmor._fixes.common import SiconcFixScalarCoord
from esmvalcore.cmor._fixes.fix import GenericFix
from esmvalcore.cmor.fix import Fix
from esmvalcore.cmor.table import get_var_info


def test_get_cl_fix():
Expand Down Expand Up @@ -143,29 +145,18 @@ def pr_cubes():
return iris.cube.CubeList([correct_pr_cube, wrong_pr_cube])


def test_get(self):
"""Test fix get."""
self.assertListEqual(
Fix.get_fixes("CMIP6", "CESM2", "day", "pr"),
[Pr(None), GenericFix(None)],
)
def test_get_pr_fix():
"""Test pr fix."""
fix = Fix.get_fixes("CMIP6", "CESM2", "day", "pr")
assert fix == [Pr(None), GenericFix(None)]


def test_pr_fix_metadata(self):
def test_pr_fix_metadata(pr_cubes):
"""Test metadata fix."""
out_wrong_cube = self.fix.fix_metadata(self.wrong_cube)
out_correct_cube = self.fix.fix_metadata(self.correct_cube)

time = out_wrong_cube[0].coord("time")
assert time == self.time_coord

time = out_correct_cube[0].coord("time")
assert time == self.time_coord

vardef = get_var_info("CMIP6", "day", "pr")
fix = Pr(vardef)

def test_pr_fix_metadata_no_time(self):
"""Test metadata fix with no time coord."""
self.correct_cube[0].remove_coord("time")
out_correct_cube = self.fix.fix_metadata(self.correct_cube)
with self.assertRaises(iris.exceptions.CoordinateNotFoundError):
out_correct_cube[0].coord("time")
out_cubes = fix.fix_metadata(pr_cubes)
assert out_cubes[0].var_name == "pr"
coord = out_cubes[0].coord("time")
assert pd.Series(coord.points).is_monotonic_increasing

0 comments on commit 15495c1

Please sign in to comment.