def setUp(self): super().setUp() self.foo_bucket = FooBucket('test_foo_bucket', [ Foo('my_foo1', list(range(10)), 'value1', 10), Foo('my_foo2', list(range(10, 20)), 'value2', 20)]) self.foo_builders = { 'my_foo1': GroupBuilder('my_foo1', datasets={'my_data': DatasetBuilder( 'my_data', list(range(10)), attributes={'attr2': 10})}, attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo', 'object_id': self.foo_bucket.foos['my_foo1'].object_id}), 'my_foo2': GroupBuilder('my_foo2', datasets={'my_data': DatasetBuilder( 'my_data', list(range(10, 20)), attributes={'attr2': 20})}, attributes={'attr1': 'value2', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo', 'object_id': self.foo_bucket.foos['my_foo2'].object_id}) } self.setUpBucketBuilder() self.setUpBucketSpec() self.spec_catalog.register_spec(self.bucket_spec, 'test.yaml') self.type_map.register_container_type(CORE_NAMESPACE, 'FooBucket', FooBucket) self.type_map.register_map(FooBucket, self.setUpBucketMapper()) self.manager = BuildManager(self.type_map)
def test_build(self): ''' Test default mapping functionality when no attributes are nested ''' foo = Foo('my_foo1', [1, 2, 3], 'string', 10) baz = Baz('MyBaz', [foo], 'abcdefghijklmnopqrstuvwxyz') msg = "MyBaz (MyBaz): Could not find already-built Builder for Foo 'my_foo1' in BuildManager" with self.assertRaisesWith(ReferenceTargetNotBuiltError, msg): self.manager.build(baz, root=True)
def test_build(self): ''' Test default mapping functionality when no attributes are nested ''' foo = Foo('my_foo1', [1, 2, 3], 'string', 10) baz = Baz('MyBaz', [foo], 'abcdefghijklmnopqrstuvwxyz') foo_builder = self.manager.build(foo) baz_builder = self.manager.build(baz, root=True) expected = DatasetBuilder('MyBaz', [ReferenceBuilder(foo_builder)], attributes={ 'baz_attr': 'abcdefghijklmnopqrstuvwxyz', 'data_type': 'Baz', 'namespace': CORE_NAMESPACE, 'object_id': baz.object_id }) self.assertBuilderEqual(baz_builder, expected)
def test_build(self): container_inst = Foo('my_foo', list(range(10)), 'value1', 10) expected = GroupBuilder( 'my_foo', datasets={ 'my_data': DatasetBuilder( 'my_data', list(range(10)), attributes={'attr2': 10})}, attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'data_type': 'Foo', 'object_id': container_inst.object_id}) builder1 = self.manager.build(container_inst) self.assertDictEqual(builder1, expected)
def test_build(self): """Test that an error is raised when a spec is mapped to a non-existent container attribute.""" class Unmapper(ObjectMapper): def __init__(self, spec): super().__init__(spec) self.map_spec("unknown", self.spec.get_dataset('my_data')) self.type_map.register_map(Foo, Unmapper) # override container_inst = Foo('my_foo', list(range(10)), 'value1', 10) msg = ("Foo 'my_foo' does not have attribute 'unknown' for mapping to spec: %s" % self.foo_spec.get_dataset('my_data')) with self.assertRaisesWith(ContainerConfigurationError, msg): self.manager.build(container_inst)
def test_append_1_0_5(self): '''Test whether we can append to files made by hdmf version 1.0.5''' foo = Foo('foo3', [10, 20, 30, 40, 50], "I am foo3", 17, 3.14) foobucket = FooBucket('foobucket2', [foo]) with HDF5IO(self.path_1_0_5, manager=self.manager, mode='a') as io: read_foofile = io.read() read_foofile.add_bucket(foobucket) io.write(read_foofile) with HDF5IO(self.path_1_0_5, manager=self.manager, mode='r') as io: read_foofile = io.read() self.assertListEqual( read_foofile.buckets['foobucket2'].foos['foo3'].my_data[:]. tolist(), foo.my_data)
def test_append_1_0_5(self): '''Test whether we can append to files made by hdmf version 1.0.5''' foo = Foo('foo3', [10, 20, 30, 40, 50], "I am foo3", 17, 3.14) foobucket = FooBucket('foobucket2', [foo]) with HDF5IO(self.path_1_0_5, manager=self.manager, mode='a') as io: read_foofile = io.read() read_foofile.buckets.append(foobucket) foobucket.parent = read_foofile io.write(read_foofile) with HDF5IO(self.path_1_0_5, manager=self.manager, mode='r') as io: read_foofile = io.read() # workaround for the fact that order of buckets is not maintained for bucket in read_foofile.buckets: if bucket.name == 'foobucket2': self.assertListEqual(bucket.foos[0].my_data[:].tolist(), foo.my_data)
def test_build(self): """Test that an error is raised when a spec is not mapped to a container attribute.""" class Unmapper(ObjectMapper): def __init__(self, spec): super().__init__(spec) self.unmap( self.spec.get_dataset('my_data') ) # remove mapping from this spec to container attribute self.type_map.register_map(Foo, Unmapper) # override container_inst = Foo('my_foo', list(range(10)), 'value1', 10) msg = ( r"<class '.*Unmapper'> has no container attribute mapped to spec: %s" % re.escape(str(self.foo_spec.get_dataset('my_data')))) with self.assertRaisesRegex(ContainerConfigurationError, msg): self.manager.build(container_inst)
def setUp(self): self.manager = _get_manager() self.path = "test_io_hdf5.h5" self.foo_builder = GroupBuilder('foo1', attributes={'data_type': 'Foo', 'namespace': 'test_core', 'attr1': "bar", 'object_id': -1}, datasets={'my_data': DatasetBuilder('my_data', list(range(100, 200, 10)), attributes={'attr2': 17})}) self.foo = Foo('foo1', list(range(100, 200, 10)), attr1="bar", attr2=17, attr3=3.14) self.manager.prebuilt(self.foo, self.foo_builder) self.builder = GroupBuilder( 'root', source=self.path, groups={'test_bucket': GroupBuilder('test_bucket', groups={'foo_holder': GroupBuilder('foo_holder', groups={'foo1': self.foo_builder})})}, attributes={'data_type': 'FooFile'})