-
Notifications
You must be signed in to change notification settings - Fork 279
/
test_meshload_checks.py
83 lines (72 loc) · 2.89 KB
/
test_meshload_checks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Copyright Iris contributors
#
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for mesh handling within iris netcdf loads."""
import pytest
import iris
from iris.experimental.ugrid.load import PARSE_UGRID_ON_LOAD
from .test_load_meshes import (
_TEST_CDL_HEAD,
_TEST_CDL_TAIL,
cdl_to_nc,
)
class TestMeshLoad:
def _create_testnc(self, location="node", meshdim="node"):
# Add an extra (possibly mal-formed) mesh data to the testfile.
if location is None:
location_cdl = ""
else:
location_cdl = f'extra_data:location = "{location}" ;'
extra_cdl = f"""
float extra_data(levels, {meshdim}) ;
extra_data:coordinates = "node_x node_y" ;
{location_cdl}
extra_data:mesh = "mesh" ;
"""
# Insert this into the definitions part of the 'standard' testfile CDL
extended_cdl = _TEST_CDL_HEAD + extra_cdl + _TEST_CDL_TAIL
testfile_path = cdl_to_nc(extended_cdl, tmpdir=self.tmpdir)
return testfile_path
@pytest.fixture(params=["nolocation", "badlocation", "baddim"])
def failnc(self, request, tmp_path_factory):
self.param = request.param
kwargs = {}
if self.param == "nolocation":
kwargs["location"] = None
elif self.param == "badlocation":
kwargs["location"] = "invalid_location"
elif self.param == "baddim":
kwargs["meshdim"] = "vertex"
else:
raise ValueError(f"unexpected param: {self.param}")
self.tmpdir = tmp_path_factory.mktemp("meshload")
return self._create_testnc(**kwargs)
def test_extrameshvar__ok(self, tmp_path_factory):
# Check that the default cdl construction loads OK
self.tmpdir = tmp_path_factory.mktemp("meshload")
testnc = self._create_testnc()
with PARSE_UGRID_ON_LOAD.context():
iris.load(testnc)
def test_extrameshvar__fail(self, failnc):
# Check that the expected errors are raised in various cases.
param = self.param
if param == "nolocation":
match_msg = (
"mesh data variable 'extra_data' has an " "invalid location='<empty>'."
)
elif param == "badlocation":
match_msg = (
"mesh data variable 'extra_data' has an "
"invalid location='invalid_location'."
)
elif param == "baddim":
match_msg = (
"mesh data variable 'extra_data' does not have the node mesh "
"dimension 'node', in its dimensions."
)
else:
raise ValueError(f"unexpected param: {param}")
with PARSE_UGRID_ON_LOAD.context():
with pytest.raises(ValueError, match=match_msg):
iris.load(failnc)