Skip to content

Commit 54f344c

Browse files
committed
Explain test routines better.
1 parent 78aaebb commit 54f344c

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

lib/iris/tests/integration/test_netcdf__loadsaveattrs.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
"""
2121
import inspect
22-
from typing import Optional, Union
22+
from typing import Iterable, List, Optional, Union
2323

2424
import numpy as np
2525
import pytest
@@ -138,14 +138,15 @@ def create_testcase_files_or_cubes(
138138
var_values_file1: Union[None, str, dict] = None,
139139
global_value_file2: Optional[str] = None,
140140
var_values_file2: Union[None, str, dict] = None,
141-
cubes=False,
141+
cubes: bool = False,
142142
):
143143
"""
144-
Create temporary input netcdf files with specific content.
144+
Create temporary input netcdf files, or cubes, with specific content.
145145
146146
Creates a temporary netcdf test file (or two) with the given global and
147-
variable-local attributes.
148-
The file(s) are used to test the behaviour of the attribute.
147+
variable-local attributes. Or build cubes, similarly.
148+
If ``cubes`` is ``True``, save cubes in ``self.input_cubes``.
149+
Else save filepaths in ``self.input_filepaths``.
149150
150151
Note: 'var_values_file<X>' args are dictionaries. The named variables are
151152
created, with an attribute = the dictionary value, *except* that a dictionary
@@ -213,7 +214,32 @@ def make_cubes(var_name, global_value=None, var_values=None):
213214
self.input_filepaths = results
214215
return results
215216

216-
def run_testcase(self, attr_name, values, create_cubes_or_files="files"):
217+
def run_testcase(
218+
self,
219+
attr_name: str,
220+
values: Union[List, List[List]],
221+
create_cubes_or_files: str = "files",
222+
) -> None:
223+
"""
224+
Create testcase inputs (files or cubes) with specified attributes.
225+
226+
Parameters
227+
----------
228+
attr_name : str
229+
name for all attributes created in this testcase.
230+
Also saved as ``self.attrname``, as used by ``fetch_results``.
231+
values : list
232+
list, or lists, of values for created attributes, each containing one global
233+
and one-or-more local attribute values as [global, local1, local2...]
234+
create_cubes_or_files : str, default "files"
235+
create either cubes or testfiles.
236+
237+
If ``create_cubes_or_files`` == "files", create one temporary netCDF file per
238+
values-list, and record in ``self.input_filepaths``.
239+
Else if ``create_cubes_or_files`` == "cubes", create sets of cubes with common
240+
global values and store all of them to ``self.input_cubes``.
241+
242+
"""
217243
# Save common attribute-name on the instance
218244
self.attrname = attr_name
219245

@@ -255,19 +281,25 @@ def run_testcase(self, attr_name, values, create_cubes_or_files="files"):
255281
)
256282

257283
def fetch_results(
258-
self, filepath=None, cubes=None, oldstyle_combined=False
284+
self,
285+
filepath: str = None,
286+
cubes: Iterable[Cube] = None,
287+
oldstyle_combined: bool = False,
259288
):
260289
"""
261290
Return testcase results from an output file or cubes in a standardised form.
262291
263-
Unpick the global+local values of an attribute resulting from an operation.
292+
Unpick the global+local values of the attribute ``self.attrname``, resulting
293+
from a test operation.
264294
A file result is always [global_value, *local_values]
265295
A cubes result is [*[global_value, *local_values]] (over different global vals)
266296
267-
When "oldstyle_combined" simulate the "legacy" result, when each cube had a
268-
single combined attribute dictionary. This enables us to check against former
269-
behaviour (and behaviour of results treated as a single dictionary).
270-
If results are from a *file*, this has no effect.
297+
When ``oldstyle_combined`` is ``True``, simulate the "legacy" style results,
298+
that is when each cube had a single combined attribute dictionary.
299+
This enables us to check against former behaviour, by combining results into a
300+
single dictionary. N.B. per-cube single results are then returned in the form:
301+
[None, cube1, cube2...].
302+
N.B. if results are from a *file*, this key has **no effect**.
271303
272304
"""
273305
attr_name = self.attrname
@@ -365,7 +397,7 @@ def check_roundtrip_results(self, expected):
365397
"""
366398
Run checks on the generated output file.
367399
368-
The counterpart to create_roundtrip_testcase_OLDSTYLE, with similar control arguments.
400+
The counterpart to :meth:`run_roundtrip_testcase`, with similar arguments.
369401
Check existence (or not) of a global attribute, and a number of local
370402
(variable) attributes.
371403
Values of 'None' mean to check that the relevant global/local attribute does
@@ -631,6 +663,7 @@ def check_load_results(self, expected, oldstyle_combined=False):
631663
results = self.fetch_results(
632664
cubes=result_cubes, oldstyle_combined=oldstyle_combined
633665
)
666+
# Standardise expected form to list(lists).
634667
assert isinstance(expected, list)
635668
if not isinstance(expected[0], list):
636669
expected = [expected]
@@ -872,9 +905,11 @@ class TestSave(MixinAttrsTesting):
872905
"""
873906

874907
def run_save_testcase(self, attr_name, values):
908+
# Create input cubes.
875909
self.run_testcase(
876910
attr_name=attr_name, values=values, create_cubes_or_files="cubes"
877911
)
912+
# Save input cubes to a temporary result file.
878913
self.result_filepath = self._testfile_path("result")
879914
iris.save(self.input_cubes, self.result_filepath)
880915

0 commit comments

Comments
 (0)