コード例 #1
0
class GroupBuilderSetterTests(TestCase):
    """Tests for setter functions in GroupBuilder class"""
    def setUp(self):
        self.gb = GroupBuilder('gb')
        self.gb2 = GroupBuilder('gb2', source='file1')

    def tearDown(self):
        pass

    def test_setitem_disabled(self):
        """Test __set_item__ is disabled"""
        with self.assertRaises(NotImplementedError):
            self.gb['key'] = 'value'

    def test_add_dataset(self):
        ds = self.gb.add_dataset('my_dataset', list(range(10)))
        self.assertIsInstance(ds, DatasetBuilder)
        self.assertIs(self.gb, ds.parent)

    def test_add_group(self):
        gp = self.gb.add_group('my_subgroup')
        self.assertIsInstance(gp, GroupBuilder)
        self.assertIs(self.gb['my_subgroup'], gp)
        self.assertIs(self.gb, gp.parent)

    def test_add_link(self):
        gp = self.gb.add_group('my_subgroup')
        sl = self.gb.add_link(gp, 'my_link')
        self.assertIsInstance(sl, LinkBuilder)
        self.assertIs(self.gb['my_link'], sl)
        self.assertIs(self.gb, sl.parent)

    def test_add_external_link(self):
        gp = self.gb2.add_group('my_subgroup')
        el = self.gb.add_link(gp, 'my_externallink')
        self.assertIsInstance(el, LinkBuilder)
        self.assertIs(self.gb['my_externallink'], el)
        self.assertIs(self.gb, el.parent)
        self.assertIs(self.gb2, gp.parent)

    # @unittest.expectedFailure
    def test_set_attribute(self):
        self.gb.set_attribute('key', 'value')
        self.assertIn('key', self.gb.obj_type)
        # self.assertEqual(dict.__getitem__(self.gb, 'attributes')['key'], 'value')
        self.assertEqual(self.gb['key'], 'value')

    def test_parent_constructor(self):
        gb2 = GroupBuilder('gb2', parent=self.gb)
        self.assertIs(gb2.parent, self.gb)

    def test_set_group(self):
        self.gb.set_group(self.gb2)
        self.assertIs(self.gb2.parent, self.gb)
コード例 #2
0
ファイル: test_io_hdf5.py プロジェクト: t-b/hdmf
class TestHDF5Writer(GroupBuilderTestCase):
    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"
                                        },
                                        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'})

    def tearDown(self):
        if os.path.exists(self.path):
            os.remove(self.path)

    def check_fields(self):
        f = File(self.path)
        self.assertIn('test_bucket', f)
        bucket = f.get('test_bucket')
        self.assertIn('foo_holder', bucket)
        holder = bucket.get('foo_holder')
        self.assertIn('foo1', holder)
        return f

    def test_write_builder(self):
        writer = HDF5IO(self.path, manager=self.manager, mode='a')
        writer.write_builder(self.builder)
        writer.close()
        self.check_fields()

    def test_write_attribute_reference_container(self):
        writer = HDF5IO(self.path, manager=self.manager, mode='a')
        self.builder.set_attribute('ref_attribute', self.foo)
        writer.write_builder(self.builder)
        writer.close()
        f = self.check_fields()
        self.assertIsInstance(f.attrs['ref_attribute'], Reference)
        self.assertEqual(f['test_bucket/foo_holder/foo1'],
                         f[f.attrs['ref_attribute']])

    def test_write_attribute_reference_builder(self):
        writer = HDF5IO(self.path, manager=self.manager, mode='a')
        self.builder.set_attribute('ref_attribute', self.foo_builder)
        writer.write_builder(self.builder)
        writer.close()
        f = self.check_fields()
        self.assertIsInstance(f.attrs['ref_attribute'], Reference)
        self.assertEqual(f['test_bucket/foo_holder/foo1'],
                         f[f.attrs['ref_attribute']])

    def test_write_context_manager(self):
        with HDF5IO(self.path, manager=self.manager, mode='a') as writer:
            writer.write_builder(self.builder)
        self.check_fields()

    def test_read_builder(self):
        self.maxDiff = None
        io = HDF5IO(self.path, manager=self.manager, mode='a')
        io.write_builder(self.builder)
        builder = io.read_builder()
        self.assertBuilderEqual(builder, self.builder)
        io.close()

    def test_overwrite_written(self):
        self.maxDiff = None
        io = HDF5IO(self.path, manager=self.manager, mode='a')
        io.write_builder(self.builder)
        builder = io.read_builder()
        with self.assertRaisesRegex(ValueError,
                                    "cannot change written to not written"):
            builder.written = False
        io.close()
コード例 #3
0
 def test_set_attribute(self):
     gb = GroupBuilder('gb')
     gb.set_attribute('key', 'value')
     self.assertIn('key', gb.obj_type)
     self.assertIn('key', gb.attributes)
     self.assertEqual(gb['key'], 'value')