55# SPDX-License-Identifier: BSD-3-Clause
66#######################################################################
77
8+ import numpy as np
89import pytest
910
1011import blosc2
@@ -27,6 +28,24 @@ def _storage(contiguous, urlpath, mode="w"):
2728 return blosc2 .Storage (contiguous = contiguous , urlpath = urlpath , mode = mode )
2829
2930
31+ def _make_nested_blosc2_objects ():
32+ ndarray = blosc2 .arange (6 , dtype = np .int32 )
33+
34+ schunk = blosc2 .SChunk (chunksize = 16 )
35+ schunk .append_data (np .arange (4 , dtype = np .int32 ))
36+
37+ nested_vlarray = blosc2 .VLArray ()
38+ nested_vlarray .extend (["alpha" , {"beta" : 2 }])
39+
40+ nested_batchstore = blosc2 .BatchStore (max_blocksize = 2 )
41+ nested_batchstore .extend ([[1 , 2 ], ["x" , {"y" : 3 }]])
42+
43+ estore = blosc2 .EmbedStore ()
44+ estore ["/node" ] = blosc2 .arange (3 , dtype = np .int32 )
45+
46+ return ndarray , schunk , nested_vlarray , nested_batchstore , estore
47+
48+
3049@pytest .mark .parametrize (
3150 ("contiguous" , "urlpath" ),
3251 [
@@ -181,6 +200,31 @@ def test_batchstore_from_cframe():
181200 assert [batch [:] for batch in restored2 ] == expected
182201
183202
203+ def test_batchstore_msgpack_supports_blosc2_objects ():
204+ ndarray , schunk , nested_vlarray , nested_batchstore , estore = _make_nested_blosc2_objects ()
205+
206+ barray = blosc2 .BatchStore (max_blocksize = 2 )
207+ barray .append ([ndarray , schunk , nested_vlarray , nested_batchstore , estore ])
208+
209+ restored = barray [0 ][:]
210+
211+ assert isinstance (restored [0 ], blosc2 .NDArray )
212+ assert np .array_equal (restored [0 ][:], ndarray [:])
213+
214+ assert isinstance (restored [1 ], blosc2 .SChunk )
215+ assert restored [1 ].decompress_chunk (0 ) == schunk .decompress_chunk (0 )
216+
217+ assert isinstance (restored [2 ], blosc2 .VLArray )
218+ assert list (restored [2 ]) == list (nested_vlarray )
219+
220+ assert isinstance (restored [3 ], blosc2 .BatchStore )
221+ assert [batch [:] for batch in restored [3 ]] == [batch [:] for batch in nested_batchstore ]
222+
223+ assert isinstance (restored [4 ], blosc2 .EmbedStore )
224+ assert list (restored [4 ].keys ()) == ["/node" ]
225+ assert np .array_equal (restored [4 ]["/node" ][:], estore ["/node" ][:])
226+
227+
184228def test_batchstore_info ():
185229 barray = blosc2 .BatchStore ()
186230 barray .extend (BATCHES )
0 commit comments