| 1 |
import json |
| 2 |
|
| 3 |
from hdmf.spec import GroupSpec, LinkSpec |
| 4 |
from hdmf.testing import TestCase |
| 5 |
|
| 6 |
|
| 7 |
class LinkSpecTests(TestCase): |
| 8 |
|
| 9 |
def test_constructor(self): |
| 10 |
spec = LinkSpec( |
| 11 |
doc='A test link', |
| 12 |
target_type='Group1', |
| 13 |
quantity='+', |
| 14 |
name='Link1', |
| 15 |
) |
| 16 |
self.assertEqual(spec.doc, 'A test link') |
| 17 |
self.assertEqual(spec.target_type, 'Group1') |
| 18 |
self.assertEqual(spec.data_type_inc, 'Group1') |
| 19 |
self.assertEqual(spec.quantity, '+') |
| 20 |
self.assertEqual(spec.name, 'Link1') |
| 21 |
json.dumps(spec) |
| 22 |
|
| 23 |
def test_constructor_target_spec_def(self): |
| 24 |
group_spec_def = GroupSpec( |
| 25 |
data_type_def='Group1', |
| 26 |
doc='A test group', |
| 27 |
) |
| 28 |
spec = LinkSpec( |
| 29 |
doc='A test link', |
| 30 |
target_type=group_spec_def, |
| 31 |
) |
| 32 |
self.assertEqual(spec.target_type, 'Group1') |
| 33 |
json.dumps(spec) |
| 34 |
|
| 35 |
def test_constructor_target_spec_inc(self): |
| 36 |
group_spec_inc = GroupSpec( |
| 37 |
data_type_inc='Group1', |
| 38 |
doc='A test group', |
| 39 |
) |
| 40 |
msg = "'target_type' must be a string or a GroupSpec or DatasetSpec with a 'data_type_def' key." |
| 41 |
with self.assertRaisesWith(ValueError, msg): |
| 42 |
LinkSpec( |
| 43 |
doc='A test link', |
| 44 |
target_type=group_spec_inc, |
| 45 |
) |
| 46 |
|
| 47 |
def test_constructor_defaults(self): |
| 48 |
spec = LinkSpec( |
| 49 |
doc='A test link', |
| 50 |
target_type='Group1', |
| 51 |
) |
| 52 |
self.assertEqual(spec.quantity, 1) |
| 53 |
self.assertIsNone(spec.name) |
| 54 |
json.dumps(spec) |
| 55 |
|
| 56 |
def test_required_is_many(self): |
| 57 |
quantity_opts = ['?', 1, '*', '+'] |
| 58 |
is_required = [False, True, False, True] |
| 59 |
is_many = [False, False, True, True] |
| 60 |
for (quantity, req, many) in zip(quantity_opts, is_required, is_many): |
| 61 |
with self.subTest(quantity=quantity): |
| 62 |
spec = LinkSpec( |
| 63 |
doc='A test link', |
| 64 |
target_type='Group1', |
| 65 |
quantity=quantity, |
| 66 |
name='Link1', |
| 67 |
) |
| 68 |
self.assertEqual(spec.required, req) |
| 69 |
self.assertEqual(spec.is_many(), many) |
| 70 |
|
| 71 |
def test_build_warn_extra_args(self): |
| 72 |
spec_dict = { |
| 73 |
'name': 'link1', |
| 74 |
'doc': 'test link', |
| 75 |
'target_type': 'TestType', |
| 76 |
'required': True, |
| 77 |
} |
| 78 |
msg = ("Unexpected keys ['required'] in spec {'name': 'link1', 'doc': 'test link', " |
| 79 |
"'target_type': 'TestType', 'required': True}") |
| 80 |
with self.assertWarnsWith(UserWarning, msg): |
| 81 |
LinkSpec.build_spec(spec_dict) |