Debian Codegraph @

tests/unit/spec_tests/test_attribute_spec.py

raw
1 import json
2
3 from hdmf.spec import AttributeSpec, RefSpec
4 from hdmf.testing import TestCase
5
6
7 class AttributeSpecTests(TestCase):
8
9 def test_constructor(self):
10 spec = AttributeSpec('attribute1',
11 'my first attribute',
12 'text')
13 self.assertEqual(spec['name'], 'attribute1')
14 self.assertEqual(spec['dtype'], 'text')
15 self.assertEqual(spec['doc'], 'my first attribute')
16 self.assertIsNone(spec.parent)
17 json.dumps(spec) # to ensure there are no circular links
18
19 def test_invalid_dtype(self):
20 with self.assertRaises(ValueError):
21 AttributeSpec(name='attribute1',
22 doc='my first attribute',
23 dtype='invalid' # <-- Invalid dtype must raise a ValueError
24 )
25
26 def test_both_value_and_default_value_set(self):
27 with self.assertRaises(ValueError):
28 AttributeSpec(name='attribute1',
29 doc='my first attribute',
30 dtype='int',
31 value=5,
32 default_value=10 # <-- Default_value and value can't be set at the same time
33 )
34
35 def test_colliding_shape_and_dims(self):
36 with self.assertRaises(ValueError):
37 AttributeSpec(name='attribute1',
38 doc='my first attribute',
39 dtype='int',
40 dims=['test'],
41 shape=[None, 2] # <-- Length of shape and dims do not match must raise a ValueError
42 )
43
44 def test_default_value(self):
45 spec = AttributeSpec('attribute1',
46 'my first attribute',
47 'text',
48 default_value='some text')
49 self.assertEqual(spec['default_value'], 'some text')
50 self.assertEqual(spec.default_value, 'some text')
51
52 def test_shape(self):
53 shape = [None, 2]
54 spec = AttributeSpec('attribute1',
55 'my first attribute',
56 'text',
57 shape=shape)
58 self.assertEqual(spec['shape'], shape)
59 self.assertEqual(spec.shape, shape)
60
61 def test_dims_without_shape(self):
62 spec = AttributeSpec('attribute1',
63 'my first attribute',
64 'text',
65 dims=['test'])
66 self.assertEqual(spec.shape, (None, ))
67
68 def test_build_spec(self):
69 spec_dict = {'name': 'attribute1',
70 'doc': 'my first attribute',
71 'dtype': 'text',
72 'shape': [None],
73 'dims': ['dim1'],
74 'value': ['a', 'b']}
75 ret = AttributeSpec.build_spec(spec_dict)
76 self.assertTrue(isinstance(ret, AttributeSpec))
77 self.assertDictEqual(ret, spec_dict)
78
79 def test_build_spec_reftype(self):
80 spec_dict = {'name': 'attribute1',
81 'doc': 'my first attribute',
82 'dtype': {'target_type': 'AnotherType', 'reftype': 'object'}}
83 expected = spec_dict.copy()
84 expected['dtype'] = RefSpec(target_type='AnotherType', reftype='object')
85 ret = AttributeSpec.build_spec(spec_dict)
86 self.assertTrue(isinstance(ret, AttributeSpec))
87 self.assertDictEqual(ret, expected)
88
89 def test_build_spec_no_doc(self):
90 spec_dict = {'name': 'attribute1', 'dtype': 'text'}
91 msg = "AttributeSpec.__init__: missing argument 'doc'"
92 with self.assertRaisesWith(TypeError, msg):
93 AttributeSpec.build_spec(spec_dict)
94
95 def test_build_warn_extra_args(self):
96 spec_dict = {
97 'name': 'attribute1',
98 'doc': 'test attribute',
99 'dtype': 'int',
100 'quantity': '?',
101 }
102 msg = ("Unexpected keys ['quantity'] in spec {'name': 'attribute1', 'doc': 'test attribute', "
103 "'dtype': 'int', 'quantity': '?'}")
104 with self.assertWarnsWith(UserWarning, msg):
105 AttributeSpec.build_spec(spec_dict)