Permalink
Browse files

Test installed module (#376)

* Allows installed testing and pubchem and jupy as optional deps.

* Install extra test files via manifest

* Install examples/

* Reorient examples/ for installed case

* Add proper skipping for ipynb

* Update _pubchem_test.py

* Rearrange and pep8ify pytest skipping
  • Loading branch information...
loriab authored and jarrodmcc committed Jun 26, 2018
1 parent 6477451 commit 7734093eeb853fa77e67826dc5463f465d96b4e2
@@ -4,5 +4,5 @@ include NOTICE
include README.rst
include requirements.txt
include setup.py
recursive-include src *.py *.hdf5 *.npy
include examples/*.py examples/*.ipynb
recursive-include src *.py *.hdf5 *.npy bad_type_operator.data geometry_example.txt
@@ -41,4 +41,11 @@
package_data={
'': [os.path.join('src', 'openfermion', 'data', '*.hdf5'),
os.path.join('src', 'openfermion', 'data', '*.npy')]
})
},
data_files=[('openfermion/examples', [
'examples/binary_code_transforms_demo.ipynb',
'examples/bosonic_operator_tutorial.ipynb',
'examples/jordan_wigner_and_bravyi_kitaev_transforms.ipynb',
'examples/openfermion_tutorial.ipynb',
'examples/performance_benchmarks.py'])],
)
@@ -18,24 +18,42 @@
import tempfile
import unittest
import nbformat
import numpy
import pytest
from openfermion.config import THIS_DIRECTORY
from openfermion.utils import *
using_nbtools = pytest.mark.skipif(not (module_importable('nbconvert') and
module_importable('ipykernel')),
reason='Not detecting `nbconvert`.')
using_matplotlib = pytest.mark.skipif(module_importable('matplotlib') is False,
reason='Not detecting `matplotlib`.')
class ExampleTest(unittest.TestCase):
"""Unit tests for example scripts."""
def setUp(self):
string_length = len(THIS_DIRECTORY)
self.directory = THIS_DIRECTORY[:(string_length - 15)] + 'examples/'
examples = THIS_DIRECTORY[:(string_length - 15)] + 'examples/'
if os.path.isdir(examples):
# source
self.directory = examples
else:
# installed
self.directory = THIS_DIRECTORY + '/examples/'
self.demo_name = 'openfermion_tutorial.ipynb'
self.binary_code_transforms_demo = 'binary_code_transforms_demo.ipynb'
self.jw_bk_demo = 'jordan_wigner_and_bravyi_kitaev_transforms.ipynb'
@using_matplotlib
@using_nbtools
def test_demo(self):
"""Unit test for demo."""
import nbformat
# Determine if python 2 or 3 is being used.
major_version, minor_version = sys.version_info[:2]
if major_version == 2 or minor_version == 6:
@@ -66,8 +84,11 @@ def test_demo(self):
errors = []
self.assertEqual(errors, [])
@using_nbtools
def test_binary_code_transforms_demo(self):
"""Unit test for demo."""
import nbformat
# Determine if python 2 or 3 is being used.
major_version, minor_version = sys.version_info[:2]
if major_version == 2 or minor_version == 6:
@@ -98,8 +119,11 @@ def test_binary_code_transforms_demo(self):
errors = []
self.assertEqual(errors, [])
@using_nbtools
def test_jordan_wigner_and_bravyi_kitaev_transforms_demo(self):
"""Unit test for demo."""
import nbformat
# Determine if python 2 or 3 is being used.
major_version, minor_version = sys.version_info[:2]
if major_version == 2 or minor_version == 6:
@@ -57,7 +57,8 @@
random_hermitian_matrix,
random_interaction_operator,
random_quadratic_hamiltonian,
random_unitary_matrix)
random_unitary_matrix,
module_importable)
from ._trotter_error import error_bound, error_operator
@@ -10,8 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pubchempy
def geometry_from_pubchem(name):
"""Function to extract geometry using the molecule's name from the PubChem
@@ -25,6 +23,7 @@ def geometry_from_pubchem(name):
geometry: a list of tuples giving the coordinates of each atom with
distances in Angstrom.
"""
import pubchempy
pubchempy_2d_molecule = pubchempy.get_compounds(name, 'name',
record_type='2d')
@@ -14,11 +14,18 @@
from __future__ import absolute_import
import numpy
import pytest
import unittest
from openfermion.utils import geometry_from_pubchem
from openfermion.utils import (geometry_from_pubchem,
module_importable)
using_pubchempy = pytest.mark.skipif(module_importable('pubchempy') is False,
reason='Not detecting `pubchempy`.')
@using_pubchempy
class OpenFermionPubChemTest(unittest.TestCase):
def test_water(self):
@@ -246,3 +246,26 @@ def __ne__(self, other):
def __hash__(self):
return hash(_ClassUnknownToSubjects)
def module_importable(module):
"""Without importing it, returns whether python module is importable.
Args:
module (string): Name of module.
Returns:
bool
"""
import sys
if sys.version_info >= (3, 4):
from importlib import util
plug_spec = util.find_spec(module)
else:
import pkgutil
plug_spec = pkgutil.find_loader(module)
if plug_spec is None:
return False
else:
return True

0 comments on commit 7734093

Please sign in to comment.