Esempio n. 1
0
    def test_update_docval_default_value_none(self):
        """Test that update_docval_args for an optional field sets default: None."""
        spec = GroupSpec(doc='A test group specification with a data type',
                         data_type_def='Baz',
                         attributes=[
                             AttributeSpec(name='attr1',
                                           doc='a string attribute',
                                           dtype='text',
                                           required=False)
                         ])
        not_inherited_fields = {'attr1': spec.get_attribute('attr1')}

        docval_args = list()
        CustomClassGenerator.process_field_spec(
            classdict={},
            docval_args=docval_args,
            parent_cls=EmptyBar,  # <-- arbitrary class
            attr_name='attr1',
            not_inherited_fields=not_inherited_fields,
            type_map=TypeMap(),
            spec=spec)

        expected = [{
            'name': 'attr1',
            'type': str,
            'doc': 'a string attribute',
            'default': None
        }]
        self.assertListEqual(docval_args, expected)
Esempio n. 2
0
    def test_update_docval_attr_shape(self):
        """Test that update_docval_args for an attribute with shape sets the type and shape keys."""
        spec = GroupSpec(doc='A test group specification with a data type',
                         data_type_def='Baz',
                         attributes=[
                             AttributeSpec(name='attr1',
                                           doc='a string attribute',
                                           dtype='text',
                                           shape=[None])
                         ])
        not_inherited_fields = {'attr1': spec.get_attribute('attr1')}

        docval_args = list()
        CustomClassGenerator.process_field_spec(
            classdict={},
            docval_args=docval_args,
            parent_cls=EmptyBar,  # <-- arbitrary class
            attr_name='attr1',
            not_inherited_fields=not_inherited_fields,
            type_map=TypeMap(),
            spec=spec)

        expected = [{
            'name': 'attr1',
            'type': ('array_data', 'data'),
            'doc': 'a string attribute',
            'shape': [None]
        }]
        self.assertListEqual(docval_args, expected)
Esempio n. 3
0
 def test_update_attribute_spec(self):
     spec = GroupSpec('A test group',
                      name='root_constructor',
                      attributes=[AttributeSpec('attribute1', 'my first attribute', 'text'), ])
     spec.set_attribute(AttributeSpec('attribute1', 'my first attribute', 'int', value=5))
     res = spec.get_attribute('attribute1')
     self.assertEqual(res.value, 5)
     self.assertEqual(res.dtype, 'int')
Esempio n. 4
0
    def test_process_field_spec_overwrite(self):
        """Test that docval generation overwrites previous docval args."""
        spec = GroupSpec(doc='A test group specification with a data type',
                         data_type_def='Baz',
                         attributes=[
                             AttributeSpec(name='attr1',
                                           doc='a string attribute',
                                           dtype='text',
                                           shape=[None])
                         ])
        not_inherited_fields = {'attr1': spec.get_attribute('attr1')}

        docval_args = [
            {
                'name': 'attr1',
                'type': ('array_data', 'data'),
                'doc': 'a string attribute',
                'shape': [[None], [None, None]]
            },  # this dict will be overwritten below
            {
                'name': 'attr2',
                'type': ('array_data', 'data'),
                'doc': 'a string attribute',
                'shape': [[None], [None, None]]
            }
        ]
        CustomClassGenerator.process_field_spec(
            classdict={},
            docval_args=docval_args,
            parent_cls=EmptyBar,  # <-- arbitrary class
            attr_name='attr1',
            not_inherited_fields=not_inherited_fields,
            type_map=TypeMap(),
            spec=spec)

        expected = [{
            'name': 'attr1',
            'type': ('array_data', 'data'),
            'doc': 'a string attribute',
            'shape': [None]
        }, {
            'name': 'attr2',
            'type': ('array_data', 'data'),
            'doc': 'a string attribute',
            'shape': [[None], [None, None]]
        }]
        self.assertListEqual(docval_args, expected)
