11# -*- coding: utf-8 -*-
22from __future__ import absolute_import , print_function , division
33import json
4- import math
54
65
7- from nose .tools import eq_ as eq , assert_is_none , assert_raises , assert_is
6+ from nose .tools import eq_ as eq , assert_is_none , assert_raises
87import numpy as np
98
109
11- from zarr .compat import PY2
10+ from zarr .compat import binary_type , text_type
1211from zarr .meta import decode_array_metadata , encode_dtype , decode_dtype , \
1312 ZARR_FORMAT , decode_group_metadata , encode_array_metadata
1413from zarr .errors import MetadataError
1514
1615
16+ def assert_json_eq (expect , actual ): # pragma: no cover
17+ if isinstance (expect , binary_type ):
18+ expect = text_type (expect , 'ascii' )
19+ if isinstance (actual , binary_type ):
20+ actual = text_type (actual , 'ascii' )
21+ ej = json .loads (expect )
22+ aj = json .loads (actual )
23+ eq (ej , aj )
24+
25+
1726def test_encode_decode_array_1 ():
1827
1928 meta = dict (
@@ -26,29 +35,23 @@ def test_encode_decode_array_1():
2635 order = 'C'
2736 )
2837
29- meta_bytes = '''{
30- "chunks": [
31- 10
32- ],
33- "compression": "zlib",
34- "compression_opts": 1,
35- "dtype": "<f8",
36- "fill_value": null,
37- "order": "C",
38- "shape": [
39- 100
40- ],
41- "zarr_format": %s
42- }''' % ZARR_FORMAT
43- if not PY2 :
44- meta_bytes = meta_bytes .encode ('ascii' )
38+ meta_json = '''{
39+ "chunks": [10],
40+ "compression": "zlib",
41+ "compression_opts": 1,
42+ "dtype": "<f8",
43+ "fill_value": null,
44+ "order": "C",
45+ "shape": [100],
46+ "zarr_format": %s
47+ }''' % ZARR_FORMAT
4548
4649 # test encoding
4750 meta_enc = encode_array_metadata (meta )
48- eq ( meta_bytes , meta_enc )
51+ assert_json_eq ( meta_json , meta_enc )
4952
5053 # test decoding
51- meta_dec = decode_array_metadata (meta_bytes )
54+ meta_dec = decode_array_metadata (meta_enc )
5255 eq (ZARR_FORMAT , meta_dec ['zarr_format' ])
5356 eq (meta ['shape' ], meta_dec ['shape' ])
5457 eq (meta ['chunks' ], meta_dec ['chunks' ])
@@ -72,44 +75,27 @@ def test_encode_decode_array_2():
7275 order = 'F'
7376 )
7477
75- meta_bytes = '''{
76- "chunks": [
77- 10,
78- 10
79- ],
80- "compression": "blosc",
81- "compression_opts": {
82- "clevel": 3,
83- "cname": "lz4",
84- "shuffle": 2
85- },
86- "dtype": [
87- [
88- "a",
89- "<i4"
90- ],
91- [
92- "b",
93- "|S10"
94- ]
95- ],
96- "fill_value": 42,
97- "order": "F",
98- "shape": [
99- 100,
100- 100
101- ],
102- "zarr_format": %s
103- }''' % ZARR_FORMAT
104- if not PY2 :
105- meta_bytes = meta_bytes .encode ('ascii' )
78+ meta_json = '''{
79+ "chunks": [10, 10],
80+ "compression": "blosc",
81+ "compression_opts": {
82+ "clevel": 3,
83+ "cname": "lz4",
84+ "shuffle": 2
85+ },
86+ "dtype": [["a", "<i4"], ["b", "|S10"]],
87+ "fill_value": 42,
88+ "order": "F",
89+ "shape": [100, 100],
90+ "zarr_format": %s
91+ }''' % ZARR_FORMAT
10692
10793 # test encoding
10894 meta_enc = encode_array_metadata (meta )
109- eq ( meta_bytes , meta_enc )
95+ assert_json_eq ( meta_json , meta_enc )
11096
11197 # test decoding
112- meta_dec = decode_array_metadata (meta_bytes )
98+ meta_dec = decode_array_metadata (meta_enc )
11399 eq (ZARR_FORMAT , meta_dec ['zarr_format' ])
114100 eq (meta ['shape' ], meta_dec ['shape' ])
115101 eq (meta ['chunks' ], meta_dec ['chunks' ])
@@ -122,31 +108,27 @@ def test_encode_decode_array_2():
122108
123109def test_encode_decode_array_nan_fill_value ():
124110
125- for fill in math .nan , np .nan :
126-
127- meta = dict (
128- shape = (100 ,),
129- chunks = (10 ,),
130- dtype = np .dtype ('f8' ),
131- compression = 'zlib' ,
132- compression_opts = 1 ,
133- fill_value = fill ,
134- order = 'C'
135- )
111+ meta = dict (
112+ shape = (100 ,),
113+ chunks = (10 ,),
114+ dtype = np .dtype ('f8' ),
115+ compression = 'zlib' ,
116+ compression_opts = 1 ,
117+ fill_value = np .nan ,
118+ order = 'C'
119+ )
136120
137- # test fill value round trip
138- meta_enc = encode_array_metadata (meta )
139- meta_dec = decode_array_metadata (meta_enc )
140- actual = meta_dec ['fill_value' ]
141- print (repr (actual ))
142- print (type (actual ))
143- assert np .isnan (actual )
121+ # test fill value round trip
122+ meta_enc = encode_array_metadata (meta )
123+ meta_dec = decode_array_metadata (meta_enc )
124+ actual = meta_dec ['fill_value' ]
125+ assert np .isnan (actual )
144126
145127
146128def test_decode_array_unsupported_format ():
147129
148130 # unsupported format
149- meta_bytes = '''{
131+ meta_json = '''{
150132 "zarr_format": %s,
151133 "shape": [100],
152134 "chunks": [10],
@@ -156,22 +138,18 @@ def test_decode_array_unsupported_format():
156138 "fill_value": null,
157139 "order": "C"
158140 }''' % (ZARR_FORMAT - 1 )
159- if not PY2 :
160- meta_bytes = meta_bytes .encode ('ascii' )
161141 with assert_raises (MetadataError ):
162- decode_array_metadata (meta_bytes )
142+ decode_array_metadata (meta_json )
163143
164144
165145def test_decode_array_missing_fields ():
166146
167147 # missing fields
168- meta_bytes = '''{
148+ meta_json = '''{
169149 "zarr_format": %s
170150 }''' % ZARR_FORMAT
171- if not PY2 :
172- meta_bytes = meta_bytes .encode ('ascii' )
173151 with assert_raises (MetadataError ):
174- decode_array_metadata (meta_bytes )
152+ decode_array_metadata (meta_json )
175153
176154
177155def test_encode_decode_dtype ():
@@ -190,16 +168,12 @@ def test_decode_group():
190168 b = '''{
191169 "zarr_format": %s
192170 }''' % ZARR_FORMAT
193- if not PY2 :
194- b = b .encode ('ascii' )
195171 meta = decode_group_metadata (b )
196172 eq (ZARR_FORMAT , meta ['zarr_format' ])
197173
198174 # unsupported format
199175 b = '''{
200176 "zarr_format": %s
201177 }''' % (ZARR_FORMAT - 1 )
202- if not PY2 :
203- b = b .encode ('ascii' )
204178 with assert_raises (MetadataError ):
205179 decode_group_metadata (b )
0 commit comments