-
Notifications
You must be signed in to change notification settings - Fork 44
Granularity for fixed surface arguments #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bjlittle
merged 9 commits into
SciTools:master
from
trexfeathers:ff_surface_granularity
Jul 23, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b725be8
product_definition_section will no longer try to populate the fixed s…
a817191
Updated copyright year, fixed pep8 line length compliance
96955ad
Creating test_product_definition_section.py
0ecc2b8
Finished test_product_definition_section.py (needs commenting)
9c05b27
Finished commenting test_product_definition_section.py and test_produ…
4e23daf
Corrected pep8 non-conformance in test_product_definition_section.py
f6ee794
Modifications following bjlittle review
fff6ad9
Adapted call_count use since test class is now preserved - call_count…
697c273
Removed print output of each test combination - could be misleading i…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
iris_grib/tests/unit/load_convert/test_product_definition_section.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # (C) British Crown Copyright 2014 - 2019, Met Office | ||
| # | ||
| # This file is part of iris-grib. | ||
| # | ||
| # iris-grib is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # iris-grib is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with iris-grib. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Tests for `iris_grib._load_convert.product_definition_section`. | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
|
|
||
| # import iris_grib.tests first so that some things can be initialised | ||
| # before importing anything else. | ||
| import iris_grib.tests as tests | ||
|
|
||
| from iris.coords import DimCoord | ||
| import mock | ||
| import six | ||
|
|
||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from itertools import product | ||
|
|
||
| from iris_grib._load_convert import product_definition_section | ||
| from iris_grib.tests.unit.load_convert import empty_metadata | ||
| from iris_grib.tests.unit.load_convert.test_product_definition_template_0 \ | ||
| import section_4 as pdt_0_section_4 | ||
| from iris_grib.tests.unit.load_convert.test_product_definition_template_31 \ | ||
| import section_4 as pdt_31_section_4 | ||
|
|
||
|
|
||
| class TestFixedSurfaces(tests.IrisGribTest): | ||
| """ | ||
| Tests focussing on the handling of fixed surface elements in section 4. | ||
| Expects/ignores depending on the template number. | ||
| """ | ||
| def setUp(self): | ||
| self.patch('warnings.warn') | ||
| self.translate_phenomenon_patch = self.patch( | ||
| 'iris_grib._load_convert.translate_phenomenon' | ||
| ) | ||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Prep placeholder variables for product_definition_section. | ||
| self.discipline = mock.sentinel.discipline | ||
| self.tablesVersion = mock.sentinel.tablesVersion | ||
| self.rt_coord = DimCoord(24, 'forecast_reference_time', | ||
| units='hours since epoch') | ||
| self.metadata = empty_metadata() | ||
|
|
||
| self.templates = {0: pdt_0_section_4(), 31: pdt_31_section_4()} | ||
| self.fixed_surface_keys = [ | ||
| 'typeOfFirstFixedSurface', | ||
| 'scaledValueOfFirstFixedSurface', | ||
| 'typeOfSecondFixedSurface' | ||
| ] | ||
|
|
||
| def _check_fixed_surface(self, fs_is_expected, fs_is_present): | ||
| """ | ||
| Whether or not fixed surface elements are expected/present in the | ||
| section 4 keys, most of the code is shared so we are using a single | ||
| function with parameters. | ||
| """ | ||
|
|
||
| # Use the section 4 from either product_definition_section #1 or #31. | ||
| # #0 contains fixed surface elements, #31 does not. | ||
| template_number = 0 if fs_is_expected else 31 | ||
| section_4 = self.templates[template_number] | ||
| section_4.update({ | ||
| 'productDefinitionTemplateNumber': template_number, | ||
| 'parameterCategory': None, | ||
| 'parameterNumber': None | ||
| }) | ||
|
|
||
| for key in self.fixed_surface_keys: | ||
| # Force the presence or absence of the fixed surface elements even | ||
| # when they're respectively ignored or expected. | ||
| if fs_is_present and key not in section_4: | ||
| section_4[key] = pdt_0_section_4()[key] | ||
| elif (not fs_is_present) and key in section_4: | ||
| del section_4[key] | ||
|
|
||
| def run_function(): | ||
| # For re-use in every type of test below. | ||
| product_definition_section( | ||
| section_4, self.metadata, self.discipline, self.tablesVersion, | ||
| self.rt_coord) | ||
|
|
||
| if fs_is_expected and not fs_is_present: | ||
| # Should error since the expected keys are missing. | ||
| error_message = 'FixedSurface' | ||
| with six.assertRaisesRegex(self, KeyError, error_message): | ||
| run_function() | ||
| else: | ||
| # Should have a successful run for all other circumstances. | ||
|
|
||
| # Translate_phenomenon_patch is the end of the function, | ||
| # and should be able to accept None for the fixed surface | ||
| # arguments. So should always have run. | ||
| previous_call_count = self.translate_phenomenon_patch.call_count | ||
| run_function() | ||
| self.assertEqual(self.translate_phenomenon_patch.call_count, | ||
| previous_call_count + 1) | ||
| phenom_call_args = self.translate_phenomenon_patch.call_args[1] | ||
| for key in self.fixed_surface_keys: | ||
| # Check whether None or actual values have been passed for | ||
| # the fixed surface arguments. | ||
| if fs_is_expected: | ||
| self.assertEqual(phenom_call_args[key], section_4[key]) | ||
| else: | ||
| self.assertIsNone(phenom_call_args[key]) | ||
|
|
||
| def test_all_combinations(self): | ||
| """ | ||
| Test all combinations of fixed surface being expected/present | ||
|
|
||
| a. Expected and Present - standard behaviour for most templates | ||
| b. Expected and Absent - unplanned combination, should error | ||
| c. Unexpected and Present - unplanned combination, should be handled | ||
| identically to (d) | ||
| d. Unexpected and Absent - standard behaviour for a few templates | ||
| e.g. #31 | ||
| """ | ||
| for pair in product([True, False], repeat=2): | ||
| self._check_fixed_surface(*pair) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.