|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | from __future__ import absolute_import, print_function, division |
3 | 3 | import json |
| 4 | +import base64 |
4 | 5 |
|
5 | 6 |
|
6 | 7 | from nose.tools import eq_ as eq, assert_is_none, assert_raises |
7 | 8 | import numpy as np |
8 | 9 |
|
9 | 10 |
|
10 | | -from zarr.compat import binary_type, text_type |
| 11 | +from zarr.compat import binary_type, text_type, PY2 |
11 | 12 | from zarr.meta import decode_array_metadata, encode_dtype, decode_dtype, \ |
12 | 13 | ZARR_FORMAT, decode_group_metadata, encode_array_metadata |
13 | 14 | from zarr.errors import MetadataError |
@@ -113,7 +114,7 @@ def test_encode_decode_array_2(): |
113 | 114 | eq([df.get_config()], meta_dec['filters']) |
114 | 115 |
|
115 | 116 |
|
116 | | -def test_encode_decode_array_fill_values(): |
| 117 | +def test_encode_decode_fill_values_nan(): |
117 | 118 |
|
118 | 119 | fills = ( |
119 | 120 | (np.nan, "NaN", np.isnan), |
@@ -154,6 +155,47 @@ def test_encode_decode_array_fill_values(): |
154 | 155 | assert f(actual) |
155 | 156 |
|
156 | 157 |
|
| 158 | +def test_encode_decode_fill_values_bytes(): |
| 159 | + |
| 160 | + fills = b'foo', bytes(10) |
| 161 | + |
| 162 | + for v in fills: |
| 163 | + |
| 164 | + s = base64.standard_b64encode(v) |
| 165 | + if not PY2: |
| 166 | + s = str(s, 'ascii') |
| 167 | + |
| 168 | + meta = dict( |
| 169 | + shape=(100,), |
| 170 | + chunks=(10,), |
| 171 | + dtype=np.dtype('S10'), |
| 172 | + compressor=Zlib(1).get_config(), |
| 173 | + fill_value=v, |
| 174 | + filters=None, |
| 175 | + order='C' |
| 176 | + ) |
| 177 | + |
| 178 | + meta_json = '''{ |
| 179 | + "chunks": [10], |
| 180 | + "compressor": {"id": "zlib", "level": 1}, |
| 181 | + "dtype": "|S10", |
| 182 | + "fill_value": "%s", |
| 183 | + "filters": null, |
| 184 | + "order": "C", |
| 185 | + "shape": [100], |
| 186 | + "zarr_format": %s |
| 187 | + }''' % (s, ZARR_FORMAT) |
| 188 | + |
| 189 | + # test encoding |
| 190 | + meta_enc = encode_array_metadata(meta) |
| 191 | + assert_json_eq(meta_json, meta_enc) |
| 192 | + |
| 193 | + # test decoding |
| 194 | + meta_dec = decode_array_metadata(meta_enc) |
| 195 | + actual = meta_dec['fill_value'] |
| 196 | + eq(v, actual) |
| 197 | + |
| 198 | + |
157 | 199 | def test_decode_array_unsupported_format(): |
158 | 200 |
|
159 | 201 | # unsupported format |
|
0 commit comments