Debian Codegraph @

tests/test_h5plugin.py

raw
1 from __future__ import absolute_import, division, print_function, unicode_literals
2 import unittest
3 import os
4 import glob
5
6 import numpy as np
7 import h5py
8 import pytest
9 from subprocess import Popen, PIPE, STDOUT
10
11 import bitshuffle
12
13
14 plugin_dir = os.path.join(os.path.dirname(bitshuffle.__file__), "plugin")
15 os.environ["HDF5_PLUGIN_PATH"] = plugin_dir
16
17
18 H5VERSION = h5py.h5.get_libversion()
19 if H5VERSION[0] < 1 or (
20 H5VERSION[0] == 1
21 and (H5VERSION[1] < 8 or (H5VERSION[1] == 8 and H5VERSION[2] < 11))
22 ):
23 H51811P = False
24 else:
25 H51811P = True
26
27
28 class TestFilterPlugins(unittest.TestCase):
29 @pytest.mark.skipif(
30 "CIBUILDWHEEL" in os.environ,
31 reason="Can't build dynamic HDF5 plugin into bitshuffle wheel.",
32 )
33 def test_plugins(self):
34 if not H51811P:
35 return
36 shape = (32 * 1024,)
37 chunks = (4 * 1024,)
38 dtype = np.int64
39 data = np.arange(shape[0])
40 fname = "tmp_test_filters.h5"
41 f = h5py.File(fname, "w")
42 dset = f.create_dataset(
43 "range", shape=shape, dtype=dtype, chunks=chunks, compression=32008
44 )
45 dset[:] = data
46 f.close()
47
48 # Make sure the filters are working outside of h5py by calling h5dump
49 h5dump = Popen(["h5dump", fname], stdout=PIPE, stderr=STDOUT)
50 stdout, nothing = h5dump.communicate()
51 err = h5dump.returncode
52 self.assertEqual(err, 0)
53
54 f = h5py.File(fname, "r")
55 d = f["range"][:]
56 self.assertTrue(np.all(d == data))
57 f.close()
58
59 def tearDown(self):
60 files = glob.glob("tmp_test_*")
61 for f in files:
62 os.remove(f)
63
64
65 if __name__ == "__main__":
66 unittest.main()