Skip to content

Commit 8aa0442

Browse files
authored
move tests.pp to tests.__init__ (#5679)
1 parent ddb2c43 commit 8aa0442

File tree

5 files changed

+92
-103
lines changed

5 files changed

+92
-103
lines changed

lib/iris/tests/__init__.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import iris.config
4444
import iris.cube
45+
import iris.fileformats
4546
import iris.tests.graphics as graphics
4647
import iris.util
4748

@@ -883,6 +884,93 @@ class GraphicsTest(graphics.GraphicsTestMixin, IrisTest):
883884
pass
884885

885886

887+
class PPTest:
888+
"""A mixin class to provide PP-specific utilities to subclasses of tests.IrisTest."""
889+
890+
@contextlib.contextmanager
891+
def cube_save_test(
892+
self,
893+
reference_txt_path,
894+
reference_cubes=None,
895+
reference_pp_path=None,
896+
**kwargs,
897+
):
898+
"""A context manager for testing the saving of Cubes to PP files.
899+
900+
Args:
901+
902+
* reference_txt_path:
903+
The path of the file containing the textual PP reference data.
904+
905+
Kwargs:
906+
907+
* reference_cubes:
908+
The cube(s) from which the textual PP reference can be re-built if necessary.
909+
* reference_pp_path:
910+
The location of a PP file from which the textual PP reference can be re-built if necessary.
911+
NB. The "reference_cubes" argument takes precedence over this argument.
912+
913+
The return value from the context manager is the name of a temporary file
914+
into which the PP data to be tested should be saved.
915+
916+
Example::
917+
with self.cube_save_test(reference_txt_path, reference_cubes=cubes) as temp_pp_path:
918+
iris.save(cubes, temp_pp_path)
919+
920+
"""
921+
# Watch out for a missing reference text file
922+
if not os.path.isfile(reference_txt_path):
923+
if reference_cubes:
924+
temp_pp_path = iris.util.create_temp_filename(".pp")
925+
try:
926+
iris.save(reference_cubes, temp_pp_path, **kwargs)
927+
self._create_reference_txt(reference_txt_path, temp_pp_path)
928+
finally:
929+
os.remove(temp_pp_path)
930+
elif reference_pp_path:
931+
self._create_reference_txt(reference_txt_path, reference_pp_path)
932+
else:
933+
raise ValueError(
934+
"Missing all of reference txt file, cubes, and PP path."
935+
)
936+
937+
temp_pp_path = iris.util.create_temp_filename(".pp")
938+
try:
939+
# This value is returned to the target of the "with" statement's "as" clause.
940+
yield temp_pp_path
941+
942+
# Load deferred data for all of the fields (but don't do anything with it)
943+
pp_fields = list(iris.fileformats.pp.load(temp_pp_path))
944+
for pp_field in pp_fields:
945+
pp_field.data
946+
with open(reference_txt_path, "r") as reference_fh:
947+
reference = "".join(reference_fh)
948+
self._assert_str_same(
949+
reference + "\n",
950+
str(pp_fields) + "\n",
951+
reference_txt_path,
952+
type_comparison_name="PP files",
953+
)
954+
finally:
955+
os.remove(temp_pp_path)
956+
957+
def _create_reference_txt(self, txt_path, pp_path):
958+
# Load the reference data
959+
pp_fields = list(iris.fileformats.pp.load(pp_path))
960+
for pp_field in pp_fields:
961+
pp_field.data
962+
963+
# Clear any header words we don't use
964+
unused = ("lbexp", "lbegin", "lbnrec", "lbproj", "lbtyp")
965+
for pp_field in pp_fields:
966+
for word_name in unused:
967+
setattr(pp_field, word_name, 0)
968+
969+
# Save the textual representation of the PP fields
970+
with open(txt_path, "w") as txt_file:
971+
txt_file.writelines(str(pp_fields))
972+
973+
886974
def skip_data(fn):
887975
"""Decorator to choose whether to run tests, based on the availability of
888976
external data.

lib/iris/tests/pp.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

lib/iris/tests/test_cdm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import iris.cube
2222
import iris.fileformats
2323
import iris.fileformats.dot
24-
import iris.tests.pp as pp
2524
import iris.tests.stock
2625

2726

@@ -1285,7 +1284,7 @@ def test_non_string_attributes(self):
12851284

12861285

12871286
@tests.skip_data
1288-
class TestMaskedData(tests.IrisTest, pp.PPTest):
1287+
class TestMaskedData(tests.IrisTest, tests.PPTest):
12891288
def _load_3d_cube(self):
12901289
# This 3D data set has a missing a slice with SOME missing values.
12911290
# The missing data is in the pressure = 1000 hPa, forcast_period = 0,

lib/iris/tests/test_cube_to_pp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import iris.coords
1818
import iris.fileformats.pp
1919
from iris.fileformats.pp import PPField3
20-
import iris.tests.pp as pp
2120
import iris.tests.stock as stock
2221
import iris.util
2322

@@ -36,7 +35,7 @@ def itab_callback(cube, field, filename):
3635

3736

3837
@tests.skip_data
39-
class TestPPSave(tests.IrisTest, pp.PPTest):
38+
class TestPPSave(tests.IrisTest, tests.PPTest):
4039
def test_no_forecast_time(self):
4140
cube = stock.lat_lon_cube()
4241
coord = iris.coords.DimCoord(
@@ -276,7 +275,7 @@ def geog_cs(self):
276275
return iris.coord_systems.GeogCS(6371229.0)
277276

278277

279-
class TestPPSaveRules(tests.IrisTest, pp.PPTest):
278+
class TestPPSaveRules(tests.IrisTest, tests.PPTest):
280279
def test_default_coord_system(self):
281280
GeogCS = iris.coord_systems.GeogCS
282281
cube = iris.tests.stock.lat_lon_cube()

lib/iris/tests/test_pp_cf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import iris.coords
1414
from iris.fileformats.netcdf import _thread_safe_nc
1515
from iris.fileformats.pp import STASH
16-
import iris.tests.pp as pp
1716
import iris.util
1817

1918

@@ -64,7 +63,7 @@ def callback_aaxzc_n10r13xy_b_pp(cube, field, filename):
6463

6564

6665
@tests.skip_data
67-
class TestAll(tests.IrisTest, pp.PPTest):
66+
class TestAll(tests.IrisTest, tests.PPTest):
6867
_ref_dir = ("usecases", "pp_to_cf_conversion")
6968

7069
def _test_file(self, name):

0 commit comments

Comments
 (0)