Skip to content

Commit 0c530c3

Browse files
jakirkhamjoshmoore
andauthored
Compare test data's content generally (#436)
Instead of strictly expecting values returned from a store to be `bytes` instances only in the tests, relax this to anything that supports the buffer protocol and has the expected content. This makes it possible to return things like NumPy `memmap` objects and still have the tests work correctly. Co-authored-by: Josh Moore <j.a.moore@dundee.ac.uk>
1 parent 760bf19 commit 0c530c3

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

zarr/tests/test_storage.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import pytest
1515
from numpy.testing import assert_array_almost_equal, assert_array_equal
1616

17+
from numcodecs.compat import ensure_bytes
18+
1719
from zarr.codecs import BZ2, AsType, Blosc, Zlib
1820
from zarr.errors import MetadataError
1921
from zarr.hierarchy import group
@@ -54,7 +56,7 @@ def test_get_set_del_contains(self):
5456
store['foo']
5557
store['foo'] = b'bar'
5658
assert 'foo' in store
57-
assert b'bar' == store['foo']
59+
assert b'bar' == ensure_bytes(store['foo'])
5860

5961
# test __delitem__ (optional)
6062
try:
@@ -101,10 +103,10 @@ def test_pop(self):
101103
store['baz'] = b'qux'
102104
assert len(store) == 2
103105
v = store.pop('foo')
104-
assert v == b'bar'
106+
assert ensure_bytes(v) == b'bar'
105107
assert len(store) == 1
106108
v = store.pop('baz')
107-
assert v == b'qux'
109+
assert ensure_bytes(v) == b'qux'
108110
assert len(store) == 0
109111
with pytest.raises(KeyError):
110112
store.pop('xxx')
@@ -123,7 +125,7 @@ def test_popitem(self):
123125
store['foo'] = b'bar'
124126
k, v = store.popitem()
125127
assert k == 'foo'
126-
assert v == b'bar'
128+
assert ensure_bytes(v) == b'bar'
127129
assert len(store) == 0
128130
with pytest.raises(KeyError):
129131
store.popitem()
@@ -148,8 +150,8 @@ def test_update(self):
148150
assert 'foo' not in store
149151
assert 'baz' not in store
150152
store.update(foo=b'bar', baz=b'quux')
151-
assert b'bar' == store['foo']
152-
assert b'quux' == store['baz']
153+
assert b'bar' == ensure_bytes(store['foo'])
154+
assert b'quux' == ensure_bytes(store['baz'])
153155

154156
if hasattr(store, 'close'):
155157
store.close()
@@ -174,9 +176,9 @@ def test_iterators(self):
174176
assert 4 == len(store)
175177
assert {'a', 'b', 'c/d', 'c/e/f'} == set(store)
176178
assert {'a', 'b', 'c/d', 'c/e/f'} == set(store.keys())
177-
assert {b'aaa', b'bbb', b'ddd', b'fff'} == set(store.values())
179+
assert {b'aaa', b'bbb', b'ddd', b'fff'} == set(map(ensure_bytes, store.values()))
178180
assert ({('a', b'aaa'), ('b', b'bbb'), ('c/d', b'ddd'), ('c/e/f', b'fff')} ==
179-
set(store.items()))
181+
set(map(lambda kv: (kv[0], ensure_bytes(kv[1])), store.items())))
180182

181183
if hasattr(store, 'close'):
182184
store.close()
@@ -203,8 +205,8 @@ def test_pickle(self):
203205
# verify
204206
assert n == len(store2)
205207
assert keys == sorted(store2.keys())
206-
assert b'bar' == store2['foo']
207-
assert b'quux' == store2['baz']
208+
assert b'bar' == ensure_bytes(store2['foo'])
209+
assert b'quux' == ensure_bytes(store2['baz'])
208210

209211
if hasattr(store2, 'close'):
210212
store2.close()
@@ -730,10 +732,10 @@ def setdel_hierarchy_checks(store):
730732
# test __setitem__ overwrite level
731733
store['x/y/z'] = b'xxx'
732734
store['x/y'] = b'yyy'
733-
assert b'yyy' == store['x/y']
735+
assert b'yyy' == ensure_bytes(store['x/y'])
734736
assert 'x/y/z' not in store
735737
store['x'] = b'zzz'
736-
assert b'zzz' == store['x']
738+
assert b'zzz' == ensure_bytes(store['x'])
737739
assert 'x/y' not in store
738740

739741
# test __delitem__ overwrite level
@@ -819,7 +821,7 @@ def test_pickle_ext(self):
819821
# check point to same underlying directory
820822
assert 'xxx' not in store
821823
store2['xxx'] = b'yyy'
822-
assert b'yyy' == store['xxx']
824+
assert b'yyy' == ensure_bytes(store['xxx'])
823825

824826
def test_setdel(self):
825827
store = self.create_store()

0 commit comments

Comments
 (0)