Skip to content

Commit 78014cb

Browse files
committed
more object tests
1 parent eafecd9 commit 78014cb

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

zarr/tests/test_core.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from nose.tools import (eq_ as eq, assert_is_instance, assert_raises, assert_true,
1414
assert_false, assert_is, assert_is_none)
1515
from nose import SkipTest
16+
import pytest
1617

1718

1819
from zarr.storage import (DirectoryStore, init_array, init_group, NestedDirectoryStore,
@@ -22,7 +23,7 @@
2223
from zarr.compat import PY2
2324
from zarr.util import buffer_size
2425
from numcodecs import (Delta, FixedScaleOffset, Zlib, Blosc, BZ2, MsgPack, Pickle,
25-
Categorize)
26+
Categorize, JSON)
2627

2728

2829
# noinspection PyMethodMayBeStatic
@@ -901,6 +902,43 @@ def test_object_arrays(self):
901902
a = z[:]
902903
assert a.dtype == object
903904

905+
# create an object array using JSON
906+
z = self.create_array(shape=10, chunks=3, dtype=object, object_codec=JSON())
907+
z[0] = 'foo'
908+
assert z[0] == 'foo'
909+
# z[1] = b'bar'
910+
# assert z[1] == b'bar' # not supported for JSON
911+
z[2] = 1
912+
assert z[2] == 1
913+
z[3] = [2, 4, 6, 'baz']
914+
assert z[3] == [2, 4, 6, 'baz']
915+
z[4] = {'a': 'b', 'c': 'd'}
916+
assert z[4] == {'a': 'b', 'c': 'd'}
917+
a = z[:]
918+
assert a.dtype == object
919+
920+
def test_object_arrays_text(self):
921+
922+
from numcodecs.tests.common import greetings
923+
data = np.array(greetings * 1000, dtype=object)
924+
925+
z = self.create_array(shape=data.shape, dtype=object, object_codec=MsgPack())
926+
z[:] = data
927+
assert_array_equal(data, z[:])
928+
929+
z = self.create_array(shape=data.shape, dtype=object, object_codec=JSON())
930+
z[:] = data
931+
assert_array_equal(data, z[:])
932+
933+
z = self.create_array(shape=data.shape, dtype=object, object_codec=Pickle())
934+
z[:] = data
935+
assert_array_equal(data, z[:])
936+
937+
z = self.create_array(shape=data.shape, dtype=object,
938+
object_codec=Categorize(greetings, dtype=object))
939+
z[:] = data
940+
assert_array_equal(data, z[:])
941+
904942
def test_object_arrays_danger(self):
905943

906944
# do something dangerous - manually force an object array with no object codec
@@ -938,6 +976,12 @@ def test_object_arrays_danger(self):
938976
# noinspection PyStatementEffect
939977
v[:]
940978

979+
def test_object_codec_warnings(self):
980+
981+
with pytest.warns(UserWarning):
982+
# provide object_codec, but not object dtype
983+
self.create_array(shape=10, chunks=5, dtype='i4', object_codec=JSON())
984+
941985

942986
class TestArrayWithPath(TestArray):
943987

@@ -1367,6 +1411,10 @@ def test_object_arrays(self):
13671411
# skip this one, cannot use delta with objects
13681412
pass
13691413

1414+
def test_object_arrays_text(self):
1415+
# skip this one, cannot use delta with objects
1416+
pass
1417+
13701418
def test_object_arrays_danger(self):
13711419
# skip this one, cannot use delta with objects
13721420
pass

0 commit comments

Comments
 (0)