-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_core.py
More file actions
354 lines (298 loc) · 11.5 KB
/
test_core.py
File metadata and controls
354 lines (298 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import dask.array
import jsonschema
import numpy as np
import pytest
import xarray as xr
from xarray_schema import DataArraySchema, DatasetSchema
from xarray_schema.base import SchemaError
from xarray_schema.components import (
ArrayTypeSchema,
AttrSchema,
AttrsSchema,
ChunksSchema,
DimsSchema,
DTypeSchema,
NameSchema,
ShapeSchema,
)
from xarray_schema.dataarray import CoordsSchema
@pytest.fixture
def ds():
ds = xr.Dataset(
{
'x': xr.DataArray(np.arange(4) - 2, dims='x'),
'foo': xr.DataArray(np.ones(4, dtype='i4'), dims='x'),
'bar': xr.DataArray(np.arange(8, dtype=np.float32).reshape(4, 2), dims=('x', 'y')),
}
)
return ds
@pytest.mark.parametrize(
'component, schema_args, validate, json',
[
(DTypeSchema, np.integer, ['i4', 'int', np.int32], 'integer'),
(DTypeSchema, np.int64, ['i8', np.int64], '<i8'),
(DTypeSchema, '<i8', ['i8', np.int64], '<i8'),
(DimsSchema, ('foo', None), [('foo', 'bar'), ('foo', 'baz')], ['foo', None]),
(DimsSchema, ('foo', 'bar'), [('foo', 'bar')], ['foo', 'bar']),
(ShapeSchema, (1, 2, None), [(1, 2, 3), (1, 2, 5)], [1, 2, None]),
(ShapeSchema, (1, 2, 3), [(1, 2, 3)], [1, 2, 3]),
(NameSchema, 'foo', ['foo'], 'foo'),
(ArrayTypeSchema, np.ndarray, [np.array([1, 2, 3])], "<class 'numpy.ndarray'>"),
(
ArrayTypeSchema,
dask.array.Array,
[dask.array.zeros(4)],
"<class 'dask.array.core.Array'>",
),
# schema_args for ChunksSchema include [chunks, dims, shape]
(ChunksSchema, True, [(((1, 1),), ('x',), (2,))], True),
(ChunksSchema, {'x': 2}, [(((2, 2),), ('x',), (4,))], {'x': 2}),
(ChunksSchema, {'x': (2, 2)}, [(((2, 2),), ('x',), (4,))], {'x': [2, 2]}),
(ChunksSchema, {'x': [2, 2]}, [(((2, 2),), ('x',), (4,))], {'x': [2, 2]}),
(ChunksSchema, {'x': 4}, [(((4,),), ('x',), (4,))], {'x': 4}),
(ChunksSchema, {'x': -1}, [(((4,),), ('x',), (4,))], {'x': -1}),
(ChunksSchema, {'x': (1, 2, 1)}, [(((1, 2, 1),), ('x',), (4,))], {'x': [1, 2, 1]}),
(
ChunksSchema,
{'x': 2, 'y': -1},
[(((2, 2), (10,)), ('x', 'y'), (4, 10))],
{'x': 2, 'y': -1},
),
(
AttrsSchema,
{'foo': AttrSchema(value='bar')},
[{'foo': 'bar'}],
{
'allow_extra_keys': True,
'require_all_keys': True,
'attrs': {'foo': {'type': None, 'value': 'bar'}},
},
),
(
AttrsSchema,
{'foo': AttrSchema(value=1)},
[{'foo': 1}],
{
'allow_extra_keys': True,
'require_all_keys': True,
'attrs': {'foo': {'type': None, 'value': 1}},
},
),
(
CoordsSchema,
{'x': DataArraySchema(name='x')},
[{'x': xr.DataArray([0, 1], name='x')}],
{'coords': {'x': {'name': 'x'}}, 'allow_extra_keys': True, 'require_all_keys': True},
),
],
)
def test_component_schema(component, schema_args, validate, json):
schema = component(schema_args)
for v in validate:
if component in [ChunksSchema]: # special case construction
schema.validate(*v)
else:
schema.validate(v)
assert schema.json == json
assert isinstance(schema.to_json(), str)
# validate schema
jsonschema.validate(schema.json, schema._json_schema)
# json roundtrip
component.from_json(schema.json).json == json
@pytest.mark.parametrize(
'type, value, validate, json',
[
(str, None, 'foo', {'type': str, 'value': None}),
(None, 'foo', 'foo', {'type': None, 'value': 'foo'}),
(str, 'foo', 'foo', {'type': str, 'value': 'foo'}),
],
)
def test_attr_schema(type, value, validate, json):
schema = AttrSchema(type=type, value=value)
schema.validate(validate)
assert schema.json == json
# assert isinstance(schema.to_json(), str)
@pytest.mark.parametrize(
'component, schema_args, validate, match',
[
(DTypeSchema, np.integer, np.float32, r'.*float.*'),
(DimsSchema, ('foo', 'bar'), ('foo',), r'.*length.*'),
(DimsSchema, ('foo', 'bar'), ('foo', 'baz'), r'.*mismatch.*'),
(ShapeSchema, (1, 2, None), (1, 2), r'.*number of dimensions.*'),
(ShapeSchema, (1, 4, 4), (1, 3, 4), r'.*mismatch.*'),
(NameSchema, 'foo', 'bar', r'.*name bar != foo.*'),
(ArrayTypeSchema, np.ndarray, 'bar', r'.*array_type.*'),
# schema_args for ChunksSchema include [chunks, dims, shape]
(ChunksSchema, {'x': 3}, (((2, 2),), ('x',), (4,)), r'.*(3).*'),
(ChunksSchema, {'x': (2, 1)}, (((2, 2),), ('x',), (4,)), r'.*(2, 1).*'),
(ChunksSchema, {'x': (2, 1)}, (None, ('x',), (4,)), r'.*expected array to be chunked.*'),
(ChunksSchema, True, (None, ('x',), (4,)), r'.*expected array to be chunked.*'),
(
ChunksSchema,
False,
(((2, 2),), ('x',), (4,)),
r'.*expected unchunked array but it is chunked*',
),
(ChunksSchema, {'x': -1}, (((1, 2, 1),), ('x',), (4,)), r'.*did not match.*'),
(ChunksSchema, {'x': 2}, (((2, 3, 2),), ('x',), (7,)), r'.*did not match.*'),
(ChunksSchema, {'x': 2}, (((2, 2, 3),), ('x',), (7,)), r'.*did not match.*'),
(ChunksSchema, {'x': 2, 'y': -1}, (((2, 2), (5, 5)), ('x', 'y'), (4, 10)), r'.*(5).*'),
],
)
def test_component_raises_schema_error(component, schema_args, validate, match):
schema = component(schema_args)
with pytest.raises(SchemaError, match=match):
if component in [ChunksSchema]: # special case construction
schema.validate(*validate)
else:
schema.validate(validate)
def test_chunks_schema_raises_for_invalid_chunks():
with pytest.raises(ValueError, match=r'.*int.*'):
schema = ChunksSchema(chunks=2)
schema.validate(((2, 2),), ('x',), (4,))
def test_unknown_array_type_raises():
with pytest.raises(ValueError, match=r'.*unknown array_type.*'):
_ = ArrayTypeSchema.from_json('foo.array')
def test_dataarray_empty_constructor():
da = xr.DataArray(np.ones(4, dtype='i4'))
da_schema = DataArraySchema()
assert hasattr(da_schema, 'validate')
jsonschema.validate(da_schema.json, da_schema._json_schema)
assert da_schema.json == {}
da_schema.validate(da)
@pytest.mark.parametrize(
'kind, component, schema_args',
[
('dtype', DTypeSchema, 'i4'),
('dims', DimsSchema, ('x', None)),
('shape', ShapeSchema, (2, None)),
('name', NameSchema, 'foo'),
('array_type', ArrayTypeSchema, np.ndarray),
('chunks', ChunksSchema, False),
],
)
def test_dataarray_component_constructors(kind, component, schema_args):
da = xr.DataArray(np.zeros((2, 4), dtype='i4'), dims=('x', 'y'), name='foo')
comp_schema = component(schema_args)
schema = DataArraySchema(**{kind: schema_args})
assert comp_schema.json == getattr(schema, kind).json
jsonschema.validate(schema.json, schema._json_schema)
assert isinstance(getattr(schema, kind), component)
# json roundtrip
rt_schema = DataArraySchema.from_json(schema.json)
assert isinstance(rt_schema, DataArraySchema)
assert rt_schema.json == schema.json
schema.validate(da)
def test_dataarray_schema_validate_raises_for_invalid_input_type():
ds = xr.Dataset()
schema = DataArraySchema()
with pytest.raises(ValueError, match='Input must be a xarray.DataArray'):
schema.validate(ds)
def test_dataset_empty_constructor():
ds_schema = DatasetSchema()
assert hasattr(ds_schema, 'validate')
jsonschema.validate(ds_schema.json, ds_schema._json_schema)
ds_schema.json == {}
def test_dataset_example(ds):
ds_schema = DatasetSchema(
data_vars={
'foo': DataArraySchema(name='foo', dtype=np.int32, dims=['x']),
'bar': DataArraySchema(name='bar', dtype=np.floating, dims=['x', 'y']),
},
coords={'x': DataArraySchema(name='x', dtype=np.int64, dims=['x'])},
attrs={},
)
jsonschema.validate(ds_schema.json, ds_schema._json_schema)
assert list(ds_schema.json['data_vars'].keys()) == ['foo', 'bar']
assert list(ds_schema.json['coords']['coords'].keys()) == ['x']
ds_schema.validate(ds)
ds2 = ds.copy()
ds2['foo'] = ds2.foo.astype('float32')
with pytest.raises(SchemaError, match='dtype'):
ds_schema.validate(ds2)
ds2 = ds2.drop_vars('foo')
with pytest.raises(SchemaError, match='variable foo'):
ds_schema.validate(ds2)
ds3 = ds.copy()
ds3['x'] = ds3.x.astype('float32')
with pytest.raises(SchemaError, match='dtype'):
ds_schema.validate(ds3)
ds3 = ds3.drop_vars('x')
with pytest.raises(SchemaError, match='coords has missing keys'):
ds_schema.validate(ds3)
# json roundtrip
rt_schema = DatasetSchema.from_json(ds_schema.json)
assert isinstance(rt_schema, DatasetSchema)
assert rt_schema.json == ds_schema.json
def test_checks_ds(ds):
def check_foo(ds):
assert 'foo' in ds
ds_schema = DatasetSchema(checks=[check_foo])
ds_schema.validate(ds)
ds = ds.drop_vars('foo')
with pytest.raises(AssertionError):
ds_schema.validate(ds)
ds_schema = DatasetSchema(checks=[])
ds_schema.validate(ds)
# TODO
# with pytest.raises(ValueError):
# DatasetSchema(checks=[2])
def test_dataset_with_attrs_schema():
name = 'name'
expected_value = 'expected_value'
actual_value = 'actual_value'
ds = xr.Dataset(attrs={name: actual_value})
ds_schema = DatasetSchema(attrs={name: AttrSchema(value=expected_value)})
jsonschema.validate(ds_schema.json, ds_schema._json_schema)
ds_schema_2 = DatasetSchema(attrs=AttrsSchema({name: AttrSchema(value=expected_value)}))
jsonschema.validate(ds_schema_2.json, ds_schema_2._json_schema)
with pytest.raises(SchemaError):
ds_schema.validate(ds)
with pytest.raises(SchemaError):
ds_schema_2.validate(ds)
def test_attrs_extra_key():
name = 'name'
value = 'value_2'
name_2 = 'name_2'
value_2 = 'value_2'
ds = xr.Dataset(attrs={name: value})
ds_schema = DatasetSchema(
attrs=AttrsSchema(
attrs={
name: AttrSchema(
value=value,
),
name_2: AttrSchema(value=value_2),
},
require_all_keys=True,
)
)
jsonschema.validate(ds_schema.json, ds_schema._json_schema)
with pytest.raises(SchemaError):
ds_schema.validate(ds)
def test_attrs_missing_key():
name = 'name'
value = 'value_2'
name_2 = 'name_2'
value_2 = 'value_2'
ds = xr.Dataset(attrs={name: value, name_2: value_2})
ds_schema = DatasetSchema(
attrs=AttrsSchema(attrs={name: AttrSchema(value=value)}, allow_extra_keys=False)
)
with pytest.raises(SchemaError):
ds_schema.validate(ds)
def test_checks_da(ds):
da = ds['foo']
def check_foo(da):
assert da.name == 'foo'
def check_bar(da):
assert da.name == 'bar'
schema = DataArraySchema(checks=[check_foo])
schema.validate(da)
schema = DataArraySchema(checks=[check_bar])
with pytest.raises(AssertionError):
schema.validate(da)
schema = DataArraySchema(checks=[])
schema.validate(da)
with pytest.raises(ValueError):
DataArraySchema(checks=[2])