Debian Codegraph @

tests/unit/common/test_common_io.py

raw
1 from h5py import File
2
3 from hdmf.backends.hdf5 import HDF5IO
4 from hdmf.common import Container, get_manager, get_hdf5io
5 from hdmf.spec import NamespaceCatalog
6 from hdmf.testing import TestCase, remove_test_file
7
8 from tests.unit.helpers.utils import get_temp_filepath
9
10
11 class TestCacheSpec(TestCase):
12 """Test caching spec specifically with the namespaces provided by hdmf.common.
13
14 See also TestCacheSpec in tests/unit/test_io_hdf5_h5tools.py.
15 """
16
17 def setUp(self):
18 self.manager = get_manager()
19 self.path = get_temp_filepath()
20 self.container = Container('dummy')
21
22 def tearDown(self):
23 remove_test_file(self.path)
24
25 def test_write_no_cache_spec(self):
26 """Roundtrip test for not writing spec."""
27 with HDF5IO(self.path, manager=self.manager, mode="a") as io:
28 io.write(self.container, cache_spec=False)
29 with File(self.path, 'r') as f:
30 self.assertNotIn('specifications', f)
31
32 def test_write_cache_spec(self):
33 """Roundtrip test for writing spec and reading it back in."""
34 with HDF5IO(self.path, manager=self.manager, mode="a") as io:
35 io.write(self.container)
36 with File(self.path, 'r') as f:
37 self.assertIn('specifications', f)
38 self._check_spec()
39
40 def test_write_cache_spec_injected(self):
41 """Roundtrip test for writing spec and reading it back in when HDF5IO is passed an open h5py.File."""
42 with File(self.path, 'w') as fil:
43 with HDF5IO(self.path, manager=self.manager, file=fil, mode='a') as io:
44 io.write(self.container)
45 with File(self.path, 'r') as f:
46 self.assertIn('specifications', f)
47 self._check_spec()
48
49 def _check_spec(self):
50 ns_catalog = NamespaceCatalog()
51 HDF5IO.load_namespaces(ns_catalog, self.path)
52 self.maxDiff = None
53 for namespace in self.manager.namespace_catalog.namespaces:
54 with self.subTest(namespace=namespace):
55 original_ns = self.manager.namespace_catalog.get_namespace(namespace)
56 cached_ns = ns_catalog.get_namespace(namespace)
57 ns_fields_to_check = list(original_ns.keys())
58 ns_fields_to_check.remove('schema') # schema fields will not match, so reset
59 for ns_field in ns_fields_to_check:
60 with self.subTest(namespace_field=ns_field):
61 self.assertEqual(original_ns[ns_field], cached_ns[ns_field])
62 for dt in original_ns.get_registered_types():
63 with self.subTest(data_type=dt):
64 original_spec = original_ns.get_spec(dt)
65 cached_spec = cached_ns.get_spec(dt)
66 with self.subTest('Data type spec is read back in'):
67 self.assertIsNotNone(cached_spec)
68 with self.subTest('Cached spec matches original spec'):
69 self.assertDictEqual(original_spec, cached_spec)
70
71
72 class TestGetHdf5IO(TestCase):
73
74 def setUp(self):
75 self.path = get_temp_filepath()
76
77 def tearDown(self):
78 remove_test_file(self.path)
79
80 def test_gethdf5io(self):
81 """Test the get_hdf5io convenience method with manager=None."""
82 with get_hdf5io(self.path, "w") as io:
83 self.assertIsNotNone(io.manager)
84
85 def test_gethdf5io_manager(self):
86 """Test the get_hdf5io convenience method with manager set."""
87 manager = get_manager()
88 with get_hdf5io(self.path, "w", manager=manager) as io:
89 self.assertIs(io.manager, manager)