def test_dynamic_container_constructor_name(self): # name is specified in spec and cannot be changed baz_spec = GroupSpec( 'A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, name='A fixed name', attributes=[ AttributeSpec('attr3', 'an example float attribute', 'float'), AttributeSpec('attr4', 'another example float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_container_cls(CORE_NAMESPACE, 'Baz') with self.assertRaises(TypeError): inst = cls('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0) inst = cls([1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0) self.assertEqual(inst.name, 'A fixed name') self.assertEqual(inst.data, [1, 2, 3, 4]) self.assertEqual(inst.attr1, 'string attribute') self.assertEqual(inst.attr2, 1000) self.assertEqual(inst.attr3, 98.6) self.assertEqual(inst.attr4, 1.0)
def getSpecs(self): ret = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec('attr2', 'an example integer attribute', 'int') ]), DatasetSpec('an example time dataset', 'isodatetime', name='time'), DatasetSpec('an array of times', 'isodatetime', name='time_array', dims=('num_times', ), shape=(None, )) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text') ]) return (ret, )
def test_dynamic_container_constructor_name_default_name(self): # if both name and default_name are specified, name should be used with self.assertWarns(Warning): baz_spec = GroupSpec('A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, name='A fixed name', default_name='A default name', attributes=[ AttributeSpec('attr3', 'a float attribute', 'float'), AttributeSpec('attr4', 'another float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_dt_container_cls('Baz', CORE_NAMESPACE) inst = cls([1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0) self.assertEqual(inst.name, 'A fixed name')
def test_dynamic_container_constructor(self): baz_spec = GroupSpec('A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'a float attribute', 'float'), AttributeSpec('attr4', 'another float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_dt_container_cls('Baz', CORE_NAMESPACE) # TODO: test that constructor works! inst = cls('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0) self.assertEqual(inst.name, 'My Baz') self.assertEqual(inst.data, [1, 2, 3, 4]) self.assertEqual(inst.attr1, 'string attribute') self.assertEqual(inst.attr2, 1000) self.assertEqual(inst.attr3, 98.6) self.assertEqual(inst.attr4, 1.0)
def setUp(self): self.attr1 = AttributeSpec(name='attr1', doc='a string attribute', dtype='text') self.attr2 = AttributeSpec(name='attr2', doc='an integer attribute', dtype='int') self.attr3 = AttributeSpec(name='attr3', doc='an integer attribute', dtype='int') self.bar_spec = GroupSpec( doc='A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec(doc='a dataset', dtype='int', name='data', attributes=[self.attr2]) ], attributes=[self.attr1]) specs = [self.bar_spec] containers = {'Bar': Bar} self.type_map = create_test_type_map(specs, containers) self.spec_catalog = self.type_map.namespace_catalog.get_namespace( CORE_NAMESPACE).catalog self.cls = self.type_map.get_dt_container_cls(self.bar_spec.data_type) self.bar = self.cls(name='bar', data=[1], attr1='attr1', attr2=1) obj_mapper_bar = self.type_map.get_map(self.bar) obj_mapper_bar.map_spec('attr2', spec=self.attr2)
def getSpecs(self): bar = GroupSpec('A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec( 'attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', text('an example string attribute'), 'text') ]) foo = GroupSpec('A test group that contains a data type', data_type_def='Foo', groups=[ GroupSpec('A Bar group for Foos', name='my_bar', data_type_inc='Bar') ], attributes=[ AttributeSpec( 'foo_attr', 'a string attribute specified as text', 'text', required=False) ]) return (bar, foo)
def setUp(self): self.foo_spec = GroupSpec( doc='A test group specification with a data type', data_type_def='Foo', datasets=[ DatasetSpec(doc='an example dataset', dtype='int', name='my_data', attributes=[ AttributeSpec( name='attr2', doc='an example integer attribute', dtype='int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text') ]) self.spec_catalog = SpecCatalog() self.spec_catalog.register_spec(self.foo_spec, 'test.yaml') self.namespace = SpecNamespace('a test namespace', CORE_NAMESPACE, [{ 'source': 'test.yaml' }], version='0.1.0', catalog=self.spec_catalog) self.namespace_catalog = NamespaceCatalog() self.namespace_catalog.add_namespace(CORE_NAMESPACE, self.namespace) self.type_map = TypeMap(self.namespace_catalog) self.type_map.register_container_type(CORE_NAMESPACE, 'Foo', Foo) self.type_map.register_map(Foo, FooMapper) self.manager = BuildManager(self.type_map)
def setUp(self): self.bar_spec = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec('attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text') ]) self.spec_catalog = SpecCatalog() self.spec_catalog.register_spec(self.bar_spec, 'test.yaml') self.namespace = SpecNamespace('a test namespace', CORE_NAMESPACE, [{ 'source': 'test.yaml' }], catalog=self.spec_catalog) self.namespace_catalog = NamespaceCatalog() self.namespace_catalog.add_namespace(CORE_NAMESPACE, self.namespace) self.type_map = TypeMap(self.namespace_catalog) self.type_map.register_container_type(CORE_NAMESPACE, 'Bar', Bar) self.type_map.register_map(Bar, ObjectMapper) self.manager = BuildManager(self.type_map) self.mapper = ObjectMapper(self.bar_spec)
def setUp(self): self.foo_spec = GroupSpec( 'A test group specification with data type Foo', data_type_def='Foo') self.bar_spec = GroupSpec( 'A test group specification with a data type Bar', data_type_def='Bar', datasets=[DatasetSpec('an example dataset', 'int', name='data')], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text'), AttributeSpec('attr2', 'an example integer attribute', 'int'), AttributeSpec('foo', 'a referenced foo', RefSpec('Foo', 'object'), required=False) ]) self.spec_catalog = SpecCatalog() self.spec_catalog.register_spec(self.foo_spec, 'test.yaml') self.spec_catalog.register_spec(self.bar_spec, 'test.yaml') self.namespace = SpecNamespace('a test namespace', CORE_NAMESPACE, [{ 'source': 'test.yaml' }], version='0.1.0', catalog=self.spec_catalog) self.namespace_catalog = NamespaceCatalog() self.namespace_catalog.add_namespace(CORE_NAMESPACE, self.namespace) self.type_map = TypeMap(self.namespace_catalog) self.type_map.register_container_type(CORE_NAMESPACE, 'Foo', Foo) self.type_map.register_container_type(CORE_NAMESPACE, 'Bar', Bar) self.manager = BuildManager(self.type_map) self.foo_mapper = ObjectMapper(self.foo_spec) self.bar_mapper = ObjectMapper(self.bar_spec)
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')
def setUpBarSpec(self): self.bar_spec = GroupSpec( doc='A test group specification with a data type Bar', data_type_def='Bar', attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text'), AttributeSpec('attr2', 'an example integer attribute', 'int') ])
def test_type_extension(self): spec = GroupSpec('A test group', name='parent_type', datasets=self.datasets, attributes=self.attributes, linkable=False, data_type_def='EphysData') dset1_attributes_ext = [ AttributeSpec('dset1_extra_attribute', 'an extra attribute for the first dataset', 'text') ] ext_datasets = [ DatasetSpec('my first dataset extension', 'int', name='dataset1', attributes=dset1_attributes_ext, linkable=True), ] ext_attributes = [ AttributeSpec('ext_extra_attribute', 'an extra attribute for the group', 'text'), ] ext = GroupSpec('A test group extension', name='child_type', datasets=ext_datasets, attributes=ext_attributes, linkable=False, data_type_inc=spec, data_type_def='SpikeData') ext_dset1 = ext.get_dataset('dataset1') ext_dset1_attrs = ext_dset1.attributes self.assertDictEqual(ext_dset1_attrs[0], dset1_attributes_ext[0]) self.assertDictEqual(ext_dset1_attrs[1], self.dset1_attributes[0]) self.assertDictEqual(ext_dset1_attrs[2], self.dset1_attributes[1]) self.assertEqual(ext.data_type_def, 'SpikeData') self.assertEqual(ext.data_type_inc, 'EphysData') ext_dset2 = ext.get_dataset('dataset2') self.maxDiff = None # this will suffice for now, assertDictEqual doesn't do deep equality checks self.assertEqual(str(ext_dset2), str(self.datasets[1])) self.assertAttributesEqual(ext_dset2, self.datasets[1]) # self.ns_attr_spec ndt_attr_spec = AttributeSpec('data_type', 'the data type of this object', # noqa: F841 'text', value='SpikeData') res_attrs = ext.attributes self.assertDictEqual(res_attrs[0], ext_attributes[0]) self.assertDictEqual(res_attrs[1], self.attributes[0]) self.assertDictEqual(res_attrs[2], self.attributes[1]) # test that inherited specs are tracked appropriate for d in self.datasets: with self.subTest(dataset=d.name): self.assertTrue(ext.is_inherited_spec(d)) self.assertFalse(spec.is_inherited_spec(d)) json.dumps(spec)
def setUp(self): self.catalog = SpecCatalog() self.attributes = [ AttributeSpec('attribute1', 'my first attribute', 'text'), AttributeSpec('attribute2', 'my second attribute', 'text') ] self.spec = DatasetSpec('my first dataset', 'int', name='dataset1', dims=(None, None), attributes=self.attributes, linkable=False, data_type_def='EphysData')
def test_dynamic_container_composition_missing_type(self): baz_spec1 = GroupSpec( 'A composition test outside', data_type_def='Baz1', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'a float attribute', 'float'), AttributeSpec('attr4', 'another float attribute', 'float') ], groups=[GroupSpec('A composition inside', data_type_inc='Baz2')]) self.spec_catalog.register_spec(baz_spec1, 'extension.yaml') msg = "No specification for 'Baz2' in namespace 'test_core'" with self.assertRaisesWith(ValueError, msg): self.type_map.get_dt_container_cls('Baz1', CORE_NAMESPACE)
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)
def test_datatype_extension(self): base = DatasetSpec('my first dataset', 'int', name='dataset1', attributes=self.attributes, linkable=False, data_type_def='EphysData') attributes = [ AttributeSpec('attribute3', 'my first extending attribute', 'float') ] ext = DatasetSpec('my first dataset extension', 'int', name='dataset1', attributes=attributes, linkable=False, data_type_inc=base, data_type_def='SpikeData') self.assertDictEqual(ext['attributes'][0], attributes[0]) self.assertDictEqual(ext['attributes'][1], self.attributes[0]) self.assertDictEqual(ext['attributes'][2], self.attributes[1]) ext_attrs = ext.attributes self.assertIs(ext, ext_attrs[0].parent) self.assertIs(ext, ext_attrs[1].parent) self.assertIs(ext, ext_attrs[2].parent)
def setUpBarSpec(self): attr1_attr = AttributeSpec( name='attr1', dtype='text', doc='an example string attribute', ) attr2_attr = AttributeSpec( name='attr2', dtype='int', doc='an example int attribute', ) self.bar_spec = GroupSpec( doc='A test group specification with a data type', data_type_def='Bar', attributes=[attr1_attr, attr2_attr], )
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)
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)
def test_multi_container_spec(self): multi_spec = GroupSpec(doc='A test extension that contains a multi', data_type_def='Multi', groups=[ GroupSpec(data_type_inc=self.bar_spec, doc='test multi', quantity='*') ], attributes=[ AttributeSpec(name='attr3', doc='a float attribute', dtype='float') ]) self.spec_catalog.register_spec(multi_spec, 'extension.yaml') Bar = self.type_map.get_dt_container_cls('Bar', CORE_NAMESPACE) Multi = self.type_map.get_dt_container_cls('Multi', CORE_NAMESPACE) assert issubclass(Multi, MultiContainerInterface) assert Multi.__clsconf__ == [ dict(attr='bars', type=Bar, add='add_bars', get='get_bars', create='create_bars') ] multi = Multi(name='my_multi', bars=[Bar('my_bar', list(range(10)), 'value1', 10)], attr3=5.) assert multi.bars['my_bar'] == Bar('my_bar', list(range(10)), 'value1', 10) assert multi.attr3 == 5.
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_get_namespace_spec(self): expected = AttributeSpec( 'namespace', 'the namespace for the data type of this object', 'text', required=False) self.assertDictEqual(GroupSpec.get_namespace_spec(), expected)
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_invalid_dtype(self): with self.assertRaises(ValueError): AttributeSpec( name='attribute1', doc='my first attribute', dtype='invalid' # <-- Invalid dtype must raise a ValueError )
def test_dynamic_container_composition_reverse_order(self): baz_spec2 = GroupSpec('A composition inside', data_type_def='Baz2', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'a float attribute', 'float'), AttributeSpec('attr4', 'another float attribute', 'float') ]) baz_spec1 = GroupSpec( 'A composition test outside', data_type_def='Baz1', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'a float attribute', 'float'), AttributeSpec('attr4', 'another float attribute', 'float') ], groups=[GroupSpec('A composition inside', data_type_inc='Baz2')]) self.spec_catalog.register_spec(baz_spec1, 'extension.yaml') self.spec_catalog.register_spec(baz_spec2, 'extension.yaml') Baz1 = self.type_map.get_dt_container_cls('Baz1', CORE_NAMESPACE) Baz2 = self.type_map.get_dt_container_cls('Baz2', CORE_NAMESPACE) Baz1('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0, baz2=Baz2('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0)) Bar = self.type_map.get_dt_container_cls('Bar', CORE_NAMESPACE) bar = Bar('My Bar', [1, 2, 3, 4], 'string attribute', 1000) with self.assertRaises(TypeError): Baz1('My Baz', [1, 2, 3, 4], 'string attribute', 1000, attr3=98.6, attr4=1.0, baz2=bar)
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_dynamic_container_creation_defaults(self): baz_spec = GroupSpec( 'A test extension with no Container class', data_type_def='Baz', data_type_inc=self.bar_spec, attributes=[ AttributeSpec('attr3', 'an example float attribute', 'float'), AttributeSpec('attr4', 'another example float attribute', 'float') ]) self.spec_catalog.register_spec(baz_spec, 'extension.yaml') cls = self.type_map.get_container_cls(CORE_NAMESPACE, 'Baz') expected_args = {'name', 'data', 'attr1', 'attr2', 'attr3', 'attr4'} received_args = set(map(lambda x: x['name'], get_docval(cls.__init__))) self.assertSetEqual(expected_args, received_args) self.assertEqual(cls.__name__, 'Baz') self.assertTrue(issubclass(cls, Bar))
def setUpBarSpec(self): self.bar_spec = GroupSpec( doc='A test group specification with a data type Bar', data_type_def='Bar', attributes=[ AttributeSpec('foo', 'a referenced foo', RefSpec('Foo', 'object')) ])
def setUpBarSpec(self): self.bar_spec = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec('attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text') ])
def getSpecs(self): ret = GroupSpec( 'A test group specification with a data type', data_type_def='Bar', datasets=[ DatasetSpec('an example dataset', 'int', name='data', attributes=[ AttributeSpec('attr2', 'an example integer attribute', 'int') ]) ], attributes=[ AttributeSpec('attr1', 'an example string attribute', 'text') ]) return (ret, )