示例#1
0
    def test_copy_array_exists_array(self):
        source = self.source
        dest = self.new_dest()

        # copy array, dest array in the way
        dest.create_dataset('baz', shape=(10,))

        # raise
        with pytest.raises(CopyError):
            # should raise by default
            copy(source['foo/bar/baz'], dest)
        assert (10,) == dest['baz'].shape
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest, if_exists='raise')
        assert (10,) == dest['baz'].shape

        # skip
        copy(source['foo/bar/baz'], dest, if_exists='skip')
        assert (10,) == dest['baz'].shape

        # replace
        copy(source['foo/bar/baz'], dest, if_exists='replace')
        check_copied_array(source['foo/bar/baz'], dest['baz'])

        # invalid option
        with pytest.raises(ValueError):
            copy(source['foo/bar/baz'], dest, if_exists='foobar')
示例#2
0
    def test_copy_group(self):
        source = self.source
        dest = self.new_dest()

        # copy group, default options
        copy(source['foo'], dest)
        check_copied_group(source['foo'], dest['foo'])
示例#3
0
    def test_copy_bad_dest(self):
        source = self.source

        # try to copy to an array, dest must be a group
        dest = self.new_dest().create_dataset('eggs', shape=(100,))
        with pytest.raises(ValueError):
            copy(source['foo/bar/baz'], dest)
示例#4
0
    def test_copy_group_shallow(self):
        source = self.source
        dest = self.new_dest()

        # copy group, shallow
        copy(source, dest, name='eggs', shallow=True)
        check_copied_group(source, dest['eggs'], shallow=True)
示例#5
0
    def test_copy_group_options(self):
        source = self.source
        dest = self.new_dest()

        # copy group, non-default options
        copy(source['foo'], dest, name='qux', without_attrs=True)
        assert 'foo' not in dest
        check_copied_group(source['foo'], dest['qux'], without_attrs=True)
示例#6
0
    def test_copy_group_exists_group(self):
        source = self.source
        dest = self.new_dest()

        # copy group, dest groups exist
        dest.create_group('foo/bar')
        copy(source['foo'], dest)
        check_copied_group(source['foo'], dest['foo'])
示例#7
0
    def test_copy_array_name(self):
        source = self.source
        dest = self.new_dest()

        # copy array with name
        copy(source['foo/bar/baz'], dest, name='qux')
        assert 'baz' not in dest
        check_copied_array(source['foo/bar/baz'], dest['qux'])
示例#8
0
    def test_copy_array(self):
        source = self.source
        dest = self.new_dest()

        # copy array with default options
        copy(source['foo/bar/baz'], dest)
        check_copied_array(source['foo/bar/baz'], dest['baz'])
        copy(source['spam'], dest)
        check_copied_array(source['spam'], dest['spam'])
示例#9
0
    def test_copy_group_no_name(self):
        source = self.source
        dest = self.new_dest()

        with pytest.raises(TypeError):
            # need a name if copy root
            copy(source, dest)

        copy(source, dest, name='root')
        check_copied_group(source, dest['root'])
示例#10
0
    def test_copy_array_create_options(self):
        source = self.source
        dest = self.new_dest()

        # copy array, provide creation options
        compressor = Zlib(9)
        create_kws = dict(chunks=(10,))
        if self.dest_h5py:
            create_kws.update(compression='gzip', compression_opts=9,
                              shuffle=True, fletcher32=True, fillvalue=42)
        else:
            create_kws.update(compressor=compressor, fill_value=42, order='F',
                              filters=[Adler32()])
        copy(source['foo/bar/baz'], dest, without_attrs=True, **create_kws)
        check_copied_array(source['foo/bar/baz'], dest['baz'],
                           without_attrs=True, expect_props=create_kws)
示例#11
0
    def test_copy_group_dry_run(self):
        source = self.source
        dest = self.new_dest()

        # dry run, empty destination
        n_copied, n_skipped, n_bytes_copied = \
            copy(source['foo'], dest, dry_run=True, return_stats=True)
        assert 0 == len(dest)
        assert 3 == n_copied
        assert 0 == n_skipped
        assert 0 == n_bytes_copied

        # dry run, array exists in destination
        baz = np.arange(100, 200)
        dest.create_dataset('foo/bar/baz', data=baz)
        assert not np.all(source['foo/bar/baz'][:] == dest['foo/bar/baz'][:])
        assert 1 == len(dest)

        # raise
        with pytest.raises(CopyError):
            copy(source['foo'], dest, dry_run=True)
        assert 1 == len(dest)

        # skip
        n_copied, n_skipped, n_bytes_copied = \
            copy(source['foo'], dest, dry_run=True, if_exists='skip',
                 return_stats=True)
        assert 1 == len(dest)
        assert 2 == n_copied
        assert 1 == n_skipped
        assert 0 == n_bytes_copied
        assert_array_equal(baz, dest['foo/bar/baz'])

        # replace
        n_copied, n_skipped, n_bytes_copied = \
            copy(source['foo'], dest, dry_run=True, if_exists='replace',
                 return_stats=True)
        assert 1 == len(dest)
        assert 3 == n_copied
        assert 0 == n_skipped
        assert 0 == n_bytes_copied
        assert_array_equal(baz, dest['foo/bar/baz'])
