Exemple #1
0
    def test_post_process_fixed_name(self):
        """Test that docval generation for a class with a fixed name does not contain a docval arg for name."""
        spec = GroupSpec(
            doc='A test group specification with a data type',
            data_type_def='Baz',
            name='MyBaz',  # <-- fixed name
            attributes=[
                AttributeSpec(name='attr1',
                              doc='a string attribute',
                              dtype='text',
                              shape=[None])
            ])

        classdict = {}
        bases = [Container]
        docval_args = [{
            'name': 'name',
            'type': str,
            'doc': 'name'
        }, {
            'name': 'attr1',
            'type': ('array_data', 'data'),
            'doc': 'a string attribute',
            'shape': [None]
        }]
        CustomClassGenerator.post_process(classdict, bases, docval_args, spec)

        expected = [{
            'name': 'attr1',
            'type': ('array_data', 'data'),
            'doc': 'a string attribute',
            'shape': [None]
        }]
        self.assertListEqual(docval_args, expected)
Exemple #2
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)
Exemple #3
0
    def test_update_docval_dset_shape(self):
        """Test that update_docval_args for a dataset with shape sets the type and shape keys."""
        spec = GroupSpec(doc='A test group specification with a data type',
                         data_type_def='Baz',
                         datasets=[
                             DatasetSpec(name='dset1',
                                         doc='a string dataset',
                                         dtype='text',
                                         shape=[None])
                         ])
        not_inherited_fields = {'dset1': spec.get_dataset('dset1')}

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

        expected = [{
            'name': 'dset1',
            'type': ('array_data', 'data'),
            'doc': 'a string dataset',
            'shape': [None]
        }]
        self.assertListEqual(docval_args, expected)
Exemple #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)
Exemple #5
0
    def test_process_field_spec_link(self):
        """Test that processing a link spec does not set child=True in __fields__."""
        classdict = {}
        not_inherited_fields = {
            'attr3': LinkSpec(name='attr3',
                              target_type='EmptyBar',
                              doc='a link')
        }
        CustomClassGenerator.process_field_spec(
            classdict=classdict,
            docval_args=[],
            parent_cls=EmptyBar,  # <-- arbitrary class
            attr_name='attr3',
            not_inherited_fields=not_inherited_fields,
            type_map=self.type_map,
            spec=GroupSpec('dummy', 'doc'))

        expected = {'__fields__': [{'name': 'attr3', 'doc': 'a link'}]}
        self.assertDictEqual(classdict, expected)
Exemple #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