Skip to content

Commit 03a8fb5

Browse files
committed
Use mock in tests
1 parent 6d2b735 commit 03a8fb5

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

lib/iris/fileformats/netcdf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2245,7 +2245,9 @@ def is_valid_packspec(p):
22452245
least_significant_digit, packing=packspec)
22462246

22472247
if iris.config.netcdf.conventions_override:
2248-
conventions = cube.attributes['Conventions']
2248+
# Set to the default if custom conventions are not available.
2249+
conventions = cube.attributes.get('Conventions',
2250+
CF_CONVENTIONS_VERSION)
22492251
else:
22502252
conventions = CF_CONVENTIONS_VERSION
22512253

lib/iris/tests/unit/config/test_NetCDF.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
17-
"""Unit tests for the `iris.options.Paralle` class."""
17+
"""Unit tests for the `iris.config.NetCDF` class."""
1818

1919
from __future__ import (absolute_import, division, print_function)
2020
from six.moves import (filter, input, map, range, zip) # noqa
@@ -30,27 +30,30 @@
3030

3131

3232
class Test(tests.IrisTest):
33+
def setUp(self):
34+
self.options = iris.config.NetCDF()
35+
3336
def test_basic(self):
34-
self.assertFalse(iris.config.netcdf.conventions_override)
37+
self.assertFalse(self.options.conventions_override)
3538

3639
def test_enabled(self):
37-
iris.config.netcdf.conventions_override = True
38-
self.assertTrue(iris.config.netcdf.conventions_override)
40+
self.options.conventions_override = True
41+
self.assertTrue(self.options.conventions_override)
3942

4043
def test_bad_value(self):
4144
# A bad value should be ignored and replaced with the default value.
4245
bad_value = 'wibble'
4346
with warnings.catch_warnings(record=True) as w:
4447
warnings.simplefilter('always')
45-
iris.config.netcdf.conventions_override = bad_value
46-
self.assertFalse(iris.config.netcdf.conventions_override)
48+
self.options.conventions_override = bad_value
49+
self.assertFalse(self.options.conventions_override)
4750
exp_wmsg = 'Attempting to set invalid value {!r}'.format(bad_value)
4851
six.assertRegex(self, str(w[0].message), exp_wmsg)
4952

5053
def test__contextmgr(self):
51-
with iris.config.netcdf.context(conventions_override=True):
52-
self.assertTrue(iris.config.netcdf.conventions_override)
53-
self.assertFalse(iris.config.netcdf.conventions_override)
54+
with self.options.context(conventions_override=True):
55+
self.assertTrue(self.options.conventions_override)
56+
self.assertFalse(self.options.conventions_override)
5457

5558

5659
if __name__ == '__main__':

lib/iris/tests/unit/fileformats/netcdf/test_save.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# importing anything else.
2424
import iris.tests as tests
2525

26+
import mock
2627
import netCDF4 as nc
2728
import numpy as np
2829

@@ -37,6 +38,7 @@ def setUp(self):
3738
self.cube = Cube([0])
3839
self.custom_conventions = 'convention1 convention2'
3940
self.cube.attributes['Conventions'] = self.custom_conventions
41+
self.options = iris.config.netcdf
4042

4143
def test_custom_conventions__ignored(self):
4244
# Ensure that we drop existing conventions attributes and replace with
@@ -51,14 +53,26 @@ def test_custom_conventions__ignored(self):
5153
def test_custom_conventions__allowed(self):
5254
# Ensure that existing conventions attributes are passed through if the
5355
# relevant Iris option is set.
54-
with iris.config.netcdf.context(conventions_override=True):
56+
with mock.patch.object(self.options, 'conventions_override', True):
5557
with self.temp_filename('.nc') as nc_path:
5658
save(self.cube, nc_path, 'NETCDF4')
5759
ds = nc.Dataset(nc_path)
5860
res = ds.getncattr('Conventions')
5961
ds.close()
6062
self.assertEqual(res, self.custom_conventions)
6163

64+
def test_custom_conventions__allowed__missing(self):
65+
# Ensure the default conventions attribute is set if the relevant Iris
66+
# option is set but there is no custom conventions attribute.
67+
del self.cube.attributes['Conventions']
68+
with mock.patch.object(self.options, 'conventions_override', True):
69+
with self.temp_filename('.nc') as nc_path:
70+
save(self.cube, nc_path, 'NETCDF4')
71+
ds = nc.Dataset(nc_path)
72+
res = ds.getncattr('Conventions')
73+
ds.close()
74+
self.assertEqual(res, CF_CONVENTIONS_VERSION)
75+
6276

6377
class Test_attributes(tests.IrisTest):
6478
def test_attributes_arrays(self):

0 commit comments

Comments
 (0)