|
19 | 19 |
|
20 | 20 | """ |
21 | 21 | import inspect |
22 | | -from typing import Optional, Union |
| 22 | +from typing import Iterable, List, Optional, Union |
23 | 23 |
|
24 | 24 | import numpy as np |
25 | 25 | import pytest |
@@ -138,14 +138,15 @@ def create_testcase_files_or_cubes( |
138 | 138 | var_values_file1: Union[None, str, dict] = None, |
139 | 139 | global_value_file2: Optional[str] = None, |
140 | 140 | var_values_file2: Union[None, str, dict] = None, |
141 | | - cubes=False, |
| 141 | + cubes: bool = False, |
142 | 142 | ): |
143 | 143 | """ |
144 | | - Create temporary input netcdf files with specific content. |
| 144 | + Create temporary input netcdf files, or cubes, with specific content. |
145 | 145 |
|
146 | 146 | 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``. |
149 | 150 |
|
150 | 151 | Note: 'var_values_file<X>' args are dictionaries. The named variables are |
151 | 152 | 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): |
213 | 214 | self.input_filepaths = results |
214 | 215 | return results |
215 | 216 |
|
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 | + """ |
217 | 243 | # Save common attribute-name on the instance |
218 | 244 | self.attrname = attr_name |
219 | 245 |
|
@@ -255,19 +281,25 @@ def run_testcase(self, attr_name, values, create_cubes_or_files="files"): |
255 | 281 | ) |
256 | 282 |
|
257 | 283 | 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, |
259 | 288 | ): |
260 | 289 | """ |
261 | 290 | Return testcase results from an output file or cubes in a standardised form. |
262 | 291 |
|
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. |
264 | 294 | A file result is always [global_value, *local_values] |
265 | 295 | A cubes result is [*[global_value, *local_values]] (over different global vals) |
266 | 296 |
|
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**. |
271 | 303 |
|
272 | 304 | """ |
273 | 305 | attr_name = self.attrname |
@@ -365,7 +397,7 @@ def check_roundtrip_results(self, expected): |
365 | 397 | """ |
366 | 398 | Run checks on the generated output file. |
367 | 399 |
|
368 | | - The counterpart to create_roundtrip_testcase_OLDSTYLE, with similar control arguments. |
| 400 | + The counterpart to :meth:`run_roundtrip_testcase`, with similar arguments. |
369 | 401 | Check existence (or not) of a global attribute, and a number of local |
370 | 402 | (variable) attributes. |
371 | 403 | 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): |
631 | 663 | results = self.fetch_results( |
632 | 664 | cubes=result_cubes, oldstyle_combined=oldstyle_combined |
633 | 665 | ) |
| 666 | + # Standardise expected form to list(lists). |
634 | 667 | assert isinstance(expected, list) |
635 | 668 | if not isinstance(expected[0], list): |
636 | 669 | expected = [expected] |
@@ -872,9 +905,11 @@ class TestSave(MixinAttrsTesting): |
872 | 905 | """ |
873 | 906 |
|
874 | 907 | def run_save_testcase(self, attr_name, values): |
| 908 | + # Create input cubes. |
875 | 909 | self.run_testcase( |
876 | 910 | attr_name=attr_name, values=values, create_cubes_or_files="cubes" |
877 | 911 | ) |
| 912 | + # Save input cubes to a temporary result file. |
878 | 913 | self.result_filepath = self._testfile_path("result") |
879 | 914 | iris.save(self.input_cubes, self.result_filepath) |
880 | 915 |
|
|
0 commit comments