Debian Codegraph @

tests/unit/spec_tests/test_group_spec.py

raw
1 import json
2
3 from hdmf.spec import GroupSpec, DatasetSpec, AttributeSpec, LinkSpec
4 from hdmf.testing import TestCase
5
6
7 class GroupSpecTests(TestCase):
8 def setUp(self):
9 self.attributes = [
10 AttributeSpec('attribute1', 'my first attribute', 'text'),
11 AttributeSpec('attribute2', 'my second attribute', 'text')
12 ]
13
14 self.dset1_attributes = [
15 AttributeSpec('attribute3', 'my third attribute', 'text'),
16 AttributeSpec('attribute4', 'my fourth attribute', 'text')
17 ]
18
19 self.dset2_attributes = [
20 AttributeSpec('attribute5', 'my fifth attribute', 'text'),
21 AttributeSpec('attribute6', 'my sixth attribute', 'text')
22 ]
23
24 self.datasets = [
25 DatasetSpec('my first dataset',
26 'int',
27 name='dataset1',
28 attributes=self.dset1_attributes,
29 linkable=True),
30 DatasetSpec('my second dataset',
31 'int',
32 name='dataset2',
33 attributes=self.dset2_attributes,
34 linkable=True,
35 data_type_def='VoltageArray')
36 ]
37
38 self.subgroups = [
39 GroupSpec('A test subgroup',
40 name='subgroup1',
41 linkable=False),
42 GroupSpec('A test subgroup',
43 name='subgroup2',
44 linkable=False)
45 ]
46
47 def test_constructor(self):
48 spec = GroupSpec('A test group',
49 name='root_constructor',
50 groups=self.subgroups,
51 datasets=self.datasets,
52 attributes=self.attributes,
53 linkable=False)
54 self.assertFalse(spec['linkable'])
55 self.assertListEqual(spec['attributes'], self.attributes)
56 self.assertListEqual(spec['datasets'], self.datasets)
57 self.assertNotIn('data_type_def', spec)
58 self.assertIs(spec, self.subgroups[0].parent)
59 self.assertIs(spec, self.subgroups[1].parent)
60 self.assertIs(spec, self.attributes[0].parent)
61 self.assertIs(spec, self.attributes[1].parent)
62 self.assertIs(spec, self.datasets[0].parent)
63 self.assertIs(spec, self.datasets[1].parent)
64 json.dumps(spec)
65
66 def test_constructor_datatype(self):
67 spec = GroupSpec('A test group',
68 name='root_constructor_datatype',
69 datasets=self.datasets,
70 attributes=self.attributes,
71 linkable=False,
72 data_type_def='EphysData')
73 self.assertFalse(spec['linkable'])
74 self.assertListEqual(spec['attributes'], self.attributes)
75 self.assertListEqual(spec['datasets'], self.datasets)
76 self.assertEqual(spec['data_type_def'], 'EphysData')
77 self.assertIs(spec, self.attributes[0].parent)
78 self.assertIs(spec, self.attributes[1].parent)
79 self.assertIs(spec, self.datasets[0].parent)
80 self.assertIs(spec, self.datasets[1].parent)
81 self.assertEqual(spec.data_type_def, 'EphysData')
82 self.assertIsNone(spec.data_type_inc)
83 json.dumps(spec)
84
85 def test_set_parent_exists(self):
86 GroupSpec('A test group',
87 name='root_constructor',
88 groups=self.subgroups)
89 msg = 'Cannot re-assign parent.'
90 with self.assertRaisesWith(AttributeError, msg):
91 self.subgroups[0].parent = self.subgroups[1]
92
93 def test_set_dataset(self):
94 spec = GroupSpec('A test group',
95 name='root_test_set_dataset',
96 linkable=False,
97 data_type_def='EphysData')
98 spec.set_dataset(self.datasets[0])
99 self.assertIs(spec, self.datasets[0].parent)
100
101 def test_set_link(self):
102 group = GroupSpec(
103 doc='A test group',
104 name='root'
105 )
106 link = LinkSpec(
107 doc='A test link',
108 target_type='LinkTarget',
109 name='link_name'
110 )
111 group.set_link(link)
112 self.assertIs(group, link.parent)
113 self.assertIs(group.get_link('link_name'), link)
114
115 def test_add_link(self):
116 group = GroupSpec(
117 doc='A test group',
118 name='root'
119 )
120 group.add_link(
121 'A test link',
122 'LinkTarget',
123 name='link_name'
124 )
125 self.assertIsInstance(group.get_link('link_name'), LinkSpec)
126
127 def test_set_group(self):
128 spec = GroupSpec('A test group',
129 name='root_test_set_group',
130 linkable=False,
131 data_type_def='EphysData')
132 spec.set_group(self.subgroups[0])
133 spec.set_group(self.subgroups[1])
134 self.assertListEqual(spec['groups'], self.subgroups)
135 self.assertIs(spec, self.subgroups[0].parent)
136 self.assertIs(spec, self.subgroups[1].parent)
137 json.dumps(spec)
138
139 def test_add_group(self):
140 group = GroupSpec(
141 doc='A test group',
142 name='root'
143 )
144 group.add_group(
145 'A test group',
146 name='subgroup'
147 )
148 self.assertIsInstance(group.get_group('subgroup'), GroupSpec)
149
150 def test_type_extension(self):
151 spec = GroupSpec('A test group',
152 name='parent_type',
153 datasets=self.datasets,
154 attributes=self.attributes,
155 linkable=False,
156 data_type_def='EphysData')
157 dset1_attributes_ext = [
158 AttributeSpec('dset1_extra_attribute', 'an extra attribute for the first dataset', 'text')
159 ]
160 ext_datasets = [
161 DatasetSpec('my first dataset extension',
162 'int',
163 name='dataset1',
164 attributes=dset1_attributes_ext,
165 linkable=True),
166 ]
167 ext_attributes = [
168 AttributeSpec('ext_extra_attribute', 'an extra attribute for the group', 'text'),
169 ]
170 ext = GroupSpec('A test group extension',
171 name='child_type',
172 datasets=ext_datasets,
173 attributes=ext_attributes,
174 linkable=False,
175 data_type_inc=spec,
176 data_type_def='SpikeData')
177 ext_dset1 = ext.get_dataset('dataset1')
178 ext_dset1_attrs = ext_dset1.attributes
179 self.assertDictEqual(ext_dset1_attrs[0], dset1_attributes_ext[0])
180 self.assertDictEqual(ext_dset1_attrs[1], self.dset1_attributes[0])
181 self.assertDictEqual(ext_dset1_attrs[2], self.dset1_attributes[1])
182 self.assertEqual(ext.data_type_def, 'SpikeData')
183 self.assertEqual(ext.data_type_inc, 'EphysData')
184
185 ext_dset2 = ext.get_dataset('dataset2')
186 self.maxDiff = None
187 # this will suffice for now, assertDictEqual doesn't do deep equality checks
188 self.assertEqual(str(ext_dset2), str(self.datasets[1]))
189 self.assertAttributesEqual(ext_dset2, self.datasets[1])
190
191 res_attrs = ext.attributes
192 self.assertDictEqual(res_attrs[0], ext_attributes[0])
193 self.assertDictEqual(res_attrs[1], self.attributes[0])
194 self.assertDictEqual(res_attrs[2], self.attributes[1])
195
196 # test that inherited specs are tracked appropriate
197 for d in self.datasets:
198 with self.subTest(dataset=d.name):
199 self.assertTrue(ext.is_inherited_spec(d))
200 self.assertFalse(spec.is_inherited_spec(d))
201
202 json.dumps(spec)
203
204 def assertDatasetsEqual(self, spec1, spec2):
205 spec1_dsets = spec1.datasets
206 spec2_dsets = spec2.datasets
207 if len(spec1_dsets) != len(spec2_dsets):
208 raise AssertionError('different number of AttributeSpecs')
209 else:
210 for i in range(len(spec1_dsets)):
211 self.assertAttributesEqual(spec1_dsets[i], spec2_dsets[i])
212
213 def assertAttributesEqual(self, spec1, spec2):
214 spec1_attr = spec1.attributes
215 spec2_attr = spec2.attributes
216 if len(spec1_attr) != len(spec2_attr):
217 raise AssertionError('different number of AttributeSpecs')
218 else:
219 for i in range(len(spec1_attr)):
220 self.assertDictEqual(spec1_attr[i], spec2_attr[i])
221
222 def test_add_attribute(self):
223 spec = GroupSpec('A test group',
224 name='root_constructor',
225 groups=self.subgroups,
226 datasets=self.datasets,
227 linkable=False)
228 for attrspec in self.attributes:
229 spec.add_attribute(**attrspec)
230 self.assertListEqual(spec['attributes'], self.attributes)
231 self.assertListEqual(spec['datasets'], self.datasets)
232 self.assertNotIn('data_type_def', spec)
233 self.assertIs(spec, self.subgroups[0].parent)
234 self.assertIs(spec, self.subgroups[1].parent)
235 self.assertIs(spec, spec.attributes[0].parent)
236 self.assertIs(spec, spec.attributes[1].parent)
237 self.assertIs(spec, self.datasets[0].parent)
238 self.assertIs(spec, self.datasets[1].parent)
239 json.dumps(spec)
240
241 def test_update_attribute_spec(self):
242 spec = GroupSpec('A test group',
243 name='root_constructor',
244 attributes=[AttributeSpec('attribute1', 'my first attribute', 'text'),
245 AttributeSpec('attribute2', 'my second attribute', 'text')])
246 spec.set_attribute(AttributeSpec('attribute2', 'my second attribute', 'int', value=5))
247 res = spec.get_attribute('attribute2')
248 self.assertEqual(res.value, 5)
249 self.assertEqual(res.dtype, 'int')
250
251 def test_path(self):
252 GroupSpec('A test group',
253 name='root_constructor',
254 groups=self.subgroups,
255 datasets=self.datasets,
256 attributes=self.attributes,
257 linkable=False)
258 self.assertEqual(self.attributes[0].path, 'root_constructor/attribute1')
259 self.assertEqual(self.datasets[0].path, 'root_constructor/dataset1')
260 self.assertEqual(self.subgroups[0].path, 'root_constructor/subgroup1')
261
262 def test_path_complicated(self):
263 attribute = AttributeSpec('attribute1', 'my fifth attribute', 'text')
264 dataset = DatasetSpec('my first dataset',
265 'int',
266 name='dataset1',
267 attributes=[attribute])
268 subgroup = GroupSpec('A subgroup',
269 name='subgroup1',
270 datasets=[dataset])
271 self.assertEqual(attribute.path, 'subgroup1/dataset1/attribute1')
272
273 _ = GroupSpec('A test group',
274 name='root',
275 groups=[subgroup])
276
277 self.assertEqual(attribute.path, 'root/subgroup1/dataset1/attribute1')
278
279 def test_path_no_name(self):
280 attribute = AttributeSpec('attribute1', 'my fifth attribute', 'text')
281 dataset = DatasetSpec('my first dataset',
282 'int',
283 data_type_inc='DatasetType',
284 attributes=[attribute])
285 subgroup = GroupSpec('A subgroup',
286 data_type_def='GroupType',
287 datasets=[dataset])
288 _ = GroupSpec('A test group',
289 name='root',
290 groups=[subgroup])
291
292 self.assertEqual(attribute.path, 'root/GroupType/DatasetType/attribute1')
293
294 def test_data_type_property_value(self):
295 """Test that the property data_type has the expected value"""
296 test_cases = {
297 ('Foo', 'Bar'): 'Bar',
298 ('Foo', None): 'Foo',
299 (None, 'Bar'): 'Bar',
300 (None, None): None,
301 }
302 for (data_type_inc, data_type_def), data_type in test_cases.items():
303 with self.subTest(data_type_inc=data_type_inc,
304 data_type_def=data_type_def, data_type=data_type):
305 dataset = DatasetSpec('A dataset', 'int', name='dataset',
306 data_type_inc=data_type_inc, data_type_def=data_type_def)
307 self.assertEqual(dataset.data_type, data_type)
308
309 def test_get_data_type_spec(self):
310 expected = AttributeSpec('data_type', 'the data type of this object', 'text', value='MyType')
311 self.assertDictEqual(GroupSpec.get_data_type_spec('MyType'), expected)
312
313 def test_get_namespace_spec(self):
314 expected = AttributeSpec('namespace', 'the namespace for the data type of this object', 'text', required=False)
315 self.assertDictEqual(GroupSpec.get_namespace_spec(), expected)
316
317 def test_build_warn_extra_args(self):
318 spec_dict = {
319 'name': 'group1',
320 'doc': 'test group',
321 'required': True,
322 }
323 msg = "Unexpected keys ['required'] in spec {'name': 'group1', 'doc': 'test group', 'required': True}"
324 with self.assertWarnsWith(UserWarning, msg):
325 GroupSpec.build_spec(spec_dict)
326
327
328 class TestNotAllowedConfig(TestCase):
329
330 def test_no_name_no_def_no_inc(self):
331 msg = ("Cannot create Group or Dataset spec with no name without specifying 'data_type_def' "
332 "and/or 'data_type_inc'.")
333 with self.assertRaisesWith(ValueError, msg):
334 GroupSpec('A test group')
335
336 def test_name_with_multiple(self):
337 msg = ("Cannot give specific name to something that can exist multiple times: name='MyGroup', quantity='*'")
338 with self.assertRaisesWith(ValueError, msg):
339 GroupSpec('A test group', name='MyGroup', quantity='*')
340
341
342 class TestResolveAttrs(TestCase):
343
344 def setUp(self):
345 self.def_group_spec = GroupSpec(
346 doc='A test group',
347 name='root',
348 data_type_def='MyGroup',
349 attributes=[AttributeSpec('attribute1', 'my first attribute', 'text'),
350 AttributeSpec('attribute2', 'my second attribute', 'text')]
351 )
352 self.inc_group_spec = GroupSpec(
353 doc='A test group',
354 name='root',
355 data_type_inc='MyGroup',
356 attributes=[AttributeSpec('attribute2', 'my second attribute', 'text', value='fixed'),
357 AttributeSpec('attribute3', 'my third attribute', 'text', value='fixed')]
358 )
359 self.inc_group_spec.resolve_spec(self.def_group_spec)
360
361 def test_resolved(self):
362 self.assertTupleEqual(self.inc_group_spec.attributes, (
363 AttributeSpec('attribute2', 'my second attribute', 'text', value='fixed'),
364 AttributeSpec('attribute3', 'my third attribute', 'text', value='fixed'),
365 AttributeSpec('attribute1', 'my first attribute', 'text')
366 ))
367
368 self.assertEqual(self.inc_group_spec.get_attribute('attribute1'),
369 AttributeSpec('attribute1', 'my first attribute', 'text'))
370 self.assertEqual(self.inc_group_spec.get_attribute('attribute2'),
371 AttributeSpec('attribute2', 'my second attribute', 'text', value='fixed'))
372 self.assertEqual(self.inc_group_spec.get_attribute('attribute3'),
373 AttributeSpec('attribute3', 'my third attribute', 'text', value='fixed'))
374
375 self.assertTrue(self.inc_group_spec.resolved)
376
377 def test_is_inherited_spec(self):
378 self.assertFalse(self.def_group_spec.is_inherited_spec(self.def_group_spec.attributes[0]))
379 self.assertFalse(self.def_group_spec.is_inherited_spec(self.def_group_spec.attributes[1]))
380
381 attr_spec_map = {attr.name: attr for attr in self.inc_group_spec.attributes}
382 self.assertTrue(self.inc_group_spec.is_inherited_spec(attr_spec_map["attribute1"]))
383 self.assertTrue(self.inc_group_spec.is_inherited_spec(attr_spec_map["attribute2"]))
384 self.assertFalse(self.inc_group_spec.is_inherited_spec(attr_spec_map["attribute3"]))
385
386 def test_is_overridden_spec(self):
387 self.assertFalse(self.def_group_spec.is_overridden_spec(self.def_group_spec.attributes[0]))
388 self.assertFalse(self.def_group_spec.is_overridden_spec(self.def_group_spec.attributes[0]))
389
390 attr_spec_map = {attr.name: attr for attr in self.inc_group_spec.attributes}
391 self.assertFalse(self.inc_group_spec.is_overridden_spec(attr_spec_map["attribute1"]))
392 self.assertTrue(self.inc_group_spec.is_overridden_spec(attr_spec_map["attribute2"]))
393 self.assertFalse(self.inc_group_spec.is_overridden_spec(attr_spec_map["attribute3"]))
394
395 def test_is_inherited_attribute(self):
396 self.assertFalse(self.def_group_spec.is_inherited_attribute('attribute1'))
397 self.assertFalse(self.def_group_spec.is_inherited_attribute('attribute2'))
398 self.assertTrue(self.inc_group_spec.is_inherited_attribute('attribute1'))
399 self.assertTrue(self.inc_group_spec.is_inherited_attribute('attribute2'))
400 self.assertFalse(self.inc_group_spec.is_inherited_attribute('attribute3'))
401 with self.assertRaisesWith(ValueError, "Attribute 'attribute4' not found"):
402 self.inc_group_spec.is_inherited_attribute('attribute4')
403
404 def test_is_overridden_attribute(self):
405 self.assertFalse(self.def_group_spec.is_overridden_attribute('attribute1'))
406 self.assertFalse(self.def_group_spec.is_overridden_attribute('attribute2'))
407 self.assertFalse(self.inc_group_spec.is_overridden_attribute('attribute1'))
408 self.assertTrue(self.inc_group_spec.is_overridden_attribute('attribute2'))
409 self.assertFalse(self.inc_group_spec.is_overridden_attribute('attribute3'))
410 with self.assertRaisesWith(ValueError, "Attribute 'attribute4' not found"):
411 self.inc_group_spec.is_overridden_attribute('attribute4')
412
413
414 class TestResolveGroupSameAttributeName(TestCase):
415 # https://github.com/hdmf-dev/hdmf/issues/1121
416
417 def test_is_inherited_two_different_datasets(self):
418 self.def_group_spec = GroupSpec(
419 doc='A test group',
420 data_type_def='MyGroup',
421 datasets=[
422 DatasetSpec(
423 name='dset1',
424 doc="dset1",
425 dtype='int',
426 attributes=[AttributeSpec('attr1', 'MyGroup.dset1.attr1', 'text')]
427 ),
428 ]
429 )
430 self.inc_group_spec = GroupSpec(
431 doc='A test subgroup',
432 data_type_def='SubGroup',
433 data_type_inc='MyGroup',
434 datasets=[
435 DatasetSpec(
436 name='dset2',
437 doc="dset2",
438 dtype='int',
439 attributes=[AttributeSpec('attr1', 'SubGroup.dset2.attr1', 'text')]
440 ),
441 ]
442 )
443 self.inc_group_spec.resolve_spec(self.def_group_spec)
444
445 self.assertFalse(self.def_group_spec.is_inherited_spec(self.def_group_spec.datasets[0].attributes[0]))
446
447 dset_spec_map = {dset.name: dset for dset in self.inc_group_spec.datasets}
448 self.assertFalse(self.inc_group_spec.is_inherited_spec(dset_spec_map["dset2"].attributes[0]))
449 self.assertTrue(self.inc_group_spec.is_inherited_spec(dset_spec_map["dset1"].attributes[0]))
450
451 def test_is_inherited_different_groups_and_datasets(self):
452 self.def_group_spec = GroupSpec(
453 doc='A test group',
454 data_type_def='MyGroup',
455 attributes=[AttributeSpec('attr1', 'MyGroup.attr1', 'text')], # <-- added from above
456 datasets=[
457 DatasetSpec(
458 name='dset1',
459 doc="dset1",
460 dtype='int',
461 attributes=[AttributeSpec('attr1', 'MyGroup.dset1.attr1', 'text')]
462 ),
463 ]
464 )
465 self.inc_group_spec = GroupSpec(
466 doc='A test subgroup',
467 data_type_def='SubGroup',
468 data_type_inc='MyGroup',
469 attributes=[AttributeSpec('attr1', 'SubGroup.attr1', 'text')], # <-- added from above
470 datasets=[
471 DatasetSpec(
472 name='dset2',
473 doc="dset2",
474 dtype='int',
475 attributes=[AttributeSpec('attr1', 'SubGroup.dset2.attr1', 'text')]
476 ),
477 ]
478 )
479 self.inc_group_spec.resolve_spec(self.def_group_spec)
480
481 self.assertFalse(self.def_group_spec.is_inherited_spec(self.def_group_spec.datasets[0].attributes[0]))
482
483 dset_spec_map = {dset.name: dset for dset in self.inc_group_spec.datasets}
484 self.assertFalse(self.inc_group_spec.is_inherited_spec(dset_spec_map["dset2"].attributes[0]))
485 self.assertTrue(self.inc_group_spec.is_inherited_spec(dset_spec_map["dset1"].attributes[0]))
486 self.assertTrue(self.inc_group_spec.is_inherited_spec(self.inc_group_spec.attributes[0]))
487
488 self.inc_group_spec2 = GroupSpec(
489 doc='A test subsubgroup',
490 data_type_def='SubSubGroup',
491 data_type_inc='SubGroup',
492 )
493 self.inc_group_spec2.resolve_spec(self.inc_group_spec)
494
495 dset_spec_map = {dset.name: dset for dset in self.inc_group_spec2.datasets}
496 self.assertTrue(self.inc_group_spec2.is_inherited_spec(dset_spec_map["dset1"].attributes[0]))
497 self.assertTrue(self.inc_group_spec2.is_inherited_spec(dset_spec_map["dset2"].attributes[0]))
498 self.assertTrue(self.inc_group_spec2.is_inherited_spec(self.inc_group_spec2.attributes[0]))
499
500
501
502
503 class GroupSpecWithLinksTest(TestCase):
504
505 def test_constructor(self):
506 link0 = LinkSpec(doc='Link 0', target_type='TargetType0')
507 link1 = LinkSpec(doc='Link 1', target_type='TargetType1')
508 links = [link0, link1]
509 spec = GroupSpec(
510 doc='A test group',
511 name='root',
512 links=links
513 )
514 self.assertIs(spec, links[0].parent)
515 self.assertIs(spec, links[1].parent)
516 json.dumps(spec)
517
518 def test_extension_no_overwrite(self):
519 link0 = LinkSpec(doc='Link 0', target_type='TargetType0') # test unnamed
520 link1 = LinkSpec(doc='Link 1', target_type='TargetType1', name='MyType1') # test named
521 link2 = LinkSpec(doc='Link 2', target_type='TargetType2', quantity='*') # test named, multiple
522 links = [link0, link1, link2]
523 parent_spec = GroupSpec(
524 doc='A test group',
525 name='parent',
526 links=links,
527 data_type_def='ParentType'
528 )
529 child_spec = GroupSpec(
530 doc='A test group',
531 name='child',
532 data_type_inc=parent_spec,
533 data_type_def='ChildType'
534 )
535
536 for link in links:
537 with self.subTest(link_target_type=link.target_type):
538 self.assertTrue(child_spec.is_inherited_spec(link))
539 self.assertFalse(child_spec.is_overridden_spec(link))
540
541 def test_extension_overwrite(self):
542 link0 = LinkSpec(doc='Link 0', target_type='TargetType0', name='MyType0')
543 link1 = LinkSpec(doc='Link 1', target_type='TargetType1', name='MyType1')
544 # NOTE overwriting unnamed LinkSpec is not allowed
545 # NOTE overwriting spec with quantity that could be >1 is not allowed
546 links = [link0, link1]
547 parent_spec = GroupSpec(
548 doc='A test group',
549 name='parent',
550 links=links,
551 data_type_def='ParentType'
552 )
553
554 link0_overwrite = LinkSpec(doc='New link 0', target_type='TargetType0', name='MyType0')
555 link1_overwrite = LinkSpec(doc='New link 1', target_type='TargetType1Child', name='MyType1')
556 overwritten_links = [link0_overwrite, link1_overwrite]
557 child_spec = GroupSpec(
558 doc='A test group',
559 name='child',
560 links=overwritten_links,
561 data_type_inc=parent_spec,
562 data_type_def='ChildType'
563 )
564
565 for link in overwritten_links:
566 with self.subTest(link_target_type=link.target_type):
567 self.assertTrue(child_spec.is_inherited_spec(link))
568 self.assertTrue(child_spec.is_overridden_spec(link))
569
570
571 class SpecWithDupsTest(TestCase):
572
573 def test_two_unnamed_group_same_type(self):
574 """Test creating a group contains multiple unnamed groups with type X."""
575 child0 = GroupSpec(doc='Group 0', data_type_inc='Type0')
576 child1 = GroupSpec(doc='Group 1', data_type_inc='Type0')
577 msg = "Cannot have multiple groups/datasets with the same data type without specifying name"
578 with self.assertRaisesWith(ValueError, msg):
579 GroupSpec(
580 doc='A test group',
581 name='parent',
582 groups=[child0, child1],
583 data_type_def='ParentType'
584 )
585
586 def test_named_unnamed_group_with_def_same_type(self):
587 """Test get_data_type when a group contains both a named and unnamed group with type X."""
588 child0 = GroupSpec(doc='Group 0', data_type_def='Type0', name='type0')
589 child1 = GroupSpec(doc='Group 1', data_type_inc='Type0')
590 parent_spec = GroupSpec(
591 doc='A test group',
592 name='parent',
593 groups=[child0, child1],
594 data_type_def='ParentType'
595 )
596 self.assertIs(parent_spec.get_data_type('Type0'), child1)
597
598 def test_named_unnamed_group_same_type(self):
599 """Test get_data_type when a group contains both a named and unnamed group with type X."""
600 child0 = GroupSpec(doc='Group 0', data_type_inc='Type0', name='type0')
601 child1 = GroupSpec(doc='Group 1', data_type_inc='Type0', name='type1')
602 child2 = GroupSpec(doc='Group 2', data_type_inc='Type0')
603 parent_spec = GroupSpec(
604 doc='A test group',
605 name='parent',
606 groups=[child0, child1, child2],
607 data_type_def='ParentType'
608 )
609 self.assertIs(parent_spec.get_data_type('Type0'), child2)
610
611 def test_unnamed_named_group_same_type(self):
612 """Test get_data_type when a group contains both an unnamed and named group with type X."""
613 child0 = GroupSpec(doc='Group 0', data_type_inc='Type0')
614 child1 = GroupSpec(doc='Group 1', data_type_inc='Type0', name='type1')
615 parent_spec = GroupSpec(
616 doc='A test group',
617 name='parent',
618 groups=[child0, child1],
619 data_type_def='ParentType'
620 )
621 self.assertIs(parent_spec.get_data_type('Type0'), child0)
622
623 def test_two_named_group_same_type(self):
624 """Test get_data_type when a group contains multiple named groups with type X."""
625 child0 = GroupSpec(doc='Group 0', data_type_inc='Type0', name='group0')
626 child1 = GroupSpec(doc='Group 1', data_type_inc='Type0', name='group1')
627 parent_spec = GroupSpec(
628 doc='A test group',
629 name='parent',
630 groups=[child0, child1],
631 data_type_def='ParentType'
632 )
633 self.assertEqual(parent_spec.get_data_type('Type0'), [child0, child1])
634
635 def test_two_unnamed_datasets_same_type(self):
636 """Test creating a group contains multiple unnamed datasets with type X."""
637 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0')
638 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0')
639 msg = "Cannot have multiple groups/datasets with the same data type without specifying name"
640 with self.assertRaisesWith(ValueError, msg):
641 GroupSpec(
642 doc='A test group',
643 name='parent',
644 datasets=[child0, child1],
645 data_type_def='ParentType'
646 )
647
648 def test_named_unnamed_dataset_with_def_same_type(self):
649 """Test get_data_type when a group contains both a named and unnamed dataset with type X."""
650 child0 = DatasetSpec(doc='Group 0', data_type_def='Type0', name='type0')
651 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0')
652 parent_spec = GroupSpec(
653 doc='A test group',
654 name='parent',
655 datasets=[child0, child1],
656 data_type_def='ParentType'
657 )
658 self.assertIs(parent_spec.get_data_type('Type0'), child1)
659
660 def test_named_unnamed_dataset_same_type(self):
661 """Test get_data_type when a group contains both a named and unnamed dataset with type X."""
662 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0', name='type0')
663 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0')
664 parent_spec = GroupSpec(
665 doc='A test group',
666 name='parent',
667 datasets=[child0, child1],
668 data_type_def='ParentType'
669 )
670 self.assertIs(parent_spec.get_data_type('Type0'), child1)
671
672 def test_two_named_unnamed_dataset_same_type(self):
673 """Test get_data_type when a group contains two named and one unnamed dataset with type X."""
674 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0', name='type0')
675 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0', name='type1')
676 child2 = DatasetSpec(doc='Group 2', data_type_inc='Type0')
677 parent_spec = GroupSpec(
678 doc='A test group',
679 name='parent',
680 datasets=[child0, child1, child2],
681 data_type_def='ParentType'
682 )
683 self.assertIs(parent_spec.get_data_type('Type0'), child2)
684
685 def test_unnamed_named_dataset_same_type(self):
686 """Test get_data_type when a group contains both an unnamed and named dataset with type X."""
687 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0')
688 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0', name='type1')
689 parent_spec = GroupSpec(
690 doc='A test group',
691 name='parent',
692 datasets=[child0, child1],
693 data_type_def='ParentType'
694 )
695 self.assertIs(parent_spec.get_data_type('Type0'), child0)
696
697 def test_two_named_datasets_same_type(self):
698 """Test get_data_type when a group contains multiple named datasets with type X."""
699 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0', name='group0')
700 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0', name='group1')
701 parent_spec = GroupSpec(
702 doc='A test group',
703 name='parent',
704 datasets=[child0, child1],
705 data_type_def='ParentType'
706 )
707 self.assertEqual(parent_spec.get_data_type('Type0'), [child0, child1])
708
709 def test_three_named_datasets_same_type(self):
710 """Test get_target_type when a group contains three named links with type X."""
711 child0 = DatasetSpec(doc='Group 0', data_type_inc='Type0', name='group0')
712 child1 = DatasetSpec(doc='Group 1', data_type_inc='Type0', name='group1')
713 child2 = DatasetSpec(doc='Group 2', data_type_inc='Type0', name='group2')
714 parent_spec = GroupSpec(
715 doc='A test group',
716 name='parent',
717 datasets=[child0, child1, child2],
718 data_type_def='ParentType'
719 )
720 self.assertEqual(parent_spec.get_data_type('Type0'), [child0, child1, child2])
721
722 def test_two_unnamed_links_same_type(self):
723 """Test creating a group contains multiple unnamed links with type X."""
724 child0 = LinkSpec(doc='Group 0', target_type='Type0')
725 child1 = LinkSpec(doc='Group 1', target_type='Type0')
726 msg = "Cannot have multiple links with the same target type without specifying name"
727 with self.assertRaisesWith(ValueError, msg):
728 GroupSpec(
729 doc='A test group',
730 name='parent',
731 links=[child0, child1],
732 data_type_def='ParentType'
733 )
734
735 def test_named_unnamed_link_same_type(self):
736 """Test get_target_type when a group contains both a named and unnamed link with type X."""
737 child0 = LinkSpec(doc='Group 0', target_type='Type0', name='type0')
738 child1 = LinkSpec(doc='Group 1', target_type='Type0')
739 parent_spec = GroupSpec(
740 doc='A test group',
741 name='parent',
742 links=[child0, child1],
743 data_type_def='ParentType'
744 )
745 self.assertIs(parent_spec.get_target_type('Type0'), child1)
746
747 def test_two_named_unnamed_link_same_type(self):
748 """Test get_target_type when a group contains two named and one unnamed link with type X."""
749 child0 = LinkSpec(doc='Group 0', target_type='Type0', name='type0')
750 child1 = LinkSpec(doc='Group 1', target_type='Type0', name='type1')
751 child2 = LinkSpec(doc='Group 2', target_type='Type0')
752 parent_spec = GroupSpec(
753 doc='A test group',
754 name='parent',
755 links=[child0, child1, child2],
756 data_type_def='ParentType'
757 )
758 self.assertIs(parent_spec.get_target_type('Type0'), child2)
759
760 def test_unnamed_named_link_same_type(self):
761 """Test get_target_type when a group contains both an unnamed and named link with type X."""
762 child0 = LinkSpec(doc='Group 0', target_type='Type0')
763 child1 = LinkSpec(doc='Group 1', target_type='Type0', name='type1')
764 parent_spec = GroupSpec(
765 doc='A test group',
766 name='parent',
767 links=[child0, child1],
768 data_type_def='ParentType'
769 )
770 self.assertIs(parent_spec.get_target_type('Type0'), child0)
771
772 def test_two_named_links_same_type(self):
773 """Test get_target_type when a group contains multiple named links with type X."""
774 child0 = LinkSpec(doc='Group 0', target_type='Type0', name='group0')
775 child1 = LinkSpec(doc='Group 1', target_type='Type0', name='group1')
776 parent_spec = GroupSpec(
777 doc='A test group',
778 name='parent',
779 links=[child0, child1],
780 data_type_def='ParentType'
781 )
782 self.assertEqual(parent_spec.get_target_type('Type0'), [child0, child1])
783
784 def test_three_named_links_same_type(self):
785 """Test get_target_type when a group contains three named links with type X."""
786 child0 = LinkSpec(doc='Group 0', target_type='Type0', name='type0')
787 child1 = LinkSpec(doc='Group 1', target_type='Type0', name='type1')
788 child2 = LinkSpec(doc='Group 2', target_type='Type0', name='type2')
789 parent_spec = GroupSpec(
790 doc='A test group',
791 name='parent',
792 links=[child0, child1, child2],
793 data_type_def='ParentType'
794 )
795 self.assertEqual(parent_spec.get_target_type('Type0'), [child0, child1, child2])
796
797
798 class SpecWithGroupsLinksTest(TestCase):
799
800 def test_unnamed_group_link_same_type(self):
801 child = GroupSpec(doc='Group 0', data_type_inc='Type0')
802 link = LinkSpec(doc='Link 0', target_type='Type0')
803 parent_spec = GroupSpec(
804 doc='A test group',
805 name='parent',
806 groups=[child],
807 links=[link],
808 data_type_def='ParentType'
809 )
810 self.assertIs(parent_spec.get_data_type('Type0'), child)
811 self.assertIs(parent_spec.get_target_type('Type0'), link)
812
813 def test_unnamed_dataset_link_same_type(self):
814 child = DatasetSpec(doc='Dataset 0', data_type_inc='Type0')
815 link = LinkSpec(doc='Link 0', target_type='Type0')
816 parent_spec = GroupSpec(
817 doc='A test group',
818 name='parent',
819 datasets=[child],
820 links=[link],
821 data_type_def='ParentType'
822 )
823 self.assertIs(parent_spec.get_data_type('Type0'), child)
824 self.assertIs(parent_spec.get_target_type('Type0'), link)