Skip to content

Commit dfa64ca

Browse files
Add test coverage for missing general_calculations in MolecularData.load (#1251)
This change removes the `pragma: nocover` and the associated TODO in `src/openfermion/chem/molecular_data.py` by adding a test case in `src/openfermion/chem/molecular_data_test.py` that triggers the branch where `general_calculations_keys` or `general_calculations_values` are missing from the HDF5 file. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 4a8eaed commit dfa64ca

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/openfermion/chem/molecular_data.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,10 @@ def load(self):
909909
key.tobytes().decode('utf-8'): value
910910
for key, value in zip(keys[...], values[...])
911911
}
912+
else:
913+
self.general_calculations = {}
912914
else:
913-
# TODO: test the no cover
914-
# no coverage here because pathway is check on
915-
# bad user generated file
916-
self.general_calculations = None # pragma: nocover
915+
self.general_calculations = {}
917916

918917
def get_from_file(self, property_name):
919918
"""Helper routine to re-open HDF5 file and pull out single property.

src/openfermion/chem/molecular_data_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"""Tests for molecular_data."""
1313

1414
import os
15+
import tempfile
1516
import unittest
17+
import h5py
1618
import numpy.random
1719
import scipy.linalg
1820
import numpy as np
@@ -347,3 +349,21 @@ def test_missing_calcs_for_integrals(self):
347349
molecule.get_k()
348350
with self.assertRaises(MissingCalculationError):
349351
molecule.get_antisym()
352+
353+
def test_load_no_general_calculations(self):
354+
# Make fake molecule.
355+
with tempfile.NamedTemporaryFile(suffix='.hdf5') as tmp_file:
356+
filename = tmp_file.name
357+
geometry = [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7414))]
358+
basis = '6-31g*'
359+
multiplicity = 1
360+
molecule = MolecularData(geometry, basis, multiplicity, filename=filename)
361+
molecule.save()
362+
363+
# Manually remove the general_calculations_keys dataset.
364+
with h5py.File(filename, 'r+') as f:
365+
del f['general_calculations_keys']
366+
367+
# Load the molecule and check that general_calculations is empty.
368+
new_molecule = MolecularData(filename=filename)
369+
self.assertEqual(new_molecule.general_calculations, {})

0 commit comments

Comments
 (0)