Skip to content

Commit b6cdb18

Browse files
committed
Migrate test from zarr-developers#702
With thanks to @ombschervister
1 parent 7b2c493 commit b6cdb18

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

zarr/tests/test_core.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from numcodecs.compat import ensure_bytes, ensure_ndarray
1616
from numcodecs.tests.common import greetings
1717
from numpy.testing import assert_array_almost_equal, assert_array_equal
18+
from pkg_resources import parse_version
1819

1920
from zarr.core import Array
2021
from zarr.meta import json_loads
@@ -1362,6 +1363,44 @@ def test_object_codec_warnings(self):
13621363
if hasattr(z.store, 'close'):
13631364
z.store.close()
13641365

1366+
@unittest.skipIf(parse_version(np.__version__) < parse_version('1.14.0'),
1367+
"unsupported numpy version")
1368+
def test_structured_array_contain_object(self):
1369+
1370+
if "PartialRead" in self.__class__.__name__:
1371+
pytest.skip("partial reads of object arrays not supported")
1372+
1373+
# ----------- creation --------------
1374+
1375+
structured_dtype = [('c_obj', object), ('c_int', int)]
1376+
a = np.array([(b'aaa', 1),
1377+
(b'bbb', 2)], dtype=structured_dtype)
1378+
1379+
# zarr-array with structured dtype require object codec
1380+
with pytest.raises(ValueError):
1381+
self.create_array(shape=a.shape, dtype=structured_dtype)
1382+
1383+
# create zarr-array by np-array
1384+
za = self.create_array(shape=a.shape, dtype=structured_dtype, object_codec=Pickle())
1385+
za[:] = a
1386+
1387+
# must be equal
1388+
assert_array_equal(a, za[:])
1389+
1390+
# ---------- indexing ---------------
1391+
1392+
assert za[0] == a[0]
1393+
1394+
za[0] = (b'ccc', 3)
1395+
za[1:2] = np.array([(b'ddd', 4)], dtype=structured_dtype) # ToDo: not work with list
1396+
assert_array_equal(za[:], np.array([(b'ccc', 3), (b'ddd', 4)], dtype=structured_dtype))
1397+
1398+
za['c_obj'] = [b'eee', b'fff']
1399+
za['c_obj', 0] = b'ggg'
1400+
assert_array_equal(za[:], np.array([(b'ggg', 3), (b'fff', 4)], dtype=structured_dtype))
1401+
assert za['c_obj', 0] == b'ggg'
1402+
assert za[1, 'c_int'] == 4
1403+
13651404
def test_iteration_exceptions(self):
13661405
# zero d array
13671406
a = np.array(1, dtype=int)
@@ -1905,6 +1944,10 @@ def test_structured_with_object(self):
19051944
# Cannot hacking out object codec as N5 doesn't allow object codecs
19061945
pass
19071946

1947+
def test_structured_array_contain_object(self):
1948+
# Cannot hacking out object codec as N5 doesn't allow object codecs
1949+
pass
1950+
19081951
def test_attrs_n5_keywords(self):
19091952
z = self.create_array(shape=(1050,), chunks=100, dtype='i4')
19101953
for k in n5_keywords:
@@ -2338,6 +2381,10 @@ def test_object_arrays_danger(self):
23382381
# skip this one, cannot use delta with objects
23392382
pass
23402383

2384+
def test_structured_array_contain_object(self):
2385+
# skip this one, cannot use delta on structured array
2386+
pass
2387+
23412388

23422389
# custom store, does not support getsize()
23432390
class CustomMapping(object):

0 commit comments

Comments
 (0)