File: test_attribute_spec.py

package info (click to toggle)
hdmf 3.14.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,372 kB
  • sloc: python: 34,738; makefile: 303; sh: 35
file content (105 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json

from hdmf.spec import AttributeSpec, RefSpec
from hdmf.testing import TestCase


class AttributeSpecTests(TestCase):

    def test_constructor(self):
        spec = AttributeSpec('attribute1',
                             'my first attribute',
                             'text')
        self.assertEqual(spec['name'], 'attribute1')
        self.assertEqual(spec['dtype'], 'text')
        self.assertEqual(spec['doc'], 'my first attribute')
        self.assertIsNone(spec.parent)
        json.dumps(spec)  # to ensure there are no circular links

    def test_invalid_dtype(self):
        with self.assertRaises(ValueError):
            AttributeSpec(name='attribute1',
                          doc='my first attribute',
                          dtype='invalid'              # <-- Invalid dtype must raise a ValueError
                          )

    def test_both_value_and_default_value_set(self):
        with self.assertRaises(ValueError):
            AttributeSpec(name='attribute1',
                          doc='my first attribute',
                          dtype='int',
                          value=5,
                          default_value=10            # <-- Default_value and value can't be set at the same time
                          )

    def test_colliding_shape_and_dims(self):
        with self.assertRaises(ValueError):
            AttributeSpec(name='attribute1',
                          doc='my first attribute',
                          dtype='int',
                          dims=['test'],
                          shape=[None, 2]           # <-- Length of shape and dims do not match must raise a ValueError
                          )

    def test_default_value(self):
        spec = AttributeSpec('attribute1',
                             'my first attribute',
                             'text',
                             default_value='some text')
        self.assertEqual(spec['default_value'], 'some text')
        self.assertEqual(spec.default_value, 'some text')

    def test_shape(self):
        shape = [None, 2]
        spec = AttributeSpec('attribute1',
                             'my first attribute',
                             'text',
                             shape=shape)
        self.assertEqual(spec['shape'], shape)
        self.assertEqual(spec.shape, shape)

    def test_dims_without_shape(self):
        spec = AttributeSpec('attribute1',
                             'my first attribute',
                             'text',
                             dims=['test'])
        self.assertEqual(spec.shape, (None, ))

    def test_build_spec(self):
        spec_dict = {'name': 'attribute1',
                     'doc': 'my first attribute',
                     'dtype': 'text',
                     'shape': [None],
                     'dims': ['dim1'],
                     'value': ['a', 'b']}
        ret = AttributeSpec.build_spec(spec_dict)
        self.assertTrue(isinstance(ret, AttributeSpec))
        self.assertDictEqual(ret, spec_dict)

    def test_build_spec_reftype(self):
        spec_dict = {'name': 'attribute1',
                     'doc': 'my first attribute',
                     'dtype': {'target_type': 'AnotherType', 'reftype': 'object'}}
        expected = spec_dict.copy()
        expected['dtype'] = RefSpec(target_type='AnotherType', reftype='object')
        ret = AttributeSpec.build_spec(spec_dict)
        self.assertTrue(isinstance(ret, AttributeSpec))
        self.assertDictEqual(ret, expected)

    def test_build_spec_no_doc(self):
        spec_dict = {'name': 'attribute1', 'dtype': 'text'}
        msg = "AttributeSpec.__init__: missing argument 'doc'"
        with self.assertRaisesWith(TypeError, msg):
            AttributeSpec.build_spec(spec_dict)

    def test_build_warn_extra_args(self):
        spec_dict = {
            'name': 'attribute1',
            'doc': 'test attribute',
            'dtype': 'int',
            'quantity': '?',
        }
        msg = ("Unexpected keys ['quantity'] in spec {'name': 'attribute1', 'doc': 'test attribute', "
               "'dtype': 'int', 'quantity': '?'}")
        with self.assertWarnsWith(UserWarning, msg):
            AttributeSpec.build_spec(spec_dict)