-
Notifications
You must be signed in to change notification settings - Fork 413
Fix #481: make tests of Pubchem more robust #1219
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
base: main
Are you sure you want to change the base?
Changes from all commits
c94825f
3e44144
1b8114b
5941b88
63ddd11
89d7c69
95558d6
707a4d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,22 +9,84 @@ | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for pubchem.py.""" | ||
|
|
||
| import time | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import numpy | ||
| import pytest | ||
|
|
||
| from openfermion.chem.pubchem import geometry_from_pubchem | ||
| from openfermion.testing.testing_utils import module_importable | ||
|
|
||
|
|
||
| class MockCompound: | ||
| def __init__(self, atoms): | ||
| self._atoms = atoms | ||
|
|
||
| def to_dict(self, properties): | ||
| return {'atoms': self._atoms} | ||
|
|
||
|
|
||
| def mock_get_compounds(name, searchtype, record_type='2d'): | ||
| if name == 'water' and record_type == '3d': | ||
| return [ | ||
| MockCompound( | ||
| [ | ||
| {'aid': 1, 'number': 8, 'element': 'O', 'y': 0, 'z': 0, 'x': 0}, | ||
| {'aid': 2, 'number': 1, 'element': 'H', 'y': 0.8929, 'z': 0.2544, 'x': 0.2774}, | ||
| { | ||
| 'aid': 3, | ||
| 'number': 1, | ||
| 'element': 'H', | ||
| 'y': -0.2383, | ||
| 'z': -0.7169, | ||
| 'x': 0.6068, | ||
| }, | ||
| ] | ||
| ) | ||
| ] | ||
| if name == 'water' and record_type == '2d': | ||
| return [ | ||
| MockCompound( | ||
| [ | ||
| {'aid': 1, 'number': 8, 'element': 'O', 'y': -0.155, 'x': 2.5369}, | ||
| {'aid': 2, 'number': 1, 'element': 'H', 'y': 0.155, 'x': 3.0739}, | ||
| {'aid': 3, 'number': 1, 'element': 'H', 'y': 0.155, 'x': 2}, | ||
| ] | ||
| ) | ||
| ] | ||
| if name == 'helium' and record_type == '2d': | ||
| return [MockCompound([{'aid': 1, 'number': 2, 'element': 'He', 'y': 0, 'x': 2}])] | ||
| return [] | ||
|
|
||
|
|
||
| using_pubchempy = pytest.mark.skipif( | ||
| module_importable('pubchempy') is False, reason='Not detecting `pubchempy`.' | ||
| ) | ||
|
|
||
|
|
||
| @using_pubchempy | ||
| class OpenFermionPubChemTest(unittest.TestCase): | ||
| def _get_geometry_with_retries(self, name): | ||
|
Contributor
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. This is only used in If the live service proves to be a problem for the CI test, I suggest to add the pytest-retry plugin and decorate |
||
| import urllib.error | ||
|
|
||
| import pubchempy | ||
|
|
||
| max_retries = 3 | ||
| delay = 2 | ||
| for attempt in range(max_retries): | ||
| try: | ||
| return geometry_from_pubchem(name) | ||
| except (pubchempy.PubChemHTTPError, urllib.error.URLError): | ||
| if attempt == max_retries - 1: | ||
| raise | ||
| time.sleep(delay) | ||
|
|
||
| @patch('pubchempy.get_compounds', mock_get_compounds) | ||
| def test_water(self): | ||
| water_geometry = geometry_from_pubchem('water') | ||
| self.water_natoms = len(water_geometry) | ||
|
|
@@ -64,18 +126,21 @@ def test_water(self): | |
| self.assertTrue(water_bond_angle_low <= self.water_bond_angle) | ||
| self.assertTrue(water_bond_angle_high >= self.water_bond_angle) | ||
|
|
||
| @patch('pubchempy.get_compounds', mock_get_compounds) | ||
| def test_helium(self): | ||
| helium_geometry = geometry_from_pubchem('helium') | ||
| self.helium_natoms = len(helium_geometry) | ||
|
|
||
| helium_natoms = 1 | ||
| self.assertEqual(helium_natoms, self.helium_natoms) | ||
|
|
||
| @patch('pubchempy.get_compounds', mock_get_compounds) | ||
| def test_none(self): | ||
| none_geometry = geometry_from_pubchem('none') | ||
|
|
||
| self.assertIsNone(none_geometry) | ||
|
|
||
| @patch('pubchempy.get_compounds', mock_get_compounds) | ||
| def test_water_2d(self): | ||
| water_geometry = geometry_from_pubchem('water', structure='2d') | ||
| self.water_natoms = len(water_geometry) | ||
|
|
@@ -91,3 +156,60 @@ def test_water_2d(self): | |
|
|
||
| with pytest.raises(ValueError, match='Incorrect value for the argument structure'): | ||
| _ = geometry_from_pubchem('water', structure='foo') | ||
|
|
||
| @pytest.mark.integration | ||
| def test_geometry_from_pubchem_live_api(self): | ||
| water_geometry = self._get_geometry_with_retries('water') # pragma: no cover | ||
| self.assertEqual(len(water_geometry), 3) # pragma: no cover | ||
|
|
||
| @patch('time.sleep', return_value=None) | ||
| @patch('pubchempy.get_compounds') | ||
| def test_geometry_from_pubchem_retry_success(self, mock_get_compounds, mock_sleep): | ||
|
Contributor
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. This is testing a test helper |
||
| import pubchempy | ||
|
|
||
| mock_get_compounds.side_effect = [ | ||
| pubchempy.PubChemHTTPError(503, 'Server Busy', 'Testing'), | ||
| pubchempy.PubChemHTTPError(503, 'Server Busy', 'Testing'), | ||
| [ | ||
| MockCompound( | ||
| [ | ||
| {'aid': 1, 'number': 8, 'element': 'O', 'y': 0, 'z': 0, 'x': 0}, | ||
| { | ||
| 'aid': 2, | ||
| 'number': 1, | ||
| 'element': 'H', | ||
| 'y': 0.8929, | ||
| 'z': 0.2544, | ||
| 'x': 0.2774, | ||
| }, | ||
| { | ||
| 'aid': 3, | ||
| 'number': 1, | ||
| 'element': 'H', | ||
| 'y': -0.2383, | ||
| 'z': -0.7169, | ||
| 'x': 0.6068, | ||
| }, | ||
| ] | ||
| ) | ||
| ], | ||
| ] | ||
|
|
||
| water_geometry = self._get_geometry_with_retries('water') | ||
|
|
||
| self.assertEqual(len(water_geometry), 3) | ||
| self.assertEqual(mock_get_compounds.call_count, 3) | ||
| self.assertEqual(mock_sleep.call_count, 2) | ||
|
|
||
| @patch('time.sleep', return_value=None) | ||
| @patch('pubchempy.get_compounds') | ||
| def test_geometry_from_pubchem_retry_failure(self, mock_get_compounds, mock_sleep): | ||
|
Contributor
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. Same as above, please delete. |
||
| import pubchempy | ||
|
|
||
| mock_get_compounds.side_effect = pubchempy.PubChemHTTPError(503, 'Server Busy', 'Testing') | ||
|
|
||
| with self.assertRaises(pubchempy.PubChemHTTPError): | ||
| self._get_geometry_with_retries('water') | ||
|
|
||
| self.assertEqual(mock_get_compounds.call_count, 3) | ||
| self.assertEqual(mock_sleep.call_count, 2) | ||
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.
Consider rewriting with match statement.