Esempio n. 5
0
class TestResolveAttrs(TestCase):
    def setUp(self):
        self.def_group_spec = GroupSpec(
            doc='A test group',
            name='root',
            data_type_def='MyGroup',
            attributes=[
                AttributeSpec('attribute1', 'my first attribute', 'text'),
                AttributeSpec('attribute2', 'my second attribute', 'text')
            ])
        self.inc_group_spec = GroupSpec(doc='A test group',
                                        name='root',
                                        data_type_inc='MyGroup',
                                        attributes=[
                                            AttributeSpec(
                                                'attribute2',
                                                'my second attribute',
                                                'text',
                                                value='fixed'),
                                            AttributeSpec('attribute3',
                                                          'my third attribute',
                                                          'text',
                                                          value='fixed')
                                        ])
        self.inc_group_spec.resolve_spec(self.def_group_spec)

    def test_resolved(self):
        self.assertTupleEqual(
            self.inc_group_spec.attributes,
            (AttributeSpec(
                'attribute2', 'my second attribute', 'text', value='fixed'),
             AttributeSpec(
                 'attribute3', 'my third attribute', 'text', value='fixed'),
             AttributeSpec('attribute1', 'my first attribute', 'text')))

        self.assertEqual(
            self.inc_group_spec.get_attribute('attribute1'),
            AttributeSpec('attribute1', 'my first attribute', 'text'))
        self.assertEqual(
            self.inc_group_spec.get_attribute('attribute2'),
            AttributeSpec('attribute2',
                          'my second attribute',
                          'text',
                          value='fixed'))
        self.assertEqual(
            self.inc_group_spec.get_attribute('attribute3'),
            AttributeSpec('attribute3',
                          'my third attribute',
                          'text',
                          value='fixed'))

        self.assertTrue(self.inc_group_spec.resolved)

    def test_is_inherited_spec(self):
        self.assertFalse(self.def_group_spec.is_inherited_spec('attribute1'))
        self.assertFalse(self.def_group_spec.is_inherited_spec('attribute2'))
        self.assertTrue(
            self.inc_group_spec.is_inherited_spec(
                AttributeSpec('attribute1', 'my first attribute', 'text')))
        self.assertTrue(self.inc_group_spec.is_inherited_spec('attribute1'))
        self.assertTrue(self.inc_group_spec.is_inherited_spec('attribute2'))
        self.assertFalse(self.inc_group_spec.is_inherited_spec('attribute3'))
        self.assertFalse(self.inc_group_spec.is_inherited_spec('attribute4'))

    def test_is_overridden_spec(self):
        self.assertFalse(self.def_group_spec.is_overridden_spec('attribute1'))
        self.assertFalse(self.def_group_spec.is_overridden_spec('attribute2'))
        self.assertFalse(
            self.inc_group_spec.is_overridden_spec(
                AttributeSpec('attribute1', 'my first attribute', 'text')))
        self.assertFalse(self.inc_group_spec.is_overridden_spec('attribute1'))
        self.assertTrue(self.inc_group_spec.is_overridden_spec('attribute2'))
        self.assertFalse(self.inc_group_spec.is_overridden_spec('attribute3'))
        self.assertFalse(self.inc_group_spec.is_overridden_spec('attribute4'))

    def test_is_inherited_attribute(self):
        self.assertFalse(
            self.def_group_spec.is_inherited_attribute('attribute1'))
        self.assertFalse(
            self.def_group_spec.is_inherited_attribute('attribute2'))
        self.assertTrue(
            self.inc_group_spec.is_inherited_attribute('attribute1'))
        self.assertTrue(
            self.inc_group_spec.is_inherited_attribute('attribute2'))
        self.assertFalse(
            self.inc_group_spec.is_inherited_attribute('attribute3'))
        with self.assertRaisesWith(ValueError,
                                   "Attribute 'attribute4' not found"):
            self.inc_group_spec.is_inherited_attribute('attribute4')

    def test_is_overridden_attribute(self):
        self.assertFalse(
            self.def_group_spec.is_overridden_attribute('attribute1'))
        self.assertFalse(
            self.def_group_spec.is_overridden_attribute('attribute2'))
        self.assertFalse(
            self.inc_group_spec.is_overridden_attribute('attribute1'))
        self.assertTrue(
            self.inc_group_spec.is_overridden_attribute('attribute2'))
        self.assertFalse(
            self.inc_group_spec.is_overridden_attribute('attribute3'))
        with self.assertRaisesWith(ValueError,
                                   "Attribute 'attribute4' not found"):
            self.inc_group_spec.is_overridden_attribute('attribute4')
Esempio n. 6
0
    def test_update_docval(self):
        """Test update_docval_args for a variety of data types and mapping configurations."""
        spec = GroupSpec(
            doc="A test group specification with a data type",
            data_type_def="Baz",
            groups=[
                GroupSpec(doc="a group",
                          data_type_inc="EmptyBar",
                          quantity="?")
            ],
            datasets=[
                DatasetSpec(
                    doc="a dataset",
                    dtype="int",
                    name="data",
                    attributes=[
                        AttributeSpec(name="attr2",
                                      doc="an integer attribute",
                                      dtype="int")
                    ],
                )
            ],
            attributes=[
                AttributeSpec(name="attr1",
                              doc="a string attribute",
                              dtype="text"),
                AttributeSpec(name="attr3",
                              doc="a numeric attribute",
                              dtype="numeric"),
                AttributeSpec(name="attr4",
                              doc="a float attribute",
                              dtype="float"),
            ],
        )

        expected = [
            {
                'name': 'data',
                'type': (int, np.int32, np.int64),
                'doc': 'a dataset'
            },
            {
                'name': 'attr1',
                'type': str,
                'doc': 'a string attribute'
            },
            {
                'name': 'attr2',
                'type': (int, np.int32, np.int64),
                'doc': 'an integer attribute'
            },
            {
                'name':
                'attr3',
                'doc':
                'a numeric attribute',
                'type':
                (float, np.float32, np.float64, np.int8, np.int16, np.int32,
                 np.int64, int, np.uint8, np.uint16, np.uint32, np.uint64)
            },
            {
                'name': 'attr4',
                'doc': 'a float attribute',
                'type': (float, np.float32, np.float64)
            },
            {
                'name': 'bar',
                'type': EmptyBar,
                'doc': 'a group',
                'default': None
            },
        ]

        not_inherited_fields = {
            'data': spec.get_dataset('data'),
            'attr1': spec.get_attribute('attr1'),
            'attr2': spec.get_dataset('data').get_attribute('attr2'),
            'attr3': spec.get_attribute('attr3'),
            'attr4': spec.get_attribute('attr4'),
            'bar': spec.groups[0]
        }

        docval_args = list()
        for i, attr_name in enumerate(not_inherited_fields):
            with self.subTest(attr_name=attr_name):
                CustomClassGenerator.process_field_spec(
                    classdict={},
                    docval_args=docval_args,
                    parent_cls=EmptyBar,  # <-- arbitrary class
                    attr_name=attr_name,
                    not_inherited_fields=not_inherited_fields,
                    type_map=self.type_map,
                    spec=spec)
                self.assertListEqual(docval_args, expected[:(
                    i + 1)])  # compare with the first i elements of expected