2222from openfermion .testing .testing_utils import module_importable
2323
2424
25+ class MockAtom :
26+ def __init__ (self , element , x , y , z = 0 ):
27+ self .element = element
28+ self .x = x
29+ self .y = y
30+ self .z = z
31+
32+
2533class MockCompound :
2634 def __init__ (self , atoms ):
27- self ._atoms = atoms
28-
29- def to_dict (self , properties ):
30- return {'atoms' : self ._atoms }
35+ self .atoms = [MockAtom (a ['element' ], a ['x' ], a ['y' ], a .get ('z' , 0 )) for a in atoms ]
3136
3237
3338def mock_get_compounds (name , searchtype , record_type = '2d' ):
@@ -70,20 +75,6 @@ def mock_get_compounds(name, searchtype, record_type='2d'):
7075
7176@using_pubchempy
7277class OpenFermionPubChemTest (unittest .TestCase ):
73- def _get_geometry_with_retries (self , name ):
74- import pubchempy
75- import urllib .error
76-
77- max_retries = 3
78- delay = 2
79- for attempt in range (max_retries ):
80- try :
81- return geometry_from_pubchem (name )
82- except (pubchempy .PubChemHTTPError , urllib .error .URLError ):
83- if attempt == max_retries - 1 :
84- raise
85- time .sleep (delay )
86-
8778 @patch ('pubchempy.get_compounds' , mock_get_compounds )
8879 def test_water (self ):
8980 water_geometry = geometry_from_pubchem ('water' )
@@ -161,7 +152,7 @@ def test_geometry_from_pubchem_live_api(self):
161152 except ImportError : # pragma: no cover
162153 return
163154
164- water_geometry = self . _get_geometry_with_retries ('water' )
155+ water_geometry = geometry_from_pubchem ('water' )
165156 self .assertEqual (len (water_geometry ), 3 )
166157
167158 def _get_mock_pubchem_error (self ):
@@ -181,6 +172,24 @@ def read(self):
181172
182173 return pubchempy .PubChemHTTPError (FakeHTTPError (503 , 'Server Busy' ))
183174
175+
176+ def _get_mock_pubchem_error (self ):
177+ import pubchempy
178+
179+ try :
180+ return pubchempy .PubChemHTTPError (503 , 'Server Busy' , ['busy' ])
181+ except TypeError :
182+
183+ class FakeHTTPError (Exception ):
184+ def __init__ (self , code , msg ):
185+ self .code = code
186+ self .reason = msg
187+
188+ def read (self ):
189+ return b'{"Fault": {"Details": ["busy"]}}'
190+
191+ return pubchempy .PubChemHTTPError (FakeHTTPError (503 , 'Server Busy' ))
192+
184193 @patch ('time.sleep' , return_value = None )
185194 @patch ('pubchempy.get_compounds' )
186195 def test_geometry_from_pubchem_retry_success (self , mock_get_compounds , mock_sleep ):
@@ -218,7 +227,7 @@ def test_geometry_from_pubchem_retry_success(self, mock_get_compounds, mock_slee
218227 ],
219228 ]
220229
221- water_geometry = self . _get_geometry_with_retries ('water' )
230+ water_geometry = geometry_from_pubchem ('water' )
222231
223232 self .assertEqual (len (water_geometry ), 3 )
224233 self .assertEqual (mock_get_compounds .call_count , 3 )
@@ -235,7 +244,7 @@ def test_geometry_from_pubchem_retry_failure(self, mock_get_compounds, mock_slee
235244 mock_get_compounds .side_effect = self ._get_mock_pubchem_error ()
236245
237246 with self .assertRaises (pubchempy .PubChemHTTPError ):
238- self . _get_geometry_with_retries ('water' )
247+ geometry_from_pubchem ('water' )
239248
240249 self .assertEqual (mock_get_compounds .call_count , 3 )
241250 self .assertEqual (mock_sleep .call_count , 2 )
0 commit comments