Skip to content

Commit 69847cd

Browse files
authored
Converted integration/fast_load to pytest (#6821)
* converted test_fast_load * added teardown
1 parent 77b2095 commit 69847cd

File tree

1 file changed

+41
-62
lines changed

1 file changed

+41
-62
lines changed

lib/iris/tests/integration/fast_load/test_fast_load.py

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@
44
# See LICENSE in the root of the repository for full licensing details.
55
"""Integration tests for fast-loading FF and PP files."""
66

7-
# import iris tests first so that some things can be initialised
8-
# before importing anything else.
9-
import iris.tests as tests # isort:skip
10-
117
from collections.abc import Iterable
12-
import shutil
13-
import tempfile
148

159
import numpy as np
10+
import pytest
1611

1712
import iris
1813
from iris.coord_systems import GeogCS
@@ -29,10 +24,10 @@ class Mixin_FieldTest:
2924
# * create 'raw' cubes to produce the desired PP fields in a test file.
3025
# * save 'raw' cubes to temporary PP files that get deleted afterwards.
3126
# * control whether tests run with 'normal' or 'fast' loading.
32-
33-
def setUp(self):
27+
@pytest.fixture(autouse=True)
28+
def _fieldtest_setup(self, tmp_path):
3429
# Create a private temporary directory.
35-
self.temp_dir_path = tempfile.mkdtemp()
30+
self.temp_dir_path = tmp_path
3631
# Initialise temporary filename generation.
3732
self.tempfile_count = 0
3833
self.tempfile_path_fmt = (
@@ -48,12 +43,8 @@ def setUp(self):
4843
# N.B. we can't use a 'with', so issue separate 'enter' and 'exit'
4944
# calls instead.
5045
self.load_context.__enter__()
51-
52-
def tearDown(self):
53-
# Delete temporary directory.
54-
shutil.rmtree(self.temp_dir_path)
46+
yield
5547
if self.do_fast_loads:
56-
# End the 'fast loading' context.
5748
self.load_context.__exit__(None, None, None)
5849

5950
def _temp_filepath(self, user_name="", suffix=".pp"):
@@ -274,15 +265,15 @@ def test_basic(self):
274265
file = self.save_fieldcubes(flds)
275266
results = iris.load(file)
276267
expected = CubeList(flds).merge()
277-
self.assertEqual(results, expected)
268+
assert results == expected
278269

279270
def test_phenomena(self):
280271
# Show that different phenomena are merged into distinct cubes.
281272
flds = self.fields(c_t="1122", phn="0101")
282273
file = self.save_fieldcubes(flds)
283274
results = iris.load(file)
284275
expected = CubeList(flds).merge()
285-
self.assertEqual(results, expected)
276+
assert results == expected
286277

287278
def test_cross_file_concatenate(self):
288279
# Combine vector dimensions (i.e. concatenate) across multiple files.
@@ -292,7 +283,7 @@ def test_cross_file_concatenate(self):
292283
file_2 = self.save_fieldcubes(fldset_2)
293284
results = iris.load((file_1, file_2))
294285
expected = CubeList(fldset_1 + fldset_2).merge()
295-
self.assertEqual(results, expected)
286+
assert results == expected
296287

297288
def test_cell_method(self):
298289
# Check that cell methods (i.e. LBPROC values) produce distinct
@@ -303,7 +294,7 @@ def test_cell_method(self):
303294
expected = CubeList(
304295
CubeList(flds[i_start::3]).merge_cube() for i_start in range(3)
305296
)
306-
self.assertEqual(results, expected)
297+
assert results == expected
307298

308299

309300
class MixinCallDetails:
@@ -322,7 +313,7 @@ def test_stash_constraint(self):
322313
stash_attribute = airtemp_flds[0].attributes["STASH"]
323314
results = iris.load(file, iris.AttributeConstraint(STASH=stash_attribute))
324315
expected = CubeList(airtemp_flds).merge()
325-
self.assertEqual(results, expected)
316+
assert results == expected
326317

327318
def test_ordinary_constraint(self):
328319
# Check that a 'normal' constraint functions correctly.
@@ -332,7 +323,7 @@ def test_ordinary_constraint(self):
332323
height_constraint = iris.Constraint(height=lambda h: 150.0 < h < 350.0)
333324
results = iris.load(file, height_constraint)
334325
expected = CubeList(flds[1:3]).merge()
335-
self.assertEqual(results, expected)
326+
assert results == expected
336327

337328
def test_callback(self):
338329
# Use 2 timesteps each of (air-temp on height) and (rh on pressure).
@@ -342,7 +333,7 @@ def test_callback(self):
342333
if not self.do_fast_loads:
343334

344335
def callback(cube, field, filename):
345-
self.assertEqual(filename, file)
336+
assert filename == file
346337
lbvc = field.lbvc
347338
if lbvc == 1:
348339
# reject the height level data (accept only pressure).
@@ -354,7 +345,7 @@ def callback(cube, field, filename):
354345
else:
355346

356347
def callback(cube, collation, filename):
357-
self.assertEqual(filename, file)
348+
assert filename == file
358349
lbvcs = [fld.lbvc for fld in collation.fields]
359350
lbvc0 = lbvcs[0]
360351
if not np.all(lbvcs == lbvc0):
@@ -377,14 +368,14 @@ def callback(cube, collation, filename):
377368
else:
378369
expected[0].attributes["A_LBVC"] = [np.int32(8)] * 2
379370

380-
self.assertEqual(results, expected)
371+
assert results == expected
381372

382373
def test_load_cube(self):
383374
flds = self.fields(c_t="123", cft="000", ctp="123", c_p=0)
384375
file = self.save_fieldcubes(flds)
385376
results = iris.load_cube(file)
386377
expected = CubeList(flds).merge_cube()
387-
self.assertEqual(results, expected)
378+
assert results == expected
388379

389380
def test_load_cubes(self):
390381
flds = self.fields(c_h="0123")
@@ -402,7 +393,7 @@ def test_load_cubes(self):
402393
CubeList(flds).merge_cube(),
403394
]
404395
)
405-
self.assertEqual(results, expected)
396+
assert results == expected
406397

