-
Notifications
You must be signed in to change notification settings - Fork 44
Update product attributes and metadata.yml with cube metadata before saving files
#1837
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c2f14f
Updated product attributes with cube metadata at closing
schlunma 898847c
Updated frequency from cube attributes
schlunma 77b353e
Added doc
schlunma b8155da
Apply suggestions from code review
schlunma ad2e8d7
Used pytest.parametrize to simplify tests
schlunma 2f39531
Made new function private
schlunma 730071f
Merge branch 'main' into update_metadata_yml_correctly
schlunma 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
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
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
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,142 @@ | ||
| """Unit tests for :class:`esmvalcore.preprocessor.PreprocessorFile`.""" | ||
|
|
||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from iris.cube import Cube, CubeList | ||
|
|
||
| from esmvalcore.preprocessor import PreprocessorFile | ||
|
|
||
| ATTRIBUTES = { | ||
| 'filename': 'file.nc', | ||
| 'standard_name': 'precipitation', | ||
| 'long_name': 'Precipitation', | ||
| 'short_name': 'pr', | ||
| 'units': 'kg m-2 s-1', | ||
| 'frequency': 'mon', | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def product(): | ||
| """PreprocessorFile object used for testing.""" | ||
| cube = Cube( | ||
| 0, | ||
| var_name='tas', | ||
| standard_name='air_temperature', | ||
| long_name='Near-Surface Air Temperature', | ||
| units='K', | ||
| attributes={'frequency': 'day'}, | ||
| ) | ||
| product = PreprocessorFile(attributes=ATTRIBUTES, settings={}) | ||
| product._cubes = CubeList([cube, cube, cube]) | ||
| return product | ||
|
|
||
|
|
||
| def test_update_attributes_empty_cubes(product): | ||
| """Test ``_update_attributes``.""" | ||
| product._cubes = CubeList([]) | ||
| product._update_attributes() | ||
|
|
||
| assert not product._cubes | ||
| assert product.attributes == ATTRIBUTES | ||
|
|
||
|
|
||
| def test_update_attributes(product): | ||
| """Test ``_update_attributes``.""" | ||
| product._update_attributes() | ||
|
|
||
| assert product.attributes == { | ||
| 'filename': 'file.nc', | ||
| 'standard_name': 'air_temperature', | ||
| 'long_name': 'Near-Surface Air Temperature', | ||
| 'short_name': 'tas', | ||
| 'units': 'K', | ||
| 'frequency': 'day', | ||
| } | ||
valeriupredoi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert isinstance(product.attributes['units'], str) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'name,cube_property,expected_name', | ||
| [ | ||
| ('standard_name', 'standard_name', ''), | ||
| ('long_name', 'long_name', ''), | ||
| ('short_name', 'var_name', ''), | ||
| ], | ||
| ) | ||
| def test_update_attributes_empty_names(product, name, cube_property, | ||
| expected_name): | ||
| """Test ``_update_attributes``.""" | ||
| setattr(product._cubes[0], cube_property, None) | ||
| product._update_attributes() | ||
|
|
||
| expected_attributes = { | ||
| 'filename': 'file.nc', | ||
| 'standard_name': 'air_temperature', | ||
| 'long_name': 'Near-Surface Air Temperature', | ||
| 'short_name': 'tas', | ||
| 'units': 'K', | ||
| 'frequency': 'day', | ||
| } | ||
| expected_attributes[name] = expected_name | ||
| assert product.attributes == expected_attributes | ||
| assert isinstance(product.attributes['units'], str) | ||
|
|
||
|
|
||
| def test_update_attributes_empty_frequency(product): | ||
| """Test ``_update_attributes``.""" | ||
| product._cubes[0].attributes.pop('frequency') | ||
| product._update_attributes() | ||
|
|
||
| assert product.attributes == { | ||
| 'filename': 'file.nc', | ||
| 'standard_name': 'air_temperature', | ||
| 'long_name': 'Near-Surface Air Temperature', | ||
| 'short_name': 'tas', | ||
| 'units': 'K', | ||
| 'frequency': 'mon', | ||
| } | ||
| assert isinstance(product.attributes['units'], str) | ||
|
|
||
|
|
||
| def test_update_attributes_no_frequency(product): | ||
| """Test ``_update_attributes``.""" | ||
| product._cubes[0].attributes.pop('frequency') | ||
| product.attributes.pop('frequency') | ||
| product._update_attributes() | ||
|
|
||
| assert product.attributes == { | ||
| 'filename': 'file.nc', | ||
| 'standard_name': 'air_temperature', | ||
| 'long_name': 'Near-Surface Air Temperature', | ||
| 'short_name': 'tas', | ||
| 'units': 'K', | ||
| } | ||
| assert isinstance(product.attributes['units'], str) | ||
|
|
||
|
|
||
| def test_close_no_cubes(): | ||
| """Test ``close``.""" | ||
| product = mock.create_autospec(PreprocessorFile, instance=True) | ||
| product._cubes = None | ||
|
|
||
| PreprocessorFile.close(product) | ||
|
|
||
| product._update_attributes.assert_not_called() | ||
| product.save.assert_not_called() | ||
| product.save_provenance.assert_not_called() | ||
| assert product._cubes is None | ||
|
|
||
|
|
||
| def test_close(): | ||
| """Test ``close``.""" | ||
| product = mock.create_autospec(PreprocessorFile, instance=True) | ||
| product._cubes = CubeList([Cube(0)]) | ||
|
|
||
| PreprocessorFile.close(product) | ||
|
|
||
| product._update_attributes.assert_called_once_with() | ||
| product.save.assert_called_once_with() | ||
| product.save_provenance.assert_called_once_with() | ||
| assert product._cubes is None | ||
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.