-
Notifications
You must be signed in to change notification settings - Fork 315
Provide utility function "file_is_newer_than" for results caching. #787
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41b0988
Provide utility function "file_is_newer_than" for results caching.
pp-mo 1d98c26
Simplify file load helper.
pp-mo aa298a1
Added whats-new entry. Slightly improved docstring.
pp-mo ab9223c
Move 'tests.util.test_file_is_newer_than' into the unit-tests structure.
pp-mo 310fe64
Tidied error case testing.
pp-mo 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
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
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,17 @@ | ||
| # (C) British Crown Copyright 2013, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris 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 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. If not, see <http://www.gnu.org/licenses/>. | ||
| """Unit tests for the :mod:`iris.util` module.""" |
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,137 @@ | ||
| # (C) British Crown Copyright 2010 - 2013, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris 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 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. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Test function :meth:`iris.util.test_file_is_newer`. | ||
|
|
||
| """ | ||
| # import iris tests first so that some things can be initialised before | ||
| # importing anything else | ||
| import iris.tests as tests | ||
|
|
||
| import os.path | ||
| import shutil | ||
| import tempfile | ||
| import time | ||
| import unittest | ||
|
|
||
| from iris.util import file_is_newer_than | ||
|
|
||
|
|
||
| class TestFileIsNewer(tests.IrisTest): | ||
| """Test the :meth:`iris.util.file_is_newer_than` function.""" | ||
|
|
||
| def _name2path(self, filename): | ||
| """Add the temporary dirpath to a filename to make a full path.""" | ||
| return os.path.join(self.temp_dir, filename) | ||
|
|
||
| def setUp(self): | ||
| # make a temporary directory with testfiles of known timestamp order. | ||
| self.temp_dir = tempfile.mkdtemp('_testfiles_tempdir') | ||
| # define the names of some files to create | ||
| create_file_names = ['older_source_1', 'older_source_2', | ||
| 'example_result', | ||
| 'newer_source_1', 'newer_source_2'] | ||
| # create test files in given name order (!important!) | ||
| for file_name in create_file_names: | ||
| file_path = self._name2path(file_name) | ||
| with open(file_path, 'w') as file: | ||
| file.write('..content..') | ||
| # Needs a tiny pause to prevent possibly equal timestamps | ||
| time.sleep(0.002) | ||
|
|
||
| def tearDown(self): | ||
| # destroy whole contents of temporary directory | ||
| shutil.rmtree(self.temp_dir) | ||
|
|
||
| def _test(self, boolean_result, result_name, source_names): | ||
| """Test expected result of executing with given args.""" | ||
| # Make args into full paths | ||
| result_path = self._name2path(result_name) | ||
| if isinstance(source_names, basestring): | ||
| source_paths = self._name2path(source_names) | ||
| else: | ||
| source_paths = [self._name2path(name) | ||
| for name in source_names] | ||
| # Check result is as expected. | ||
| self.assertEqual( | ||
| boolean_result, | ||
| file_is_newer_than(result_path, source_paths)) | ||
|
|
||
| def test_no_sources(self): | ||
| self._test(True, 'example_result', []) | ||
|
|
||
| def test_string_ok(self): | ||
| self._test(True, 'example_result', 'older_source_1') | ||
|
|
||
| def test_string_fail(self): | ||
| self._test(False, 'example_result', 'newer_source_1') | ||
|
|
||
| def test_self_result(self): | ||
| # This fails, because same-timestamp is *not* acceptable. | ||
| self._test(False, 'example_result', 'example_result') | ||
|
|
||
| def test_single_ok(self): | ||
| self._test(True, 'example_result', ['older_source_2']) | ||
|
|
||
| def test_single_fail(self): | ||
| self._test(False, 'example_result', ['newer_source_2']) | ||
|
|
||
| def test_multiple_ok(self): | ||
| self._test(True, 'example_result', ['older_source_1', | ||
| 'older_source_2']) | ||
|
|
||
| def test_multiple_fail(self): | ||
| self._test(False, 'example_result', ['older_source_1', | ||
| 'older_source_2', | ||
| 'newer_source_1']) | ||
|
|
||
| def test_wild_ok(self): | ||
| self._test(True, 'example_result', ['older_sour*_*']) | ||
|
|
||
| def test_wild_fail(self): | ||
| self._test(False, 'example_result', ['older_sour*', 'newer_sour*']) | ||
|
|
||
| def test_error_missing_result(self): | ||
| try: | ||
| self._test(False, 'non_exist', ['older_sour*']) | ||
| except Exception as error: | ||
| pass | ||
| self.assertIsInstance(error, OSError) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As with the changes to test_load.py, this (and the other instances of this idiom in this file) would be better handled with the self.assertRaises context manager. |
||
| self.assertEqual(error.strerror, 'No such file or directory') | ||
| self.assertEqual(error.filename, self._name2path('non_exist')) | ||
|
|
||
| def test_error_missing_source(self): | ||
| try: | ||
| self._test(False, 'example_result', ['older_sour*', 'non_exist']) | ||
| except Exception as error: | ||
| pass | ||
| self.assertIsInstance(error, IOError) | ||
| self.assertTrue(error.message.startswith( | ||
| 'One or more of the files specified did not exist')) | ||
|
|
||
| def test_error_missing_wild(self): | ||
| try: | ||
| self._test(False, 'example_result', ['older_sour*', 'unknown_*']) | ||
| except Exception as error: | ||
| pass | ||
| self.assertIsInstance(error, IOError) | ||
| self.assertTrue(error.message.startswith( | ||
| 'One or more of the files specified did not exist')) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would normally be done with the
self.assertRaisescontext manager. See the unittest docs for an example which makes further assertions about the exception raised.