Skip to content

Commit 41e01cf

Browse files
authored
Add tests for additional h5py.File exceptions (#1258)
This adds tests for a couple of exceptions that were not yet covered. (Tests generated with the help of Jules.)
1 parent c3e9c4a commit 41e01cf

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/openfermion/chem/molecular_data_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import tempfile
1616
import unittest
17+
from unittest.mock import patch
1718
import h5py
1819
import numpy.random
1920
import scipy.linalg
@@ -367,3 +368,14 @@ def test_load_no_general_calculations(self):
367368
# Load the molecule and check that general_calculations is empty.
368369
new_molecule = MolecularData(filename=filename)
369370
self.assertEqual(new_molecule.general_calculations, {})
371+
372+
def test_get_from_file_exceptions(self):
373+
with patch('h5py.File') as mock_file:
374+
mock_file.return_value.__enter__.return_value.__getitem__.side_effect = KeyError
375+
data = self.molecule.get_from_file("nonexistent_property")
376+
self.assertIsNone(data)
377+
378+
with patch('h5py.File') as mock_file:
379+
mock_file.side_effect = IOError
380+
data = self.molecule.get_from_file("any_property")
381+
self.assertIsNone(data)

0 commit comments

Comments
 (0)