From 97c48661735ebfa90e9a486a455524f3dd5541a5 Mon Sep 17 00:00:00 2001 From: "stephen.worsley" Date: Wed, 17 Jul 2024 15:14:55 +0100 Subject: [PATCH] add tests and error checking --- lib/iris/experimental/ugrid/mesh.py | 9 +++- .../unit/experimental/ugrid/mesh/test_Mesh.py | 44 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/iris/experimental/ugrid/mesh.py b/lib/iris/experimental/ugrid/mesh.py index 698839db9c..77d2c58515 100644 --- a/lib/iris/experimental/ugrid/mesh.py +++ b/lib/iris/experimental/ugrid/mesh.py @@ -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" @@ -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 diff --git a/lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py b/lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py index 48f9910099..c7ab870008 100644 --- a/lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py +++ b/lib/iris/tests/unit/experimental/ugrid/mesh/test_Mesh.py @@ -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. @@ -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]) @@ -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 @@ -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 = {