Debian Codegraph @

tests/unit/build_tests/test_io_manager.py

raw
1 from abc import ABCMeta, abstractmethod
2
3 from hdmf.build import GroupBuilder, DatasetBuilder, ObjectMapper, BuildManager, TypeMap, ContainerConfigurationError
4 from hdmf.spec import GroupSpec, AttributeSpec, DatasetSpec, SpecCatalog, SpecNamespace, NamespaceCatalog
5 from hdmf.spec.spec import ZERO_OR_MANY
6 from hdmf.testing import TestCase
7
8 from tests.unit.helpers.utils import Foo, FooBucket, CORE_NAMESPACE
9
10
11 class FooMapper(ObjectMapper):
12 """Maps nested 'attr2' attribute on dataset 'my_data' to Foo.attr2 in constructor and attribute map
13 """
14
15 def __init__(self, spec):
16 super().__init__(spec)
17 my_data_spec = spec.get_dataset('my_data')
18 self.map_spec('attr2', my_data_spec.get_attribute('attr2'))
19
20
21 class TestBase(TestCase):
22
23 def setUp(self):
24 self.foo_spec = GroupSpec(
25 doc='A test group specification with a data type',
26 data_type_def='Foo',
27 datasets=[
28 DatasetSpec(
29 doc='an example dataset',
30 dtype='int',
31 name='my_data',
32 attributes=[
33 AttributeSpec(
34 name='attr2',
35 doc='an example integer attribute',
36 dtype='int'
37 )
38 ]
39 )
40 ],
41 attributes=[AttributeSpec('attr1', 'an example string attribute', 'text')]
42 )
43
44 self.spec_catalog = SpecCatalog()
45 self.spec_catalog.register_spec(self.foo_spec, 'test.yaml')
46 self.namespace = SpecNamespace(
47 'a test namespace',
48 CORE_NAMESPACE,
49 [{'source': 'test.yaml'}],
50 version='0.1.0',
51 catalog=self.spec_catalog)
52 self.namespace_catalog = NamespaceCatalog()
53 self.namespace_catalog.add_namespace(CORE_NAMESPACE, self.namespace)
54 self.type_map = TypeMap(self.namespace_catalog)
55 self.type_map.register_container_type(CORE_NAMESPACE, 'Foo', Foo)
56 self.type_map.register_map(Foo, FooMapper)
57 self.manager = BuildManager(self.type_map)
58
59
60 class TestBuildManager(TestBase):
61
62 def test_build(self):
63 container_inst = Foo('my_foo', list(range(10)), 'value1', 10)
64 expected = GroupBuilder(
65 'my_foo',
66 datasets={
67 'my_data':
68 DatasetBuilder(
69 'my_data',
70 list(range(10)),
71 attributes={'attr2': 10})},
72 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
73 'object_id': container_inst.object_id})
74 builder1 = self.manager.build(container_inst)
75 self.assertDictEqual(builder1, expected)
76
77 def test_build_memoization(self):
78 container_inst = Foo('my_foo', list(range(10)), 'value1', 10)
79 expected = GroupBuilder(
80 'my_foo',
81 datasets={
82 'my_data': DatasetBuilder(
83 'my_data',
84 list(range(10)),
85 attributes={'attr2': 10})},
86 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
87 'object_id': container_inst.object_id})
88 builder1 = self.manager.build(container_inst)
89 builder2 = self.manager.build(container_inst)
90 self.assertDictEqual(builder1, expected)
91 self.assertIs(builder1, builder2)
92
93 def test_construct(self):
94 builder = GroupBuilder(
95 'my_foo',
96 datasets={
97 'my_data': DatasetBuilder(
98 'my_data',
99 list(range(10)),
100 attributes={'attr2': 10})},
101 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
102 'object_id': -1})
103 container = self.manager.construct(builder)
104 self.assertListEqual(container.my_data, list(range(10)))
105 self.assertEqual(container.attr1, 'value1')
106 self.assertEqual(container.attr2, 10)
107
108 def test_construct_memoization(self):
109 builder = GroupBuilder(
110 'my_foo', datasets={'my_data': DatasetBuilder(
111 'my_data',
112 list(range(10)),
113 attributes={'attr2': 10})},
114 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
115 'object_id': -1})
116 container1 = self.manager.construct(builder)
117 container2 = self.manager.construct(builder)
118 self.assertIs(container1, container2)
119
120 def test_clear_cache(self):
121 container_inst = Foo('my_foo', list(range(10)), 'value1', 10)
122 builder1 = self.manager.build(container_inst)
123 self.manager.clear_cache()
124 builder2 = self.manager.build(container_inst)
125 self.assertIsNot(builder1, builder2)
126
127 builder = GroupBuilder(
128 'my_foo', datasets={'my_data': DatasetBuilder(
129 'my_data',
130 list(range(10)),
131 attributes={'attr2': 10})},
132 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
133 'object_id': -1})
134 container1 = self.manager.construct(builder)
135 self.manager.clear_cache()
136 container2 = self.manager.construct(builder)
137 self.assertIsNot(container1, container2)
138
139
140 class NestedBaseMixin(metaclass=ABCMeta):
141
142 def setUp(self):
143 super().setUp()
144 self.foo_bucket = FooBucket('test_foo_bucket', [
145 Foo('my_foo1', list(range(10)), 'value1', 10),
146 Foo('my_foo2', list(range(10, 20)), 'value2', 20)])
147 self.foo_builders = {
148 'my_foo1': GroupBuilder('my_foo1',
149 datasets={'my_data': DatasetBuilder(
150 'my_data',
151 list(range(10)),
152 attributes={'attr2': 10})},
153 attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
154 'object_id': self.foo_bucket.foos['my_foo1'].object_id}),
155 'my_foo2': GroupBuilder('my_foo2', datasets={'my_data':
156 DatasetBuilder(
157 'my_data',
158 list(range(10, 20)),
159 attributes={'attr2': 20})},
160 attributes={'attr1': 'value2', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo',
161 'object_id': self.foo_bucket.foos['my_foo2'].object_id})
162 }
163 self.setUpBucketBuilder()
164 self.setUpBucketSpec()
165
166 self.spec_catalog.register_spec(self.bucket_spec, 'test.yaml')
167 self.type_map.register_container_type(CORE_NAMESPACE, 'FooBucket', FooBucket)
168 self.type_map.register_map(FooBucket, self.setUpBucketMapper())
169 self.manager = BuildManager(self.type_map)
170
171 @abstractmethod
172 def setUpBucketBuilder(self):
173 raise NotImplementedError('Cannot run test unless setUpBucketBuilder is implemented')
174
175 @abstractmethod
176 def setUpBucketSpec(self):
177 raise NotImplementedError('Cannot run test unless setUpBucketSpec is implemented')
178
179 @abstractmethod
180 def setUpBucketMapper(self):
181 raise NotImplementedError('Cannot run test unless setUpBucketMapper is implemented')
182
183 def test_build(self):
184 ''' Test default mapping for an Container that has an Container as an attribute value '''
185 builder = self.manager.build(self.foo_bucket)
186 self.assertDictEqual(builder, self.bucket_builder)
187
188 def test_construct(self):
189 container = self.manager.construct(self.bucket_builder)
190 self.assertEqual(container, self.foo_bucket)
191
192
193 class TestNestedContainersNoSubgroups(NestedBaseMixin, TestBase):
194 '''
195 Test BuildManager.build and BuildManager.construct when the
196 Container contains other Containers, but does not keep them in
197 additional subgroups
198 '''
199
200 def setUpBucketBuilder(self):
201 self.bucket_builder = GroupBuilder(
202 'test_foo_bucket',
203 groups=self.foo_builders,
204 attributes={'namespace': CORE_NAMESPACE, 'data_type': 'FooBucket', 'object_id': self.foo_bucket.object_id})
205
206 def setUpBucketSpec(self):
207 self.bucket_spec = GroupSpec('A test group specification for a data type containing data type',
208 name="test_foo_bucket",
209 data_type_def='FooBucket',
210 groups=[GroupSpec(
211 'the Foos in this bucket',
212 data_type_inc='Foo',
213 quantity=ZERO_OR_MANY)])
214
215 def setUpBucketMapper(self):
216 return ObjectMapper
217
218
219 class TestNestedContainersSubgroup(NestedBaseMixin, TestBase):
220 '''
221 Test BuildManager.build and BuildManager.construct when the
222 Container contains other Containers that are stored in a subgroup
223 '''
224
225 def setUpBucketBuilder(self):
226 tmp_builder = GroupBuilder('foo_holder', groups=self.foo_builders)
227 self.bucket_builder = GroupBuilder(
228 'test_foo_bucket',
229 groups={'foos': tmp_builder},
230 attributes={'namespace': CORE_NAMESPACE, 'data_type': 'FooBucket', 'object_id': self.foo_bucket.object_id})
231
232 def setUpBucketSpec(self):
233 tmp_spec = GroupSpec(
234 'A subgroup for Foos',
235 name='foo_holder',
236 groups=[GroupSpec('the Foos in this bucket',
237 data_type_inc='Foo',
238 quantity=ZERO_OR_MANY)])
239 self.bucket_spec = GroupSpec('A test group specification for a data type containing data type',
240 name="test_foo_bucket",
241 data_type_def='FooBucket',
242 groups=[tmp_spec])
243
244 def setUpBucketMapper(self):
245 class BucketMapper(ObjectMapper):
246 def __init__(self, spec):
247 super().__init__(spec)
248 self.unmap(spec.get_group('foo_holder'))
249 self.map_spec('foos', spec.get_group('foo_holder').get_data_type('Foo'))
250
251 return BucketMapper
252
253
254 class TestNestedContainersSubgroupSubgroup(NestedBaseMixin, TestBase):
255 '''
256 Test BuildManager.build and BuildManager.construct when the
257 Container contains other Containers that are stored in a subgroup
258 in a subgroup
259 '''
260
261 def setUpBucketBuilder(self):
262 tmp_builder = GroupBuilder('foo_holder', groups=self.foo_builders)
263 tmp_builder = GroupBuilder('foo_holder_holder', groups={'foo_holder': tmp_builder})
264 self.bucket_builder = GroupBuilder(
265 'test_foo_bucket',
266 groups={'foo_holder': tmp_builder},
267 attributes={'namespace': CORE_NAMESPACE, 'data_type': 'FooBucket', 'object_id': self.foo_bucket.object_id})
268
269 def setUpBucketSpec(self):
270 tmp_spec = GroupSpec('A subgroup for Foos',
271 name='foo_holder',
272 groups=[GroupSpec('the Foos in this bucket',
273 data_type_inc='Foo',
274 quantity=ZERO_OR_MANY)])
275 tmp_spec = GroupSpec('A subgroup to hold the subgroup', name='foo_holder_holder', groups=[tmp_spec])
276 self.bucket_spec = GroupSpec('A test group specification for a data type containing data type',
277 name="test_foo_bucket",
278 data_type_def='FooBucket',
279 groups=[tmp_spec])
280
281 def setUpBucketMapper(self):
282 class BucketMapper(ObjectMapper):
283 def __init__(self, spec):
284 super().__init__(spec)
285 self.unmap(spec.get_group('foo_holder_holder'))
286 self.unmap(spec.get_group('foo_holder_holder').get_group('foo_holder'))
287 self.map_spec('foos', spec.get_group('foo_holder_holder').get_group('foo_holder').get_data_type('Foo'))
288
289 return BucketMapper
290
291 def test_build(self):
292 ''' Test default mapping for an Container that has an Container as an attribute value '''
293 builder = self.manager.build(self.foo_bucket)
294 self.assertDictEqual(builder, self.bucket_builder)
295
296 def test_construct(self):
297 container = self.manager.construct(self.bucket_builder)
298 self.assertEqual(container, self.foo_bucket)
299
300
301 class TestNoAttribute(TestBase):
302
303 def test_build(self):
304 """Test that an error is raised when a spec is mapped to a non-existent container attribute."""
305 class Unmapper(ObjectMapper):
306 def __init__(self, spec):
307 super().__init__(spec)
308 self.map_spec("unknown", self.spec.get_dataset('my_data'))
309
310 self.type_map.register_map(Foo, Unmapper) # override
311
312 container_inst = Foo('my_foo', list(range(10)), 'value1', 10)
313 msg = ("Foo 'my_foo' does not have attribute 'unknown' for mapping to spec: %s"
314 % self.foo_spec.get_dataset('my_data'))
315 with self.assertRaisesWith(ContainerConfigurationError, msg):
316 self.manager.build(container_inst)
317
318
319 class TestTypeMap(TestBase):
320
321 def test_get_ns_dt_missing(self):
322 bldr = GroupBuilder('my_foo', attributes={'attr1': 'value1'})
323 dt = self.type_map.get_builder_dt(bldr)
324 ns = self.type_map.get_builder_ns(bldr)
325 self.assertIsNone(dt)
326 self.assertIsNone(ns)
327
328 def test_get_ns_dt(self):
329 bldr = GroupBuilder('my_foo', attributes={'attr1': 'value1', 'namespace': 'CORE', 'data_type': 'Foo',
330 'object_id': -1})
331 dt = self.type_map.get_builder_dt(bldr)
332 ns = self.type_map.get_builder_ns(bldr)
333 self.assertEqual(dt, 'Foo')
334 self.assertEqual(ns, 'CORE')
335
336
337 class TestRetrieveContainerClass(TestBase):
338
339 def test_get_dt_container_cls(self):
340 ret = self.type_map.get_dt_container_cls(data_type="Foo")
341 self.assertIs(ret, Foo)
342
343 def test_get_dt_container_cls_no_namespace(self):
344 with self.assertRaisesWith(ValueError, "Namespace could not be resolved for data type 'Unknown'."):
345 self.type_map.get_dt_container_cls(data_type="Unknown")
346
347
348 # TODO:
349 class TestWildCardNamedSpecs(TestCase):
350 pass