Skip to content

Commit

Permalink
add tests and error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenworsley committed Jul 17, 2024
1 parent 4212b5c commit 97c4866
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/iris/experimental/ugrid/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,8 @@ def coord(
"""
if location is not None:
if location not in ["node", "edge", "face"]:
raise ValueError(f"Expected location to be one of `node`, `edge` or `face`, got `{location}`")
include_nodes_new = location == "node"
include_edges_new = location == "edge"
include_faces_new = location == "face"
Expand Down Expand Up @@ -1661,8 +1663,13 @@ def coords(
if location is not None:
if isinstance(location, str):
_location = [location]
else:
elif isinstance(location, Iterable):
_location = location
else:
raise TypeError(f"Expected location to be string or an Iterable, got {type(location)}")
for loc in _location:
if loc not in ["node", "edge", "face"]:
raise ValueError(f"Expected location to contain only `node`, `edge` or `face`, got `{location}`")
include_nodes_new = "node" in _location
include_edges_new = "edge" in _location
include_faces_new = "face" in _location
Expand Down
44 changes: 44 additions & 0 deletions lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,25 @@ def test_coord(self):
func = self.mesh.coord
exception = CoordinateNotFoundError
self.assertRaisesRegex(exception, ".*but found 2", func, include_nodes=True)
self.assertRaisesRegex(exception, ".*but found 2", func, location="node")
self.assertRaisesRegex(exception, ".*but found none", func, axis="t")
self.assertRaisesRegex(
ValueError,
"include_edges.*incompatible with.*node",
func,
location="node",
include_edges=True,
)
self.assertRaisesRegex(
ValueError,
"include_nodes.*incompatible with.*node",
func,
location="node",
include_nodes=False,
)
self.assertRaisesRegex(
ValueError, "Expected location.*got `foo`", func, location="foo"
)

def test_coords(self):
# General results. Method intended for inheritance.
Expand All @@ -238,6 +256,9 @@ def test_coords(self):
{"long_name": "long_name"},
{"var_name": "node_lon"},
{"attributes": {"test": 1}},
{"include_nodes": True},
{"location": "node"},
{"location": ["node", "edge"]},
)

fake_coord = AuxCoord([0])
Expand All @@ -248,6 +269,10 @@ def test_coords(self):
{"long_name": "foo"},
{"var_name": "foo"},
{"attributes": {"test": 2}},
{"include_nodes": False},
{"include_edges": True},
{"location": "face"},
{"location": ["face", "edge"]},
)

func = self.mesh.coords
Expand All @@ -256,6 +281,25 @@ def test_coords(self):
for kwargs in negative_kwargs:
self.assertNotIn(self.NODE_LON, func(**kwargs))

func = self.mesh.coords
self.assertRaisesRegex(
ValueError,
"include_edges.*incompatible with.*node",
func,
location=["node", "face"],
include_edges=True,
)
self.assertRaisesRegex(
ValueError,
"include_nodes.*incompatible with.*node",
func,
location=["node", "face"],
include_nodes=False,
)
self.assertRaisesRegex(
ValueError, "Expected location.*got.*foo", func, location=["node","foo"]
)

def test_coords_elements(self):
# topology_dimension-specific results. Method intended to be overridden.
all_expected = {
Expand Down

0 comments on commit 97c4866

Please sign in to comment.