diff --git a/test/unit/tool_util/verify/test_asserts.py b/test/unit/tool_util/verify/test_asserts.py index 787be6e0375..bd505e025c3 100644 --- a/test/unit/tool_util/verify/test_asserts.py +++ b/test/unit/tool_util/verify/test_asserts.py @@ -1,6 +1,9 @@ import tempfile -import h5py +try: + import h5py +except ImportError: + h5py = None import pytest from galaxy.tool_util.parser.xml import __parse_assert_list_from_elem @@ -254,40 +257,41 @@ VALID_XML = ''' ''' INVALID_XML = '' -with tempfile.NamedTemporaryFile() as tmp: - with h5py.File(tmp.name, "w") as h5fh: - h5fh.attrs['myfileattr'] = "myfileattrvalue" - h5fh.attrs['myfileattrint'] = 1 - dset = h5fh.create_dataset("myint", (100,), dtype='i') - dset.attrs['myintattr'] = "myintattrvalue" - grp = h5fh.create_group("mygroup") - grp.attrs['mygroupattr'] = "mygroupattrvalue" - grp.create_dataset("myfloat", (50,), dtype='f') - dset.attrs['myfloatattr'] = "myfloatattrvalue" - H5BYTES = open(tmp.name, "rb").read() +if h5py is not None: + with tempfile.NamedTemporaryFile() as tmp: + with h5py.File(tmp.name, "w") as h5fh: + h5fh.attrs['myfileattr'] = "myfileattrvalue" + h5fh.attrs['myfileattrint'] = 1 + dset = h5fh.create_dataset("myint", (100,), dtype='i') + dset.attrs['myintattr'] = "myintattrvalue" + grp = h5fh.create_group("mygroup") + grp.attrs['mygroupattr'] = "mygroupattrvalue" + grp.create_dataset("myfloat", (50,), dtype='f') + dset.attrs['myfloatattr'] = "myfloatattrvalue" + H5BYTES = open(tmp.name, "rb").read() -H5_HAS_H5_KEYS = """ - - - -""" -H5_HAS_H5_KEYS_NEGATIVE = """ - - - -""" -H5_HAS_ATTRIBUTE = """ - - - - -""" -H5_HAS_ATTRIBUTE_NEGATIVE = """ - - - - -""" + H5_HAS_H5_KEYS = """ + + + + """ + H5_HAS_H5_KEYS_NEGATIVE = """ + + + + """ + H5_HAS_ATTRIBUTE = """ + + + + + """ + H5_HAS_ATTRIBUTE_NEGATIVE = """ + + + + + """ with open("test-data/example-bag.zip", "rb") as zipfh: ZIPBYTES = zipfh.read() @@ -619,26 +623,6 @@ TESTS = [ XML_ELEMENT_TEXT.format(path="./elem/more", content_assert=''), VALID_XML, lambda x: "Expected text 'NOTBAR' in output ('BAR')" in x ), - # test has_h5_keys - ( - H5_HAS_H5_KEYS, H5BYTES, - lambda x: len(x) == 0 - ), - # test has_h5_keys .. negative - ( - H5_HAS_H5_KEYS_NEGATIVE, H5BYTES, - lambda x: "Not a HDF5 file or H5 keys missing:\n\t['mygroup', 'mygroup/myfloat', 'myint']\n\t['absent']" in x - ), - # test has_attribute - ( - H5_HAS_ATTRIBUTE, H5BYTES, - lambda x: len(x) == 0 - ), - # test has_attribute .. negative - ( - H5_HAS_ATTRIBUTE_NEGATIVE, H5BYTES, - lambda x: "Not a HDF5 file or H5 attributes do not match:\n\t[('myfileattr', 'myfileattrvalue'), ('myfileattrint', 1)]\n\n\t(myfileattr : wrong)" in x - ), # test has_archive_member with zip ( ARCHIVE_HAS_ARCHIVE_MEMBER.format(path="test-bag-fetch-http/bag-info.txt", content_assert=""), ZIPBYTES, @@ -686,6 +670,32 @@ TESTS = [ ), ] +if h5py is not None: + H5PY_TESTS = [ + # test has_h5_keys + ( + H5_HAS_H5_KEYS, H5BYTES, + lambda x: len(x) == 0 + ), + # test has_h5_keys .. negative + ( + H5_HAS_H5_KEYS_NEGATIVE, H5BYTES, + lambda x: "Not a HDF5 file or H5 keys missing:\n\t['mygroup', 'mygroup/myfloat', 'myint']\n\t['absent']" in x + ), + # test has_attribute + ( + H5_HAS_ATTRIBUTE, H5BYTES, + lambda x: len(x) == 0 + ), + # test has_attribute .. negative + ( + H5_HAS_ATTRIBUTE_NEGATIVE, H5BYTES, + lambda x: "Not a HDF5 file or H5 attributes do not match:\n\t[('myfileattr', 'myfileattrvalue'), ('myfileattrint', 1)]\n\n\t(myfileattr : wrong)" in x + ), + ] + TESTS.extend(H5PY_TESTS) + + TEST_IDS = [ 'has_n_columns success', 'has_n_columns failure', @@ -749,10 +759,6 @@ TEST_IDS = [ 'element_text failure', 'element_text with subassertion sucess', 'element_text with subassertion failure', - 'has_h5_keys', - 'has_h5_keys failure', - 'has_h5_attribute', - 'has_h5_attribute failure', 'has_archive_member zip', 'has_archive_member tar', 'has_archive_member non-archive', @@ -764,6 +770,15 @@ TEST_IDS = [ 'has_archive_member tar with content assertion', ] +if h5py is not None: + H5PY_TEST_IDS = [ + 'has_h5_keys', + 'has_h5_keys failure', + 'has_h5_attribute', + 'has_h5_attribute failure', + ] + TEST_IDS.extend(H5PY_TEST_IDS) + @pytest.mark.parametrize('assertion_xml,data,assert_func', TESTS, ids=TEST_IDS) def test_assertions(assertion_xml, data, assert_func):