Skip to content

Commit 314e0e3

Browse files
committed
Final test-related changes
1 parent bf70ff4 commit 314e0e3

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/iris/fileformats/pp_save_rules.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ def _lbproc_rules(cube, pp):
561561
The PP field with updated metadata.
562562
563563
"""
564+
# Basic setting (this may be overridden by subsequent rules).
565+
pp.lbproc = 0
566+
564567
if cube.attributes.get("ukmo__process_flags", None):
565568
pp.lbproc += sum([lbproc_map[name]
566569
for name in cube.attributes["ukmo__process_flags"]])
@@ -799,9 +802,6 @@ def _all_other_rules(cube, pp):
799802

800803

801804
def verify(cube, field):
802-
# Basic setting (this may be overridden by subsequent rules).
803-
field.lbproc = 0
804-
805805
# Rules functions.
806806
field = _basic_coord_system_rules(cube, field)
807807
field = _um_version_rules(cube, field)

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from iris.coords import DimCoord, CellMethod
2727
from iris.fileformats._ff_cross_references import STASH_TRANS
2828
import iris.fileformats.pp as pp
29+
from iris.fileformats.pp_save_rules import _lbproc_rules
2930
from iris.tests import mock
3031
import iris.tests.stock as stock
3132

@@ -162,26 +163,42 @@ def test_um_version(self):
162163

163164

164165
class Test_Save__LbprocProduction(tests.IrisTest):
166+
# This test class is a little different to the others.
167+
# If it called `pp.save` via `_pp_save_ppfield_values` it would run
168+
# `pp_save_rules.verify` and run all the save rules. As this class uses
169+
# a 3D cube with a time coord it would run the time rules, which would fail
170+
# because the mock object does not set up the `pp.lbtim` attribute
171+
# correctly (i.e. as a `SplittableInt` object).
172+
# To work around this we call the lbproc rules directly here.
173+
165174
def setUp(self):
166175
self.cube = stock.realistic_3d()
176+
self.pp_field = mock.MagicMock(spec=pp.PPField3)
177+
self.pp_field.HEADER_DEFN = pp.PPField3.HEADER_DEFN
178+
self.patcher = mock.patch('iris.fileformats.pp.PPField3',
179+
return_value=self.pp_field)
180+
self.patcher.start()
181+
182+
def tearDown(self):
183+
self.patcher.stop()
167184

168185
def test_no_cell_methods(self):
169-
lbproc = _pp_save_ppfield_values(self.cube).lbproc
186+
lbproc = _lbproc_rules(self.cube, self.pp_field).lbproc
170187
self.assertEqual(lbproc, 0)
171188

172189
def test_mean(self):
173190
self.cube.cell_methods = (CellMethod('mean', 'time', '1 hour'),)
174-
lbproc = _pp_save_ppfield_values(self.cube).lbproc
191+
lbproc = _lbproc_rules(self.cube, self.pp_field).lbproc
175192
self.assertEqual(lbproc, 128)
176193

177194
def test_minimum(self):
178195
self.cube.cell_methods = (CellMethod('minimum', 'time', '1 hour'),)
179-
lbproc = _pp_save_ppfield_values(self.cube).lbproc
196+
lbproc = _lbproc_rules(self.cube, self.pp_field).lbproc
180197
self.assertEqual(lbproc, 4096)
181198

182199
def test_maximum(self):
183200
self.cube.cell_methods = (CellMethod('maximum', 'time', '1 hour'),)
184-
lbproc = _pp_save_ppfield_values(self.cube).lbproc
201+
lbproc = _lbproc_rules(self.cube, self.pp_field).lbproc
185202
self.assertEqual(lbproc, 8192)
186203

187204

0 commit comments

Comments
 (0)