407398
def test_load_raw(self):
408399
fldset_1 = self.fields(c_t="015", phn="001")
@@ -443,7 +434,7 @@ def timeorder(cube):
443434
expected = sorted(expected, key=timeorder)
444435
results = sorted(results, key=timeorder)
445436

446-
self.assertEqual(results, expected)
437+
assert results == expected
447438

448439

449440
class MixinDimsAndOrdering:
@@ -459,7 +450,7 @@ def test_multidim(self):
459450
file = self.save_fieldcubes(flds)
460451
results = iris.load(file)
461452
expected = CubeList(flds).merge()
462-
self.assertEqual(results, expected)
453+
assert results == expected
463454

464455
def test_odd_order(self):
465456
# Show that an erratic interleaving of phenomena fields still works.
@@ -468,7 +459,7 @@ def test_odd_order(self):
468459
file = self.save_fieldcubes(flds)
469460
results = iris.load(file)
470461
expected = CubeList(flds).merge()
471-
self.assertEqual(results, expected)
462+
assert results == expected
472463

473464
def test_v_t_order(self):
474465
# With height varying faster than time, first dimension is time,
@@ -478,9 +469,9 @@ def test_v_t_order(self):
478469
results = iris.load(file)
479470
expected = CubeList(flds).merge()
480471
# Order is (t, h, y, x), which is "standard".
481-
self.assertEqual(expected[0].coord_dims("time"), (0,))
482-
self.assertEqual(expected[0].coord_dims("height"), (1,))
483-
self.assertEqual(results, expected)
472+
assert expected[0].coord_dims("time") == (0,)
473+
assert expected[0].coord_dims("height") == (1,)
474+
assert results == expected
484475

485476
def test_t_v_order(self):
486477
# With time varying faster than height, first dimension is height,
@@ -491,23 +482,23 @@ def test_t_v_order(self):
491482
expected = CubeList(flds).merge()
492483
if not self.do_fast_loads:
493484
# Order is (t, h, y, x), which is "standard".
494-
self.assertEqual(results[0].coord_dims("time"), (0,))
495-
self.assertEqual(results[0].coord_dims("height"), (1,))
485+
assert results[0].coord_dims("time") == (0,)
486+
assert results[0].coord_dims("height") == (1,)
496487
else:
497488
# Order is (h, t, y, x), which is *not* "standard".
498-
self.assertEqual(results[0].coord_dims("time"), (1,))
499-
self.assertEqual(results[0].coord_dims("height"), (0,))
489+
assert results[0].coord_dims("time") == (1,)
490+
assert results[0].coord_dims("height") == (0,)
500491
expected[0].transpose((1, 0, 2, 3))
501-
self.assertEqual(results, expected)
492+
assert results == expected
502493

503494
def test_missing_combination(self):
504495
# A case where one field is 'missing' to make a 2d result.
505496
flds = self.fields(c_t="00011", c_h="01202")
506497
file = self.save_fieldcubes(flds)
507498
results = iris.load(file)
508499
expected = CubeList(flds).merge()
509-
self.assertEqual(expected[0].coord_dims("time"), (0,))
510-
self.assertEqual(expected[0].coord_dims("height"), (0,))
500+
assert expected[0].coord_dims("time") == (0,)
501+
assert expected[0].coord_dims("height") == (0,)
511502
if self.do_fast_loads:
512503
# Something a bit weird happens to the 'height' coordinate in this
513504
# case (and not for standard load).
@@ -516,7 +507,7 @@ def test_missing_combination(self):
516507
cube.coord("height").points, dtype=np.float32
517508
)
518509
cube.coord("height").attributes = {}
519-
self.assertEqual(results, expected)
510+
assert results == expected
520511

