diff --git a/src/openfermion/chem/molecular_data_test.py b/src/openfermion/chem/molecular_data_test.py index 7e12f328e..de7f5afcb 100644 --- a/src/openfermion/chem/molecular_data_test.py +++ b/src/openfermion/chem/molecular_data_test.py @@ -14,6 +14,7 @@ import os import tempfile import unittest +from unittest.mock import patch import h5py import numpy.random import scipy.linalg @@ -367,3 +368,14 @@ def test_load_no_general_calculations(self): # Load the molecule and check that general_calculations is empty. new_molecule = MolecularData(filename=filename) self.assertEqual(new_molecule.general_calculations, {}) + + def test_get_from_file_exceptions(self): + with patch('h5py.File') as mock_file: + mock_file.return_value.__enter__.return_value.__getitem__.side_effect = KeyError + data = self.molecule.get_from_file("nonexistent_property") + self.assertIsNone(data) + + with patch('h5py.File') as mock_file: + mock_file.side_effect = IOError + data = self.molecule.get_from_file("any_property") + self.assertIsNone(data)