Debian Codegraph @

tests/unit/build_tests/mapper_tests/test_build_quantity.py

raw
1 from hdmf import Container, Data
2 from hdmf.build import (BuildManager, TypeMap, GroupBuilder, DatasetBuilder, LinkBuilder, ObjectMapper,
3 MissingRequiredBuildWarning, IncorrectQuantityBuildWarning)
4 from hdmf.spec import GroupSpec, DatasetSpec, LinkSpec, SpecCatalog, SpecNamespace, NamespaceCatalog
5 from hdmf.spec.spec import ZERO_OR_MANY, ONE_OR_MANY, ZERO_OR_ONE, DEF_QUANTITY
6 from hdmf.testing import TestCase
7 from hdmf.utils import docval, getargs
8
9 from tests.unit.helpers.utils import CORE_NAMESPACE
10
11
12 ##########################
13 # test all crosses:
14 # {
15 # untyped, named group with data-type-included groups / data-type-included datasets / links
16 # nested, type definition
17 # included groups / included datasets / links
18 # }
19 # x
20 # group/dataset/link with quantity {'*', '+', 1, 2, '?'}
21 # x
22 # builder with 2, 1, or 0 instances of the type, or 0 instances of the type with some instances of a mismatched type
23
24
25 class SimpleFoo(Container):
26 pass
27
28
29 class NotSimpleFoo(Container):
30 pass
31
32
33 class SimpleQux(Data):
34 pass
35
36
37 class NotSimpleQux(Data):
38 pass
39
40
41 class SimpleBucket(Container):
42
43 @docval({'name': 'name', 'type': str, 'doc': 'the name of this SimpleBucket'},
44 {'name': 'foos', 'type': list, 'doc': 'the SimpleFoo objects', 'default': list()},
45 {'name': 'quxs', 'type': list, 'doc': 'the SimpleQux objects', 'default': list()},
46 {'name': 'links', 'type': list, 'doc': 'another way to store SimpleFoo objects', 'default': list()})
47 def __init__(self, **kwargs):
48 name, foos, quxs, links = getargs('name', 'foos', 'quxs', 'links', kwargs)
49 super().__init__(name=name)
50 # note: collections of groups are unordered in HDF5, so make these dictionaries for keyed access
51 self.foos = {f.name: f for f in foos}
52 for f in foos:
53 f.parent = self
54 self.quxs = {q.name: q for q in quxs}
55 for q in quxs:
56 q.parent = self
57 self.links = {i.name: i for i in links}
58 for i in links:
59 i.parent = self
60
61
62 class BasicBucket(Container):
63
64 @docval({'name': 'name', 'type': str, 'doc': 'the name of this BasicBucket'},
65 {'name': 'untyped_dataset', 'type': 'scalar_data',
66 'doc': 'a scalar dataset within this BasicBucket', 'default': None},
67 {'name': 'untyped_array_dataset', 'type': ('data', 'array_data'),
68 'doc': 'an array dataset within this BasicBucket', 'default': None},)
69 def __init__(self, **kwargs):
70 name, untyped_dataset, untyped_array_dataset = getargs('name', 'untyped_dataset', 'untyped_array_dataset',
71 kwargs)
72 super().__init__(name=name)
73 self.untyped_dataset = untyped_dataset
74 self.untyped_array_dataset = untyped_array_dataset
75
76
77 class BuildQuantityMixin:
78 """Base test class mixin to set up the BuildManager."""
79
80 def setUpManager(self, specs):
81 spec_catalog = SpecCatalog()
82 schema_file = 'test.yaml'
83 for s in specs:
84 spec_catalog.register_spec(s, schema_file)
85 namespace = SpecNamespace(
86 doc='a test namespace',
87 name=CORE_NAMESPACE,
88 schema=[{'source': schema_file}],
89 version='0.1.0',
90 catalog=spec_catalog
91 )
92 namespace_catalog = NamespaceCatalog()
93 namespace_catalog.add_namespace(CORE_NAMESPACE, namespace)
94 type_map = TypeMap(namespace_catalog)
95 type_map.register_container_type(CORE_NAMESPACE, 'SimpleFoo', SimpleFoo)
96 type_map.register_container_type(CORE_NAMESPACE, 'NotSimpleFoo', NotSimpleFoo)
97 type_map.register_container_type(CORE_NAMESPACE, 'SimpleQux', SimpleQux)
98 type_map.register_container_type(CORE_NAMESPACE, 'NotSimpleQux', NotSimpleQux)
99 type_map.register_container_type(CORE_NAMESPACE, 'SimpleBucket', SimpleBucket)
100 type_map.register_map(SimpleBucket, self.setUpBucketMapper())
101 self.manager = BuildManager(type_map)
102
103 def _create_builder(self, container):
104 """Helper function to get a basic builder for a container with no subgroups/datasets/links."""
105 if isinstance(container, Container):
106 ret = GroupBuilder(
107 name=container.name,
108 attributes={'namespace': container.namespace,
109 'data_type': container.data_type,
110 'object_id': container.object_id}
111 )
112 else:
113 ret = DatasetBuilder(
114 name=container.name,
115 data=container.data,
116 attributes={'namespace': container.namespace,
117 'data_type': container.data_type,
118 'object_id': container.object_id}
119 )
120 return ret
121
122
123 class TypeIncUntypedGroupMixin:
124
125 def create_specs(self, quantity):
126 # Type SimpleBucket contains:
127 # - an untyped group "foo_holder" which contains [quantity] groups of data_type_inc SimpleFoo
128 # - an untyped group "qux_holder" which contains [quantity] datasets of data_type_inc SimpleQux
129 # - an untyped group "link_holder" which contains [quantity] links of target_type SimpleFoo
130 foo_spec = GroupSpec(
131 doc='A test group specification with a data type',
132 data_type_def='SimpleFoo',
133 )
134 not_foo_spec = GroupSpec(
135 doc='A test group specification with a data type',
136 data_type_def='NotSimpleFoo',
137 )
138 qux_spec = DatasetSpec(
139 doc='A test group specification with a data type',
140 data_type_def='SimpleQux',
141 )
142 not_qux_spec = DatasetSpec(
143 doc='A test group specification with a data type',
144 data_type_def='NotSimpleQux',
145 )
146 foo_inc_spec = GroupSpec(
147 doc='the SimpleFoos in this bucket',
148 data_type_inc='SimpleFoo',
149 quantity=quantity
150 )
151 foo_holder_spec = GroupSpec(
152 doc='An untyped subgroup for SimpleFoos',
153 name='foo_holder',
154 groups=[foo_inc_spec]
155 )
156 qux_inc_spec = DatasetSpec(
157 doc='the SimpleQuxs in this bucket',
158 data_type_inc='SimpleQux',
159 quantity=quantity
160 )
161 qux_holder_spec = GroupSpec(
162 doc='An untyped subgroup for SimpleQuxs',
163 name='qux_holder',
164 datasets=[qux_inc_spec]
165 )
166 foo_link_spec = LinkSpec(
167 doc='the links in this bucket',
168 target_type='SimpleFoo',
169 quantity=quantity
170 )
171 link_holder_spec = GroupSpec(
172 doc='An untyped subgroup for links',
173 name='link_holder',
174 links=[foo_link_spec]
175 )
176 bucket_spec = GroupSpec(
177 doc='A test group specification for a data type containing data type',
178 name="test_bucket",
179 data_type_def='SimpleBucket',
180 groups=[foo_holder_spec, qux_holder_spec, link_holder_spec]
181 )
182 return [foo_spec, not_foo_spec, qux_spec, not_qux_spec, bucket_spec]
183
184 def setUpBucketMapper(self):
185 class BucketMapper(ObjectMapper):
186 def __init__(self, spec):
187 super().__init__(spec)
188 self.unmap(spec.get_group('foo_holder'))
189 self.map_spec('foos', spec.get_group('foo_holder').get_data_type('SimpleFoo'))
190 self.unmap(spec.get_group('qux_holder'))
191 self.map_spec('quxs', spec.get_group('qux_holder').get_data_type('SimpleQux'))
192 self.unmap(spec.get_group('link_holder'))
193 self.map_spec('links', spec.get_group('link_holder').links[0])
194
195 return BucketMapper
196
197 def get_two_bucket_test(self):
198 foos = [SimpleFoo('my_foo1'), SimpleFoo('my_foo2')]
199 quxs = [SimpleQux('my_qux1', data=[1, 2, 3]), SimpleQux('my_qux2', data=[4, 5, 6])]
200 bucket = SimpleBucket(
201 name='test_bucket',
202 foos=foos,
203 quxs=quxs,
204 links=foos
205 )
206 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
207 foo2_builder = self._create_builder(bucket.foos['my_foo2'])
208 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
209 qux2_builder = self._create_builder(bucket.quxs['my_qux2'])
210 foo_holder_builder = GroupBuilder(
211 name='foo_holder',
212 groups={'my_foo1': foo1_builder,
213 'my_foo2': foo2_builder}
214 )
215 qux_holder_builder = GroupBuilder(
216 name='qux_holder',
217 datasets={'my_qux1': qux1_builder,
218 'my_qux2': qux2_builder}
219 )
220 foo1_link_builder = LinkBuilder(builder=foo1_builder)
221 foo2_link_builder = LinkBuilder(builder=foo2_builder)
222 link_holder_builder = GroupBuilder(
223 name='link_holder',
224 links={'my_foo1': foo1_link_builder,
225 'my_foo2': foo2_link_builder}
226 )
227 bucket_builder = GroupBuilder(
228 name='test_bucket',
229 groups={'foos': foo_holder_builder,
230 'quxs': qux_holder_builder,
231 'links': link_holder_builder},
232 attributes={'namespace': CORE_NAMESPACE,
233 'data_type': 'SimpleBucket',
234 'object_id': bucket.object_id}
235 )
236 return bucket, bucket_builder
237
238 def get_one_bucket_test(self):
239 foos = [SimpleFoo('my_foo1')]
240 quxs = [SimpleQux('my_qux1', data=[1, 2, 3])]
241 bucket = SimpleBucket(
242 name='test_bucket',
243 foos=foos,
244 quxs=quxs,
245 links=foos
246 )
247 foo1_builder = GroupBuilder(
248 name='my_foo1',
249 attributes={'namespace': CORE_NAMESPACE,
250 'data_type': 'SimpleFoo',
251 'object_id': bucket.foos['my_foo1'].object_id}
252 )
253 foo_holder_builder = GroupBuilder(
254 name='foo_holder',
255 groups={'my_foo1': foo1_builder}
256 )
257 qux1_builder = DatasetBuilder(
258 name='my_qux1',
259 data=[1, 2, 3],
260 attributes={'namespace': CORE_NAMESPACE,
261 'data_type': 'SimpleQux',
262 'object_id': bucket.quxs['my_qux1'].object_id}
263 )
264 qux_holder_builder = GroupBuilder(
265 name='qux_holder',
266 datasets={'my_qux1': qux1_builder}
267 )
268 foo1_link_builder = LinkBuilder(builder=foo1_builder)
269 link_holder_builder = GroupBuilder(
270 name='link_holder',
271 links={'my_foo1': foo1_link_builder}
272 )
273 bucket_builder = GroupBuilder(
274 name='test_bucket',
275 groups={'foos': foo_holder_builder,
276 'quxs': qux_holder_builder,
277 'links': link_holder_builder},
278 attributes={'namespace': CORE_NAMESPACE,
279 'data_type': 'SimpleBucket',
280 'object_id': bucket.object_id}
281 )
282 return bucket, bucket_builder
283
284 def get_zero_bucket_test(self):
285 bucket = SimpleBucket(
286 name='test_bucket'
287 )
288 foo_holder_builder = GroupBuilder(
289 name='foo_holder',
290 groups={}
291 )
292 qux_holder_builder = GroupBuilder(
293 name='qux_holder',
294 datasets={}
295 )
296 link_holder_builder = GroupBuilder(
297 name='link_holder',
298 links={}
299 )
300 bucket_builder = GroupBuilder(
301 name='test_bucket',
302 groups={'foos': foo_holder_builder,
303 'quxs': qux_holder_builder,
304 'links': link_holder_builder},
305 attributes={'namespace': CORE_NAMESPACE,
306 'data_type': 'SimpleBucket',
307 'object_id': bucket.object_id}
308 )
309 return bucket, bucket_builder
310
311 def get_mismatch_bucket_test(self):
312 foos = [NotSimpleFoo('my_foo1'), NotSimpleFoo('my_foo2')]
313 quxs = [NotSimpleQux('my_qux1', data=[1, 2, 3]), NotSimpleQux('my_qux2', data=[4, 5, 6])]
314 bucket = SimpleBucket(
315 name='test_bucket',
316 foos=foos,
317 quxs=quxs,
318 links=foos
319 )
320 foo_holder_builder = GroupBuilder(
321 name='foo_holder',
322 groups={}
323 )
324 qux_holder_builder = GroupBuilder(
325 name='qux_holder',
326 datasets={}
327 )
328 link_holder_builder = GroupBuilder(
329 name='link_holder',
330 links={}
331 )
332 bucket_builder = GroupBuilder(
333 name='test_bucket',
334 groups={'foos': foo_holder_builder,
335 'quxs': qux_holder_builder,
336 'links': link_holder_builder},
337 attributes={'namespace': CORE_NAMESPACE,
338 'data_type': 'SimpleBucket',
339 'object_id': bucket.object_id}
340 )
341 return bucket, bucket_builder
342
343
344 class TypeDefMixin:
345
346 def create_specs(self, quantity):
347 # Type SimpleBucket contains:
348 # - contains [quantity] groups of data_type_def SimpleFoo
349 # - contains [quantity] datasets of data_type_def SimpleQux
350 # NOTE: links do not have data_type_def, so leave them out of these tests
351 # NOTE: nested type definitions are strongly discouraged now
352 foo_spec = GroupSpec(
353 doc='the SimpleFoos in this bucket',
354 data_type_def='SimpleFoo',
355 quantity=quantity
356 )
357 qux_spec = DatasetSpec(
358 doc='the SimpleQuxs in this bucket',
359 data_type_def='SimpleQux',
360 quantity=quantity
361 )
362 not_foo_spec = GroupSpec(
363 doc='A test group specification with a data type',
364 data_type_def='NotSimpleFoo',
365 )
366 not_qux_spec = DatasetSpec(
367 doc='A test group specification with a data type',
368 data_type_def='NotSimpleQux',
369 )
370 bucket_spec = GroupSpec(
371 doc='A test group specification for a data type containing data type',
372 name="test_bucket",
373 data_type_def='SimpleBucket',
374 groups=[foo_spec],
375 datasets=[qux_spec]
376 )
377 return [foo_spec, not_foo_spec, qux_spec, not_qux_spec, bucket_spec]
378
379 def setUpBucketMapper(self):
380 class BucketMapper(ObjectMapper):
381 def __init__(self, spec):
382 super().__init__(spec)
383 self.map_spec('foos', spec.get_data_type('SimpleFoo'))
384 self.map_spec('quxs', spec.get_data_type('SimpleQux'))
385
386 return BucketMapper
387
388 def get_two_bucket_test(self):
389 foos = [SimpleFoo('my_foo1'), SimpleFoo('my_foo2')]
390 quxs = [SimpleQux('my_qux1', data=[1, 2, 3]), SimpleQux('my_qux2', data=[4, 5, 6])]
391 bucket = SimpleBucket(
392 name='test_bucket',
393 foos=foos,
394 quxs=quxs,
395 )
396 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
397 foo2_builder = self._create_builder(bucket.foos['my_foo2'])
398 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
399 qux2_builder = self._create_builder(bucket.quxs['my_qux2'])
400 bucket_builder = GroupBuilder(
401 name='test_bucket',
402 groups={'my_foo1': foo1_builder,
403 'my_foo2': foo2_builder},
404 datasets={'my_qux1': qux1_builder,
405 'my_qux2': qux2_builder},
406 attributes={'namespace': CORE_NAMESPACE,
407 'data_type': 'SimpleBucket',
408 'object_id': bucket.object_id}
409 )
410 return bucket, bucket_builder
411
412 def get_one_bucket_test(self):
413 foos = [SimpleFoo('my_foo1')]
414 quxs = [SimpleQux('my_qux1', data=[1, 2, 3])]
415 bucket = SimpleBucket(
416 name='test_bucket',
417 foos=foos,
418 quxs=quxs,
419 )
420 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
421 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
422 bucket_builder = GroupBuilder(
423 name='test_bucket',
424 groups={'my_foo1': foo1_builder},
425 datasets={'my_qux1': qux1_builder},
426 attributes={'namespace': CORE_NAMESPACE,
427 'data_type': 'SimpleBucket',
428 'object_id': bucket.object_id}
429 )
430 return bucket, bucket_builder
431
432 def get_zero_bucket_test(self):
433 bucket = SimpleBucket(
434 name='test_bucket'
435 )
436 bucket_builder = GroupBuilder(
437 name='test_bucket',
438 attributes={'namespace': CORE_NAMESPACE,
439 'data_type': 'SimpleBucket',
440 'object_id': bucket.object_id}
441 )
442 return bucket, bucket_builder
443
444 def get_mismatch_bucket_test(self):
445 foos = [NotSimpleFoo('my_foo1'), NotSimpleFoo('my_foo2')]
446 quxs = [NotSimpleQux('my_qux1', data=[1, 2, 3]), NotSimpleQux('my_qux2', data=[4, 5, 6])]
447 bucket = SimpleBucket(
448 name='test_bucket',
449 foos=foos,
450 quxs=quxs,
451 )
452 bucket_builder = GroupBuilder(
453 name='test_bucket',
454 attributes={'namespace': CORE_NAMESPACE,
455 'data_type': 'SimpleBucket',
456 'object_id': bucket.object_id}
457 )
458 return bucket, bucket_builder
459
460
461 class TypeIncMixin:
462
463 def create_specs(self, quantity):
464 # Type SimpleBucket contains:
465 # - [quantity] groups of data_type_inc SimpleFoo
466 # - [quantity] datasets of data_type_inc SimpleQux
467 # - [quantity] links of target_type SimpleFoo
468 foo_spec = GroupSpec(
469 doc='A test group specification with a data type',
470 data_type_def='SimpleFoo',
471 )
472 not_foo_spec = GroupSpec(
473 doc='A test group specification with a data type',
474 data_type_def='NotSimpleFoo',
475 )
476 qux_spec = DatasetSpec(
477 doc='A test group specification with a data type',
478 data_type_def='SimpleQux',
479 )
480 not_qux_spec = DatasetSpec(
481 doc='A test group specification with a data type',
482 data_type_def='NotSimpleQux',
483 )
484 foo_inc_spec = GroupSpec(
485 doc='the SimpleFoos in this bucket',
486 data_type_inc='SimpleFoo',
487 quantity=quantity
488 )
489 qux_inc_spec = DatasetSpec(
490 doc='the SimpleQuxs in this bucket',
491 data_type_inc='SimpleQux',
492 quantity=quantity
493 )
494 foo_link_spec = LinkSpec(
495 doc='the links in this bucket',
496 target_type='SimpleFoo',
497 quantity=quantity
498 )
499 bucket_spec = GroupSpec(
500 doc='A test group specification for a data type containing data type',
501 name="test_bucket",
502 data_type_def='SimpleBucket',
503 groups=[foo_inc_spec],
504 datasets=[qux_inc_spec],
505 links=[foo_link_spec]
506 )
507 return [foo_spec, not_foo_spec, qux_spec, not_qux_spec, bucket_spec]
508
509 def setUpBucketMapper(self):
510 class BucketMapper(ObjectMapper):
511 def __init__(self, spec):
512 super().__init__(spec)
513 self.map_spec('foos', spec.get_data_type('SimpleFoo'))
514 self.map_spec('quxs', spec.get_data_type('SimpleQux'))
515 self.map_spec('links', spec.links[0])
516
517 return BucketMapper
518
519 def get_two_bucket_test(self):
520 foos = [SimpleFoo('my_foo1'), SimpleFoo('my_foo2')]
521 quxs = [SimpleQux('my_qux1', data=[1, 2, 3]), SimpleQux('my_qux2', data=[4, 5, 6])]
522 # NOTE: unlike in the other tests, links cannot map to the same foos in bucket because of a name clash
523 links = [SimpleFoo('my_foo3'), SimpleFoo('my_foo4')]
524 bucket = SimpleBucket(
525 name='test_bucket',
526 foos=foos,
527 quxs=quxs,
528 links=links
529 )
530 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
531 foo2_builder = self._create_builder(bucket.foos['my_foo2'])
532 foo3_builder = self._create_builder(bucket.links['my_foo3'])
533 foo4_builder = self._create_builder(bucket.links['my_foo4'])
534 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
535 qux2_builder = self._create_builder(bucket.quxs['my_qux2'])
536 foo3_link_builder = LinkBuilder(builder=foo3_builder)
537 foo4_link_builder = LinkBuilder(builder=foo4_builder)
538 bucket_builder = GroupBuilder(
539 name='test_bucket',
540 groups={'my_foo1': foo1_builder,
541 'my_foo2': foo2_builder},
542 datasets={'my_qux1': qux1_builder,
543 'my_qux2': qux2_builder},
544 links={'my_foo3': foo3_link_builder,
545 'my_foo4': foo4_link_builder},
546 attributes={'namespace': CORE_NAMESPACE,
547 'data_type': 'SimpleBucket',
548 'object_id': bucket.object_id}
549 )
550 return bucket, bucket_builder
551
552 def get_one_bucket_test(self):
553 foos = [SimpleFoo('my_foo1')]
554 quxs = [SimpleQux('my_qux1', data=[1, 2, 3])]
555 # NOTE: unlike in the other tests, links cannot map to the same foos in bucket because of a name clash
556 links = [SimpleFoo('my_foo3')]
557 bucket = SimpleBucket(
558 name='test_bucket',
559 foos=foos,
560 quxs=quxs,
561 links=links
562 )
563 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
564 foo3_builder = self._create_builder(bucket.links['my_foo3'])
565 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
566 foo3_link_builder = LinkBuilder(builder=foo3_builder)
567 bucket_builder = GroupBuilder(
568 name='test_bucket',
569 groups={'my_foo1': foo1_builder},
570 datasets={'my_qux1': qux1_builder},
571 links={'my_foo1': foo3_link_builder},
572 attributes={'namespace': CORE_NAMESPACE,
573 'data_type': 'SimpleBucket',
574 'object_id': bucket.object_id}
575 )
576 return bucket, bucket_builder
577
578 def get_zero_bucket_test(self):
579 bucket = SimpleBucket(
580 name='test_bucket'
581 )
582 bucket_builder = GroupBuilder(
583 name='test_bucket',
584 attributes={'namespace': CORE_NAMESPACE,
585 'data_type': 'SimpleBucket',
586 'object_id': bucket.object_id}
587 )
588 return bucket, bucket_builder
589
590 def get_mismatch_bucket_test(self):
591 foos = [NotSimpleFoo('my_foo1'), NotSimpleFoo('my_foo2')]
592 quxs = [NotSimpleQux('my_qux1', data=[1, 2, 3]), NotSimpleQux('my_qux2', data=[4, 5, 6])]
593 links = [NotSimpleFoo('my_foo1'), NotSimpleFoo('my_foo2')]
594 bucket = SimpleBucket(
595 name='test_bucket',
596 foos=foos,
597 quxs=quxs,
598 links=links
599 )
600 bucket_builder = GroupBuilder(
601 name='test_bucket',
602 attributes={'namespace': CORE_NAMESPACE,
603 'data_type': 'SimpleBucket',
604 'object_id': bucket.object_id}
605 )
606 return bucket, bucket_builder
607
608
609 class ZeroOrManyMixin:
610
611 def setUp(self):
612 specs = self.create_specs(ZERO_OR_MANY)
613 self.setUpManager(specs)
614
615 def test_build_two(self):
616 """Test building a container which contains multiple containers as the spec allows."""
617 bucket, bucket_builder = self.get_two_bucket_test()
618 builder = self.manager.build(bucket)
619 self.assertDictEqual(builder, bucket_builder)
620
621 def test_build_one(self):
622 """Test building a container which contains one container as the spec allows."""
623 bucket, bucket_builder = self.get_one_bucket_test()
624 builder = self.manager.build(bucket)
625 self.assertDictEqual(builder, bucket_builder)
626
627 def test_build_zero(self):
628 """Test building a container which contains no containers as the spec allows."""
629 bucket, bucket_builder = self.get_zero_bucket_test()
630 builder = self.manager.build(bucket)
631 self.assertDictEqual(builder, bucket_builder)
632
633 def test_build_mismatch(self):
634 """Test building a container which contains no containers that match the spec as the spec allows."""
635 bucket, bucket_builder = self.get_mismatch_bucket_test()
636 builder = self.manager.build(bucket)
637 self.assertDictEqual(builder, bucket_builder)
638
639
640 class OneOrManyMixin:
641
642 def setUp(self):
643 specs = self.create_specs(ONE_OR_MANY)
644 self.setUpManager(specs)
645
646 def test_build_two(self):
647 """Test building a container which contains multiple containers as the spec allows."""
648 bucket, bucket_builder = self.get_two_bucket_test()
649 builder = self.manager.build(bucket)
650 self.assertDictEqual(builder, bucket_builder)
651
652 def test_build_one(self):
653 """Test building a container which contains one container as the spec allows."""
654 bucket, bucket_builder = self.get_one_bucket_test()
655 builder = self.manager.build(bucket)
656 self.assertDictEqual(builder, bucket_builder)
657
658 def test_build_zero(self):
659 """Test building a container which contains no containers as the spec allows."""
660 bucket, bucket_builder = self.get_zero_bucket_test()
661 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
662 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
663 builder = self.manager.build(bucket)
664 self.assertDictEqual(builder, bucket_builder)
665
666 def test_build_mismatch(self):
667 """Test building a container which contains no containers that match the spec as the spec allows."""
668 bucket, bucket_builder = self.get_mismatch_bucket_test()
669 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
670 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
671 builder = self.manager.build(bucket)
672 self.assertDictEqual(builder, bucket_builder)
673
674
675 class OneMixin:
676
677 def setUp(self):
678 specs = self.create_specs(DEF_QUANTITY)
679 self.setUpManager(specs)
680
681 def test_build_two(self):
682 """Test building a container which contains multiple containers as the spec allows."""
683 bucket, bucket_builder = self.get_two_bucket_test()
684 msg = r"SimpleBucket 'test_bucket' has 2 values for attribute '.*' but spec allows 1\."
685 with self.assertWarnsRegex(IncorrectQuantityBuildWarning, msg):
686 builder = self.manager.build(bucket)
687 self.assertDictEqual(builder, bucket_builder)
688
689 def test_build_one(self):
690 """Test building a container which contains one container as the spec allows."""
691 bucket, bucket_builder = self.get_one_bucket_test()
692 builder = self.manager.build(bucket)
693 self.assertDictEqual(builder, bucket_builder)
694
695 def test_build_zero(self):
696 """Test building a container which contains no containers as the spec allows."""
697 bucket, bucket_builder = self.get_zero_bucket_test()
698 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
699 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
700 builder = self.manager.build(bucket)
701 self.assertDictEqual(builder, bucket_builder)
702
703 def test_build_mismatch(self):
704 """Test building a container which contains no containers that match the spec as the spec allows."""
705 bucket, bucket_builder = self.get_mismatch_bucket_test()
706 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
707 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
708 builder = self.manager.build(bucket)
709 self.assertDictEqual(builder, bucket_builder)
710
711
712 class TwoMixin:
713
714 def setUp(self):
715 specs = self.create_specs(2)
716 self.setUpManager(specs)
717
718 def test_build_two(self):
719 """Test building a container which contains multiple containers as the spec allows."""
720 bucket, bucket_builder = self.get_two_bucket_test()
721 builder = self.manager.build(bucket)
722 self.assertDictEqual(builder, bucket_builder)
723
724 def test_build_one(self):
725 """Test building a container which contains one container as the spec allows."""
726 bucket, bucket_builder = self.get_one_bucket_test()
727 msg = r"SimpleBucket 'test_bucket' has 1 values for attribute '.*' but spec allows 2\."
728 with self.assertWarnsRegex(IncorrectQuantityBuildWarning, msg):
729 builder = self.manager.build(bucket)
730 self.assertDictEqual(builder, bucket_builder)
731
732 def test_build_zero(self):
733 """Test building a container which contains no containers as the spec allows."""
734 bucket, bucket_builder = self.get_zero_bucket_test()
735 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
736 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
737 builder = self.manager.build(bucket)
738 self.assertDictEqual(builder, bucket_builder)
739
740 def test_build_mismatch(self):
741 """Test building a container which contains no containers that match the spec as the spec allows."""
742 bucket, bucket_builder = self.get_mismatch_bucket_test()
743 msg = r"SimpleBucket 'test_bucket' is missing required value for attribute '.*'\."
744 with self.assertWarnsRegex(MissingRequiredBuildWarning, msg):
745 builder = self.manager.build(bucket)
746 self.assertDictEqual(builder, bucket_builder)
747
748
749 class ZeroOrOneMixin:
750
751 def setUp(self):
752 specs = self.create_specs(ZERO_OR_ONE)
753 self.setUpManager(specs)
754
755 def test_build_two(self):
756 """Test building a container which contains multiple containers as the spec allows."""
757 bucket, bucket_builder = self.get_two_bucket_test()
758 msg = r"SimpleBucket 'test_bucket' has 2 values for attribute '.*' but spec allows '\?'\."
759 with self.assertWarnsRegex(IncorrectQuantityBuildWarning, msg):
760 builder = self.manager.build(bucket)
761 self.assertDictEqual(builder, bucket_builder)
762
763 def test_build_one(self):
764 """Test building a container which contains one container as the spec allows."""
765 bucket, bucket_builder = self.get_one_bucket_test()
766 builder = self.manager.build(bucket)
767 self.assertDictEqual(builder, bucket_builder)
768
769 def test_build_zero(self):
770 """Test building a container which contains no containers as the spec allows."""
771 bucket, bucket_builder = self.get_zero_bucket_test()
772 builder = self.manager.build(bucket)
773 self.assertDictEqual(builder, bucket_builder)
774
775 def test_build_mismatch(self):
776 """Test building a container which contains no containers that match the spec as the spec allows."""
777 bucket, bucket_builder = self.get_mismatch_bucket_test()
778 builder = self.manager.build(bucket)
779 self.assertDictEqual(builder, bucket_builder)
780
781
782 # Untyped group with included groups / included datasets / links with quantity {'*', '+', 1, 2 '?'}
783
784 class TestBuildZeroOrManyTypeIncUntypedGroup(ZeroOrManyMixin, TypeIncUntypedGroupMixin, BuildQuantityMixin, TestCase):
785 """Test building a group that has an untyped subgroup with a data type inc subgroup/dataset/link with quantity '*'
786 """
787 pass
788
789
790 class TestBuildOneOrManyTypeIncUntypedGroup(OneOrManyMixin, TypeIncUntypedGroupMixin, BuildQuantityMixin, TestCase):
791 """Test building a group that has an untyped subgroup with a data type inc subgroup/dataset/link with quantity '+'
792 """
793 pass
794
795
796 class TestBuildOneTypeIncUntypedGroup(OneMixin, TypeIncUntypedGroupMixin, BuildQuantityMixin, TestCase):
797 """Test building a group that has an untyped subgroup with a data type inc subgroup/dataset/link with quantity 1
798 """
799 pass
800
801
802 class TestBuildTwoTypeIncUntypedGroup(TwoMixin, TypeIncUntypedGroupMixin, BuildQuantityMixin, TestCase):
803 """Test building a group that has an untyped subgroup with a data type inc subgroup/dataset/link with quantity 2
804 """
805 pass
806
807
808 class TestBuildZeroOrOneTypeIncUntypedGroup(ZeroOrOneMixin, TypeIncUntypedGroupMixin, BuildQuantityMixin, TestCase):
809 """Test building a group that has an untyped subgroup with a data type inc subgroup/dataset/link with quantity '?'
810 """
811 pass
812
813
814 # Nested type definition of group/dataset with quantity {'*', '+', 1, 2, '?'}
815
816 class TestBuildZeroOrManyTypeDef(ZeroOrManyMixin, TypeDefMixin, BuildQuantityMixin, TestCase):
817 """Test building a group that has a nested type def with quantity '*'
818 """
819 pass
820
821
822 class TestBuildOneOrManyTypeDef(OneOrManyMixin, TypeDefMixin, BuildQuantityMixin, TestCase):
823 """Test building a group that has a nested type def with quantity '+'
824 """
825 pass
826
827
828 class TestBuildOneTypeDef(OneMixin, TypeDefMixin, BuildQuantityMixin, TestCase):
829 """Test building a group that has a nested type def with quantity 1
830 """
831 pass
832
833
834 class TestBuildTwoTypeDef(TwoMixin, TypeDefMixin, BuildQuantityMixin, TestCase):
835 """Test building a group that has a nested type def with quantity 2
836 """
837 pass
838
839
840 class TestBuildZeroOrOneTypeDef(ZeroOrOneMixin, TypeDefMixin, BuildQuantityMixin, TestCase):
841 """Test building a group that has a nested type def with quantity '?'
842 """
843 pass
844
845
846 # Included groups / included datasets / links with quantity {'*', '+', 1, 2, '?'}
847
848 class TestBuildZeroOrManyTypeInc(ZeroOrManyMixin, TypeIncMixin, BuildQuantityMixin, TestCase):
849 """Test building a group that has a data type inc subgroup/dataset/link with quantity '*'
850 """
851 pass
852
853
854 class TestBuildOneOrManyTypeInc(OneOrManyMixin, TypeIncMixin, BuildQuantityMixin, TestCase):
855 """Test building a group that has a data type inc subgroup/dataset/link with quantity '+'
856 """
857 pass
858
859
860 class TestBuildOneTypeInc(OneMixin, TypeIncMixin, BuildQuantityMixin, TestCase):
861 """Test building a group that has a data type inc subgroup/dataset/link with quantity 1
862 """
863 pass
864
865
866 class TestBuildTwoTypeInc(TwoMixin, TypeIncMixin, BuildQuantityMixin, TestCase):
867 """Test building a group that has a data type inc subgroup/dataset/link with quantity 2
868 """
869 pass
870
871
872 class TestBuildZeroOrOneTypeInc(ZeroOrOneMixin, TypeIncMixin, BuildQuantityMixin, TestCase):
873 """Test building a group that has a data type inc subgroup/dataset/link with quantity '?'
874 """
875 pass
876
877
878 # Untyped group/dataset with quantity {1, '?'}
879
880 class UntypedMixin:
881
882 def setUpManager(self, specs):
883 spec_catalog = SpecCatalog()
884 schema_file = 'test.yaml'
885 for s in specs:
886 spec_catalog.register_spec(s, schema_file)
887 namespace = SpecNamespace(
888 doc='a test namespace',
889 name=CORE_NAMESPACE,
890 schema=[{'source': schema_file}],
891 version='0.1.0',
892 catalog=spec_catalog
893 )
894 namespace_catalog = NamespaceCatalog()
895 namespace_catalog.add_namespace(CORE_NAMESPACE, namespace)
896 type_map = TypeMap(namespace_catalog)
897 type_map.register_container_type(CORE_NAMESPACE, 'BasicBucket', BasicBucket)
898 self.manager = BuildManager(type_map)
899
900 def create_specs(self, quantity):
901 # Type BasicBucket contains:
902 # - [quantity] untyped group
903 # - [quantity] untyped dataset
904 # - [quantity] untyped array dataset
905 # quantity can be only '?' or 1
906 untyped_group_spec = GroupSpec(
907 doc='A test group specification with no data type',
908 name='untyped_group',
909 quantity=quantity,
910 )
911 untyped_dataset_spec = DatasetSpec(
912 doc='A test dataset specification with no data type',
913 name='untyped_dataset',
914 dtype='int',
915 quantity=quantity,
916 )
917 untyped_array_dataset_spec = DatasetSpec(
918 doc='A test dataset specification with no data type',
919 name='untyped_array_dataset',
920 dtype='int',
921 dims=[None],
922 shape=[None],
923 quantity=quantity,
924 )
925 basic_bucket_spec = GroupSpec(
926 doc='A test group specification for a data type containing data type',
927 name="test_bucket",
928 data_type_def='BasicBucket',
929 groups=[untyped_group_spec],
930 datasets=[untyped_dataset_spec, untyped_array_dataset_spec],
931 )
932 return [basic_bucket_spec]
933
934
935 class TestBuildOneUntyped(UntypedMixin, TestCase):
936 """Test building a group that has an untyped subgroup/dataset with quantity 1.
937 """
938 def setUp(self):
939 specs = self.create_specs(DEF_QUANTITY)
940 self.setUpManager(specs)
941
942 def test_build_data(self):
943 """Test building a container which contains an untyped empty subgroup and an untyped non-empty dataset."""
944 bucket = BasicBucket(name='test_bucket', untyped_dataset=3, untyped_array_dataset=[3])
945 # a required untyped empty group builder will be created by default
946 untyped_group_builder = GroupBuilder(name='untyped_group')
947 untyped_dataset_builder = DatasetBuilder(name='untyped_dataset', data=3)
948 untyped_array_dataset_builder = DatasetBuilder(name='untyped_array_dataset', data=[3])
949 bucket_builder = GroupBuilder(
950 name='test_bucket',
951 groups={'untyped_group': untyped_group_builder},
952 datasets={'untyped_dataset': untyped_dataset_builder,
953 'untyped_array_dataset': untyped_array_dataset_builder},
954 attributes={'namespace': CORE_NAMESPACE,
955 'data_type': 'BasicBucket',
956 'object_id': bucket.object_id}
957 )
958 builder = self.manager.build(bucket)
959 self.assertDictEqual(builder, bucket_builder)
960
961 def test_build_empty_data(self):
962 """Test building a container which contains an untyped empty subgroup and an untyped empty dataset."""
963 bucket = BasicBucket(name='test_bucket')
964 # a required untyped empty group builder will be created by default
965 untyped_group_builder = GroupBuilder(name='untyped_group')
966 # a required untyped empty dataset builder will NOT be created by default
967 bucket_builder = GroupBuilder(
968 name='test_bucket',
969 groups={'untyped_group': untyped_group_builder},
970 attributes={'namespace': CORE_NAMESPACE,
971 'data_type': 'BasicBucket',
972 'object_id': bucket.object_id}
973 )
974 msg = "BasicBucket 'test_bucket' is missing required value for attribute 'untyped_dataset'."
975 # also raises "BasicBucket 'test_bucket' is missing required value for attribute 'untyped_array_dataset'."
976 with self.assertWarnsWith(MissingRequiredBuildWarning, msg):
977 builder = self.manager.build(bucket)
978 self.assertDictEqual(builder, bucket_builder)
979
980
981 class TestBuildZeroOrOneUntyped(UntypedMixin, TestCase):
982 """Test building a group that has an untyped subgroup/dataset with quantity '?'.
983 """
984 def setUp(self):
985 specs = self.create_specs(ZERO_OR_ONE)
986 self.setUpManager(specs)
987
988 def test_build_data(self):
989 """Test building a container which contains an untyped empty subgroup and an untyped non-empty dataset."""
990 bucket = BasicBucket(name='test_bucket', untyped_dataset=3, untyped_array_dataset=[3])
991 # an optional untyped empty group builder will NOT be created by default
992 untyped_dataset_builder = DatasetBuilder(name='untyped_dataset', data=3)
993 untyped_array_dataset_builder = DatasetBuilder(name='untyped_array_dataset', data=[3])
994 bucket_builder = GroupBuilder(
995 name='test_bucket',
996 datasets={'untyped_dataset': untyped_dataset_builder,
997 'untyped_array_dataset': untyped_array_dataset_builder},
998 attributes={'namespace': CORE_NAMESPACE,
999 'data_type': 'BasicBucket',
1000 'object_id': bucket.object_id}
1001 )
1002 builder = self.manager.build(bucket)
1003 self.assertDictEqual(builder, bucket_builder)
1004
1005 def test_build_empty_data(self):
1006 """Test building a container which contains an untyped empty subgroup and an untyped empty dataset."""
1007 bucket = BasicBucket(name='test_bucket')
1008 # an optional untyped empty group builder will NOT be created by default
1009 # an optional untyped empty dataset builder will NOT be created by default
1010 bucket_builder = GroupBuilder(
1011 name='test_bucket',
1012 attributes={'namespace': CORE_NAMESPACE,
1013 'data_type': 'BasicBucket',
1014 'object_id': bucket.object_id}
1015 )
1016 builder = self.manager.build(bucket)
1017 self.assertDictEqual(builder, bucket_builder)
1018
1019
1020 # Multiple allowed types
1021
1022 class TestBuildMultiTypeInc(BuildQuantityMixin, TestCase):
1023 """Test build process when a groupspec allows multiple groups/datasets/links with different data types / targets.
1024 """
1025
1026 def setUp(self):
1027 specs = self.create_specs(ZERO_OR_MANY)
1028 self.setUpManager(specs)
1029
1030 def create_specs(self, quantity):
1031 # Type SimpleBucket contains:
1032 # - [quantity] groups of data_type_inc SimpleFoo and [quantity] group of data_type_inc NotSimpleFoo
1033 # - [quantity] datasets of data_type_inc SimpleQux and [quantity] datasets of data_type_inc NotSimpleQux
1034 # - [quantity] links of target_type SimpleFoo and [quantity] links of target_type NotSimpleFoo
1035 foo_spec = GroupSpec(
1036 doc='A test group specification with a data type',
1037 data_type_def='SimpleFoo',
1038 )
1039 not_foo_spec = GroupSpec(
1040 doc='A test group specification with a data type',
1041 data_type_def='NotSimpleFoo',
1042 )
1043 qux_spec = DatasetSpec(
1044 doc='A test group specification with a data type',
1045 data_type_def='SimpleQux',
1046 )
1047 not_qux_spec = DatasetSpec(
1048 doc='A test group specification with a data type',
1049 data_type_def='NotSimpleQux',
1050 )
1051 foo_inc_spec = GroupSpec(
1052 doc='the SimpleFoos in this bucket',
1053 data_type_inc='SimpleFoo',
1054 quantity=quantity
1055 )
1056 not_foo_inc_spec = GroupSpec(
1057 doc='the SimpleFoos in this bucket',
1058 data_type_inc='NotSimpleFoo',
1059 quantity=quantity
1060 )
1061 qux_inc_spec = DatasetSpec(
1062 doc='the SimpleQuxs in this bucket',
1063 data_type_inc='SimpleQux',
1064 quantity=quantity
1065 )
1066 not_qux_inc_spec = DatasetSpec(
1067 doc='the SimpleQuxs in this bucket',
1068 data_type_inc='NotSimpleQux',
1069 quantity=quantity
1070 )
1071 foo_link_spec = LinkSpec(
1072 doc='the links in this bucket',
1073 target_type='SimpleFoo',
1074 quantity=quantity
1075 )
1076 not_foo_link_spec = LinkSpec(
1077 doc='the links in this bucket',
1078 target_type='NotSimpleFoo',
1079 quantity=quantity
1080 )
1081 bucket_spec = GroupSpec(
1082 doc='A test group specification for a data type containing data type',
1083 name="test_bucket",
1084 data_type_def='SimpleBucket',
1085 groups=[foo_inc_spec, not_foo_inc_spec],
1086 datasets=[qux_inc_spec, not_qux_inc_spec],
1087 links=[foo_link_spec, not_foo_link_spec]
1088 )
1089 return [foo_spec, not_foo_spec, qux_spec, not_qux_spec, bucket_spec]
1090
1091 def setUpBucketMapper(self):
1092 class BucketMapper(ObjectMapper):
1093 def __init__(self, spec):
1094 super().__init__(spec)
1095 self.map_spec('foos', spec.get_data_type('SimpleFoo'))
1096 self.map_spec('foos', spec.get_data_type('NotSimpleFoo'))
1097 self.map_spec('quxs', spec.get_data_type('SimpleQux'))
1098 self.map_spec('quxs', spec.get_data_type('NotSimpleQux'))
1099 self.map_spec('links', spec.links[0])
1100 self.map_spec('links', spec.links[1])
1101
1102 return BucketMapper
1103
1104 def get_two_bucket_test(self):
1105 foos = [SimpleFoo('my_foo1'), NotSimpleFoo('my_foo2')]
1106 quxs = [SimpleQux('my_qux1', data=[1, 2, 3]), NotSimpleQux('my_qux2', data=[4, 5, 6])]
1107 # NOTE: unlike in the other tests, links cannot map to the same foos in bucket because of a name clash
1108 links = [SimpleFoo('my_foo3'), NotSimpleFoo('my_foo4')]
1109 bucket = SimpleBucket(
1110 name='test_bucket',
1111 foos=foos,
1112 quxs=quxs,
1113 links=links
1114 )
1115 foo1_builder = self._create_builder(bucket.foos['my_foo1'])
1116 foo2_builder = self._create_builder(bucket.foos['my_foo2'])
1117 foo3_builder = self._create_builder(bucket.links['my_foo3'])
1118 foo4_builder = self._create_builder(bucket.links['my_foo4'])
1119 qux1_builder = self._create_builder(bucket.quxs['my_qux1'])
1120 qux2_builder = self._create_builder(bucket.quxs['my_qux2'])
1121 foo3_link_builder = LinkBuilder(builder=foo3_builder)
1122 foo4_link_builder = LinkBuilder(builder=foo4_builder)
1123 bucket_builder = GroupBuilder(
1124 name='test_bucket',
1125 groups={'my_foo1': foo1_builder,
1126 'my_foo2': foo2_builder},
1127 datasets={'my_qux1': qux1_builder,
1128 'my_qux2': qux2_builder},
1129 links={'my_foo3': foo3_link_builder,
1130 'my_foo4': foo4_link_builder},
1131 attributes={'namespace': CORE_NAMESPACE,
1132 'data_type': 'SimpleBucket',
1133 'object_id': bucket.object_id}
1134 )
1135 return bucket, bucket_builder
1136
1137 def test_build_two(self):
1138 """Test building a container which contains multiple containers of different types as the spec allows."""
1139 bucket, bucket_builder = self.get_two_bucket_test()
1140 builder = self.manager.build(bucket)
1141 self.assertDictEqual(builder, bucket_builder)