示例#12
0
    def test_copy_array_exists_array(self, source, dest):
        # copy array, dest array in the way
        dest.create_dataset('baz', shape=(10,))

        # raise
        with pytest.raises(CopyError):
            # should raise by default
            copy(source['foo/bar/baz'], dest)
        assert (10,) == dest['baz'].shape
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest, if_exists='raise')
        assert (10,) == dest['baz'].shape

        # skip
        copy(source['foo/bar/baz'], dest, if_exists='skip')
        assert (10,) == dest['baz'].shape

        # replace
        copy(source['foo/bar/baz'], dest, if_exists='replace')
        check_copied_array(source['foo/bar/baz'], dest['baz'])

        # invalid option
        with pytest.raises(ValueError):
            copy(source['foo/bar/baz'], dest, if_exists='foobar')
示例#13
0
    def test_copy_group_no_name(self, source, dest):
        if source.__module__.startswith('h5py'):
            with pytest.raises(TypeError):
                copy(source, dest)
        else:
            # For v3, dest.name will be inferred from source.name
            copy(source, dest)
            check_copied_group(source, dest[source.name.lstrip('/')])

        copy(source, dest, name='root')
        check_copied_group(source, dest['root'])
    def test_logging(self):
        source = self.source
        dest = self.new_dest()

        # callable log
        copy(source['foo'], dest, dry_run=True, log=print)

        # file name
        fn = tempfile.mktemp()
        atexit.register(os.remove, fn)
        copy(source['foo'], dest, dry_run=True, log=fn)

        # file
        with tempfile.TemporaryFile(mode='w') as f:
            copy(source['foo'], dest, dry_run=True, log=f)

        # bad option
        with pytest.raises(TypeError):
            copy(source['foo'], dest, dry_run=True, log=True)
示例#15
0
    def test_copy_group_exists_array(self, source, dest):
        # copy group, dest array in the way
        dest.create_dataset('foo/bar', shape=(10, ))

        # raise
        with pytest.raises(CopyError):
            copy(source['foo'], dest)
        assert dest['foo/bar'].shape == (10, )
        with pytest.raises(CopyError):
            copy(source['foo'], dest, if_exists='raise')
        assert dest['foo/bar'].shape == (10, )

        # skip
        copy(source['foo'], dest, if_exists='skip')
        assert dest['foo/bar'].shape == (10, )

        # replace
        copy(source['foo'], dest, if_exists='replace')
        check_copied_group(source['foo'], dest['foo'])
示例#16
0
    def test_copy_array_exists_group(self, source, dest):
        # copy array, dest group in the way
        dest.create_group('baz')

        # raise
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest)
        assert not hasattr(dest['baz'], 'shape')
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest, if_exists='raise')
        assert not hasattr(dest['baz'], 'shape')

        # skip
        copy(source['foo/bar/baz'], dest, if_exists='skip')
        assert not hasattr(dest['baz'], 'shape')

        # replace
        copy(source['foo/bar/baz'], dest, if_exists='replace')
        check_copied_array(source['foo/bar/baz'], dest['baz'])
示例#17
0
    def test_logging(self):
        source = self.source
        dest = self.new_dest()

        # callable log
        copy(source['foo'], dest, dry_run=True, log=print)

        # file name
        fn = tempfile.mktemp()
        atexit.register(os.remove, fn)
        copy(source['foo'], dest, dry_run=True, log=fn)

        # file
        with tempfile.TemporaryFile(mode='w') as f:
            copy(source['foo'], dest, dry_run=True, log=f)

        # bad option
        with pytest.raises(TypeError):
            copy(source['foo'], dest, dry_run=True, log=True)