521512

522513
class MixinProblemCases:
@@ -546,7 +537,7 @@ def test_FAIL_scalar_vector_concatenate(self):
546537
# repeatable ordering, because ??somehow?? the random temporary
547538
# directory name affects the ordering of the cubes in the result !
548539
results = CubeList(sorted(results, key=lambda cube: cube.shape))
549-
self.assertEqual(results, expected)
540+
assert results == expected
550541

551542
def test_FAIL_phenomena_nostash(self):
552543
# If we remove the 'STASH' attributes, certain phenomena can still be
@@ -586,7 +577,7 @@ def test_FAIL_phenomena_nostash(self):
586577
)
587578
one_cube.add_aux_coord(co_t_new, 0)
588579
expected = [one_cube]
589-
self.assertEqual(results, expected)
580+
assert results == expected
590581

591582
def test_FAIL_pseudo_levels(self):
592583
# Show how pseudo levels are handled.
@@ -619,56 +610,44 @@ def test_FAIL_pseudo_levels(self):
619610
# # Make a cubelist with this single cube.
620611
# expected = CubeList([nine_timepoints_cube])
621612

622-
self.assertEqual(results, expected)
613+
assert results == expected
623614

624615

625-
class TestBasic__Iris(Mixin_FieldTest, MixinBasic, tests.IrisTest):
626-
# Finally, an actual test-class (unittest.TestCase) :
616+
class TestBasic__Iris(Mixin_FieldTest, MixinBasic):
627617
# run the 'basic' tests with *normal* loading.
628618
do_fast_loads = False
629619

630620

631-
class TestBasic__Fast(Mixin_FieldTest, MixinBasic, tests.IrisTest):
632-
# Finally, an actual test-class (unittest.TestCase) :
621+
class TestBasic__Fast(Mixin_FieldTest, MixinBasic):
633622
# run the 'basic' tests with *FAST* loading.
634623
do_fast_loads = True
635624

636625

637-
class TestCallDetails__Iris(Mixin_FieldTest, MixinCallDetails, tests.IrisTest):
638-
# Finally, an actual test-class (unittest.TestCase) :
626+
class TestCallDetails__Iris(Mixin_FieldTest, MixinCallDetails):
639627
# run the 'call details' tests with *normal* loading.
640628
do_fast_loads = False
641629

642630

643-
class TestCallDetails__Fast(Mixin_FieldTest, MixinCallDetails, tests.IrisTest):
644-
# Finally, an actual test-class (unittest.TestCase) :
631+
class TestCallDetails__Fast(Mixin_FieldTest, MixinCallDetails):
645632
# run the 'call details' tests with *FAST* loading.
646633
do_fast_loads = True
647634

648635

649-
class TestDimsAndOrdering__Iris(Mixin_FieldTest, MixinDimsAndOrdering, tests.IrisTest):
650-
# Finally, an actual test-class (unittest.TestCase) :
636+
class TestDimsAndOrdering__Iris(Mixin_FieldTest, MixinDimsAndOrdering):
651637
# run the 'dimensions and ordering' tests with *normal* loading.
652638
do_fast_loads = False
653639

654640

655-
class TestDimsAndOrdering__Fast(Mixin_FieldTest, MixinDimsAndOrdering, tests.IrisTest):
656-
# Finally, an actual test-class (unittest.TestCase) :
641+
class TestDimsAndOrdering__Fast(Mixin_FieldTest, MixinDimsAndOrdering):
657642
# run the 'dimensions and ordering' tests with *FAST* loading.
658643
do_fast_loads = True
659644

660645

661-
class TestProblems__Iris(Mixin_FieldTest, MixinProblemCases, tests.IrisTest):
662-
# Finally, an actual test-class (unittest.TestCase) :
646+
class TestProblems__Iris(Mixin_FieldTest, MixinProblemCases):
663647
# run the 'failure cases' tests with *normal* loading.
664648
do_fast_loads = False
665649

666650

667-
class TestProblems__Fast(Mixin_FieldTest, MixinProblemCases, tests.IrisTest):
668-
# Finally, an actual test-class (unittest.TestCase) :
651+
class TestProblems__Fast(Mixin_FieldTest, MixinProblemCases):
669652
# run the 'failure cases' tests with *FAST* loading.
670653
do_fast_loads = True
671-
672-
673-
if __name__ == "__main__":
674-
tests.main()

0 commit comments

Comments
 (0)