示例#18
0
    def test_copy_array_exists_group(self):
        source = self.source
        dest = self.new_dest()

        # copy array, dest group in the way
        dest.create_group('baz')

        # raise
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest)
        assert not hasattr(dest['baz'], 'shape')
        with pytest.raises(CopyError):
            copy(source['foo/bar/baz'], dest, if_exists='raise')
        assert not hasattr(dest['baz'], 'shape')

        # skip
        copy(source['foo/bar/baz'], dest, if_exists='skip')
        assert not hasattr(dest['baz'], 'shape')

        # replace
        copy(source['foo/bar/baz'], dest, if_exists='replace')
        check_copied_array(source['foo/bar/baz'], dest['baz'])
示例#19
0
    def test_copy_group_exists_array(self):
        source = self.source
        dest = self.new_dest()

        # copy group, dest array in the way
        dest.create_dataset('foo/bar', shape=(10,))

        # raise
        with pytest.raises(CopyError):
            copy(source['foo'], dest)
        assert dest['foo/bar'].shape == (10,)
        with pytest.raises(CopyError):
            copy(source['foo'], dest, if_exists='raise')
        assert dest['foo/bar'].shape == (10,)

        # skip
        copy(source['foo'], dest, if_exists='skip')
        assert dest['foo/bar'].shape == (10,)

        # replace
        copy(source['foo'], dest, if_exists='replace')
        check_copied_group(source['foo'], dest['foo'])
    def test_copy_array_skip_initialized(self):
        source = self.source
        dest = self.new_dest()
        dest.create_dataset('baz', shape=(100, ), chunks=(10, ), dtype='i8')
        assert not np.all(source['foo/bar/baz'][:] == dest['baz'][:])

        if self.dest_h5py:
            with pytest.raises(ValueError):
                # not available with copy to h5py
                copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')

        else:
            # copy array, dest array exists but not yet initialized
            copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')
            check_copied_array(source['foo/bar/baz'], dest['baz'])

            # copy array, dest array exists and initialized, will be skipped
            dest['baz'][:] = np.arange(100, 200)
            copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')
            assert_array_equal(np.arange(100, 200), dest['baz'][:])
            assert not np.all(source['foo/bar/baz'][:] == dest['baz'][:])
示例#21
0
    def test_copy_array_skip_initialized(self):
        source = self.source
        dest = self.new_dest()
        dest.create_dataset('baz', shape=(100,), chunks=(10,), dtype='i8')
        assert not np.all(source['foo/bar/baz'][:] == dest['baz'][:])

        if self.dest_h5py:
            with pytest.raises(ValueError):
                # not available with copy to h5py
                copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')

        else:
            # copy array, dest array exists but not yet initialized
            copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')
            check_copied_array(source['foo/bar/baz'], dest['baz'])

            # copy array, dest array exists and initialized, will be skipped
            dest['baz'][:] = np.arange(100, 200)
            copy(source['foo/bar/baz'], dest, if_exists='skip_initialized')
            assert_array_equal(np.arange(100, 200), dest['baz'][:])
            assert not np.all(source['foo/bar/baz'][:] == dest['baz'][:])
示例#22
0
 def test_copy_group_exists_group(self, source, dest):
     # copy group, dest groups exist
     dest.create_group('foo/bar')
     copy(source['foo'], dest)
     check_copied_group(source['foo'], dest['foo'])
示例#23
0
 def test_copy_group_shallow(self, source, dest):
     # copy group, shallow
     copy(source, dest, name='eggs', shallow=True)
     check_copied_group(source, dest['eggs'], shallow=True)
示例#24
0
 def test_copy_group_options(self, source, dest):
     # copy group, non-default options
     copy(source['foo'], dest, name='qux', without_attrs=True)
     assert 'foo' not in dest
     check_copied_group(source['foo'], dest['qux'], without_attrs=True)
示例#25
0
 def test_copy_group(self, source, dest):
     # copy group, default options
     copy(source['foo'], dest)
     check_copied_group(source['foo'], dest['foo'])
示例#26
0
 def test_copy_array_name(self, source, dest):
     # copy array with name
     copy(source['foo/bar/baz'], dest, name='qux')
     assert 'baz' not in dest
     check_copied_array(source['foo/bar/baz'], dest['qux'])
示例#27
0
 def test_copy_bad_dest(self, source, dest):
     # try to copy to an array, dest must be a group
     dest = dest.create_dataset('eggs', shape=(100, ))
     with pytest.raises(ValueError):
         copy(source['foo/bar/baz'], dest)
示例#28
0
 def test_copy_array(self, source, dest):
     # copy array with default options
     copy(source['foo/bar/baz'], dest)
     check_copied_array(source['foo/bar/baz'], dest['baz'])
     copy(source['spam'], dest)
     check_copied_array(source['spam'], dest['spam'])