|
3 | 3 | import xarray as xr
|
4 | 4 |
|
5 | 5 | from xarray_schema import DataArraySchema, DatasetSchema
|
6 |
| -from xarray_schema.core import SchemaError |
| 6 | +from xarray_schema.base import SchemaError |
| 7 | +from xarray_schema.components import ( |
| 8 | + ArrayTypeSchema, |
| 9 | + ChunksSchema, |
| 10 | + DimsSchema, |
| 11 | + DTypeSchema, |
| 12 | + NameSchema, |
| 13 | + ShapeSchema, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def ds(): |
| 19 | + ds = xr.Dataset( |
| 20 | + { |
| 21 | + 'x': xr.DataArray(np.arange(4) - 2, dims='x'), |
| 22 | + 'foo': xr.DataArray(np.ones(4, dtype='i4'), dims='x'), |
| 23 | + 'bar': xr.DataArray(np.arange(8, dtype=np.float32).reshape(4, 2), dims=('x', 'y')), |
| 24 | + } |
| 25 | + ) |
| 26 | + return ds |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + 'component, schema_args, validate, json', |
| 31 | + [ |
| 32 | + (DTypeSchema, np.integer, ['i4', 'int', np.int32], 'integer'), |
| 33 | + (DTypeSchema, np.int64, ['i8', np.int64], '<i8'), |
| 34 | + (DTypeSchema, '<i8', ['i8', np.int64], '<i8'), |
| 35 | + (DimsSchema, ('foo', None), [('foo', 'bar'), ('foo', 'baz')], ['foo', None]), |
| 36 | + (DimsSchema, ('foo', 'bar'), [('foo', 'bar')], ['foo', 'bar']), |
| 37 | + (ShapeSchema, (1, 2, None), [(1, 2, 3), (1, 2, 5)], [1, 2, None]), |
| 38 | + (ShapeSchema, (1, 2, 3), [(1, 2, 3)], [1, 2, 3]), |
| 39 | + (NameSchema, 'foo', ['foo'], 'foo'), |
| 40 | + (ArrayTypeSchema, np.ndarray, [np.array([1, 2, 3])], "<class 'numpy.ndarray'>"), |
| 41 | + # schema_args for ChunksSchema include [chunks, dims, shape] |
| 42 | + (ChunksSchema, True, [(((1, 1),), ('x',), (2,))], True), |
| 43 | + (ChunksSchema, {'x': 2}, [(((2, 2),), ('x',), (4,))], {'x': 2}), |
| 44 | + (ChunksSchema, {'x': (2, 2)}, [(((2, 2),), ('x',), (4,))], {'x': [2, 2]}), |
| 45 | + (ChunksSchema, {'x': [2, 2]}, [(((2, 2),), ('x',), (4,))], {'x': [2, 2]}), |
| 46 | + (ChunksSchema, {'x': 4}, [(((4,),), ('x',), (4,))], {'x': 4}), |
| 47 | + (ChunksSchema, {'x': -1}, [(((4,),), ('x',), (4,))], {'x': -1}), |
| 48 | + (ChunksSchema, {'x': (1, 2, 1)}, [(((1, 2, 1),), ('x',), (4,))], {'x': [1, 2, 1]}), |
| 49 | + ( |
| 50 | + ChunksSchema, |
| 51 | + {'x': 2, 'y': -1}, |
| 52 | + [(((2, 2), (10,)), ('x', 'y'), (4, 10))], |
| 53 | + {'x': 2, 'y': -1}, |
| 54 | + ), |
| 55 | + ], |
| 56 | +) |
| 57 | +def test_component_schema(component, schema_args, validate, json): |
| 58 | + schema = component(schema_args) |
| 59 | + for v in validate: |
| 60 | + if component in [ChunksSchema]: # special case construction |
| 61 | + schema.validate(*v) |
| 62 | + else: |
| 63 | + schema.validate(v) |
| 64 | + assert schema.json == json |
| 65 | + assert isinstance(schema.to_json(), str) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + 'component, schema_args, validate, match', |
| 70 | + [ |
| 71 | + (DTypeSchema, np.integer, np.float32, r'.*float.*'), |
| 72 | + (DimsSchema, ('foo', 'bar'), ('foo',), r'.*length.*'), |
| 73 | + (DimsSchema, ('foo', 'bar'), ('foo', 'baz'), r'.*mismatch.*'), |
| 74 | + (ShapeSchema, (1, 2, None), (1, 2), r'.*number of dimensions.*'), |
| 75 | + (ShapeSchema, (1, 4, 4), (1, 3, 4), r'.*mismatch.*'), |
| 76 | + (NameSchema, 'foo', 'bar', r'.*name bar != foo.*'), |
| 77 | + (ArrayTypeSchema, np.ndarray, 'bar', r'.*array_type.*'), |
| 78 | + # schema_args for ChunksSchema include [chunks, dims, shape] |
| 79 | + (ChunksSchema, {'x': 3}, (((2, 2),), ('x',), (4,)), r'.*(3).*'), |
| 80 | + (ChunksSchema, {'x': (2, 1)}, (((2, 2),), ('x',), (4,)), r'.*(2, 1).*'), |
| 81 | + (ChunksSchema, True, (None, ('x',), (4,)), r'.*expected array to be chunked.*'), |
| 82 | + ( |
| 83 | + ChunksSchema, |
| 84 | + False, |
| 85 | + (((2, 2),), ('x',), (4,)), |
| 86 | + r'.*expected unchunked array but it is chunked*', |
| 87 | + ), |
| 88 | + (ChunksSchema, {'x': -1}, (((1, 2, 1),), ('x',), (4,)), r'.*did not match.*'), |
| 89 | + (ChunksSchema, {'x': 2}, (((2, 3, 2),), ('x',), (7,)), r'.*did not match.*'), |
| 90 | + (ChunksSchema, {'x': 2}, (((2, 2, 3),), ('x',), (7,)), r'.*did not match.*'), |
| 91 | + (ChunksSchema, {'x': 2, 'y': -1}, (((2, 2), (5, 5)), ('x', 'y'), (4, 10)), r'.*(5).*'), |
| 92 | + ], |
| 93 | +) |
| 94 | +def test_component_raises_schema_error(component, schema_args, validate, match): |
| 95 | + schema = component(schema_args) |
| 96 | + with pytest.raises(SchemaError, match=match): |
| 97 | + if component in [ChunksSchema]: # special case construction |
| 98 | + schema.validate(*validate) |
| 99 | + else: |
| 100 | + schema.validate(validate) |
| 101 | + |
| 102 | + |
| 103 | +def test_chunks_schema_raises_for_invalid_chunks(): |
| 104 | + with pytest.raises(ValueError, match=r'.*int.*'): |
| 105 | + schema = ChunksSchema(chunks=2) |
| 106 | + schema.validate(((2, 2),), ('x',), (4,)) |
7 | 107 |
|
8 | 108 |
|
9 | 109 | def test_dataarray_empty_constructor():
|
10 | 110 |
|
| 111 | + da = xr.DataArray(np.ones(4, dtype='i4')) |
11 | 112 | da_schema = DataArraySchema()
|
12 | 113 | assert hasattr(da_schema, 'validate')
|
| 114 | + assert da_schema.json == {} |
| 115 | + da_schema.validate(da) |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize( |
| 119 | + 'kind, component, schema_args', |
| 120 | + [ |
| 121 | + ('dtype', DTypeSchema, 'i4'), |
| 122 | + ('dims', DimsSchema, ('x', None)), |
| 123 | + ('shape', ShapeSchema, (2, None)), |
| 124 | + ('name', NameSchema, 'foo'), |
| 125 | + ('array_type', ArrayTypeSchema, np.ndarray), |
| 126 | + ('chunks', ChunksSchema, False), |
| 127 | + ], |
| 128 | +) |
| 129 | +def test_dataarray_component_constructors(kind, component, schema_args): |
| 130 | + da = xr.DataArray(np.zeros((2, 4), dtype='i4'), dims=('x', 'y'), name='foo') |
| 131 | + comp_schema = component(schema_args) |
| 132 | + schema = DataArraySchema(**{kind: schema_args}) |
| 133 | + assert comp_schema.json == getattr(schema, kind).json |
| 134 | + assert isinstance(getattr(schema, kind), component) |
13 | 135 |
|
14 |
| - |
15 |
| -def test_dataarray_validate_dtype(): |
16 |
| - |
17 |
| - da = xr.DataArray(np.ones(4, dtype='i4')) |
18 |
| - schema = DataArraySchema(dtype='i4') |
19 |
| - schema.validate(da) |
20 |
| - |
21 |
| - schema = DataArraySchema(dtype=np.int32) |
22 |
| - schema.validate(da) |
23 |
| - |
24 |
| - schema = DataArraySchema(dtype=np.integer) |
25 |
| - schema.validate(da) |
26 |
| - |
27 |
| - schema = DataArraySchema(dtype=np.floating) |
28 |
| - with pytest.raises(SchemaError, match=r'.*floating.*'): |
29 |
| - schema.validate(da) |
30 |
| - |
31 |
| - |
32 |
| -def test_dataarray_validate_name(): |
33 |
| - |
34 |
| - da = xr.DataArray(np.ones(4), name='foo') |
35 |
| - schema = DataArraySchema(name='foo') |
36 | 136 | schema.validate(da)
|
37 | 137 |
|
38 |
| - schema = DataArraySchema(name='bar') |
39 |
| - with pytest.raises(SchemaError, match=r'.*foo.*'): |
40 |
| - schema.validate(da) |
41 |
| - |
42 |
| - |
43 |
| -def test_dataarray_validate_shape(): |
44 | 138 |
|
45 |
| - da = xr.DataArray(np.ones(4)) |
46 |
| - schema = DataArraySchema(shape=(4,)) |
47 |
| - schema.validate(da) |
48 |
| - |
49 |
| - schema = DataArraySchema(shape=(4, 2)) |
50 |
| - with pytest.raises(SchemaError, match=r'.*ndim.*'): |
51 |
| - schema.validate(da) |
| 139 | +def test_dataarray_schema_validate_raises_for_invalid_input_type(): |
| 140 | + ds = xr.Dataset() |
| 141 | + schema = DataArraySchema() |
| 142 | + with pytest.raises(ValueError, match='Input must be a xarray.DataArray'): |
| 143 | + schema.validate(ds) |
52 | 144 |
|
53 |
| - schema = DataArraySchema(shape=(3,)) |
54 |
| - with pytest.raises(SchemaError, match=r'.*(4).*'): |
55 |
| - schema.validate(da) |
56 |
| - |
57 |
| - |
58 |
| -def test_dataarray_validate_dims(): |
59 |
| - |
60 |
| - da = xr.DataArray(np.ones(4), dims=['x']) |
61 |
| - schema = DataArraySchema(dims=['x']) |
62 |
| - schema.validate(da) |
63 | 145 |
|
64 |
| - schema = DataArraySchema(dims=(['x', 'y'])) |
65 |
| - with pytest.raises(SchemaError, match=r'.*length of dims.*'): |
66 |
| - schema.validate(da) |
| 146 | +def test_dataset_empty_constructor(): |
| 147 | + ds_schema = DatasetSchema() |
| 148 | + assert hasattr(ds_schema, 'validate') |
| 149 | + ds_schema.json == {} |
67 | 150 |
|
68 |
| - schema = DataArraySchema(dims=['y']) |
69 |
| - with pytest.raises(SchemaError, match=r'.*(y).*'): |
70 |
| - schema.validate(da) |
71 | 151 |
|
| 152 | +def test_dataset_example(ds): |
72 | 153 |
|
73 |
| -def test_dataarray_validate_array_type(): |
| 154 | + ds_schema = DatasetSchema( |
| 155 | + { |
| 156 | + 'foo': DataArraySchema(name='foo', dtype=np.int32, dims=['x']), |
| 157 | + 'bar': DataArraySchema(name='bar', dtype=np.floating, dims=['x', 'y']), |
| 158 | + } |
| 159 | + ) |
| 160 | + assert list(ds_schema.json['data_vars'].keys()) == ['foo', 'bar'] |
| 161 | + ds_schema.validate(ds) |
74 | 162 |
|
75 |
| - da = xr.DataArray(np.ones(4), dims=['x']) |
76 |
| - schema = DataArraySchema(array_type=np.ndarray) |
77 |
| - schema.validate(da) |
| 163 | + ds['foo'] = ds.foo.astype('float32') |
| 164 | + with pytest.raises(SchemaError, match='dtype'): |
| 165 | + ds_schema.validate(ds) |
78 | 166 |
|
79 |
| - schema = DataArraySchema(array_type=float) |
80 |
| - with pytest.raises(SchemaError, match=r'.*(float).*'): |
81 |
| - schema.validate(da) |
| 167 | + ds = ds.drop_vars('foo') |
| 168 | + with pytest.raises(SchemaError, match='variable foo'): |
| 169 | + ds_schema.validate(ds) |
82 | 170 |
|
83 | 171 |
|
84 |
| -def test_dataarray_validate_chunks(): |
85 |
| - pytest.importorskip('dask') |
| 172 | +def test_checks_ds(ds): |
| 173 | + def check_foo(ds): |
| 174 | + assert 'foo' in ds |
86 | 175 |
|
87 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': 2}) |
88 |
| - schema = DataArraySchema(chunks={'x': 2}) |
89 |
| - schema.validate(da) |
| 176 | + ds_schema = DatasetSchema(checks=[check_foo]) |
| 177 | + ds_schema.validate(ds) |
90 | 178 |
|
91 |
| - schema = DataArraySchema(chunks={'x': (2, 2)}) |
92 |
| - schema.validate(da) |
| 179 | + ds = ds.drop_vars('foo') |
| 180 | + with pytest.raises(AssertionError): |
| 181 | + ds_schema.validate(ds) |
93 | 182 |
|
94 |
| - schema = DataArraySchema(chunks={'x': [2, 2]}) |
95 |
| - schema.validate(da) |
| 183 | + ds_schema = DatasetSchema(checks=[]) |
| 184 | + ds_schema.validate(ds) |
96 | 185 |
|
97 |
| - schema = DataArraySchema(chunks={'x': 3}) |
98 |
| - with pytest.raises(SchemaError, match=r'.*(3).*'): |
99 |
| - schema.validate(da) |
| 186 | + # TODO |
| 187 | + # with pytest.raises(ValueError): |
| 188 | + # DatasetSchema(checks=[2]) |
100 | 189 |
|
101 |
| - schema = DataArraySchema(chunks={'x': (2, 1)}) |
102 |
| - with pytest.raises(SchemaError, match=r'.*(2, 1).*'): |
103 |
| - schema.validate(da) |
104 | 190 |
|
105 |
| - # check that when expected chunk == -1 it fails |
106 |
| - schema = DataArraySchema(chunks={'x': -1}) |
107 |
| - with pytest.raises(SchemaError, match=r'.*(4).*'): |
108 |
| - schema.validate(da) |
| 191 | +def test_checks_da(ds): |
| 192 | + da = ds['foo'] |
109 | 193 |
|
110 |
| - # check that when chunking schema is -1 it also works |
111 |
| - # both when chunking is specified as -1 and as 4 |
112 |
| - schema = DataArraySchema(chunks={'x': 4}) |
113 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': -1}) |
114 |
| - schema.validate(da) |
| 194 | + def check_foo(da): |
| 195 | + assert da.name == 'foo' |
115 | 196 |
|
116 |
| - schema = DataArraySchema(chunks={'x': -1}) |
117 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': 4}) |
118 |
| - schema.validate(da) |
| 197 | + def check_bar(da): |
| 198 | + assert da.name == 'bar' |
119 | 199 |
|
120 |
| - schema = DataArraySchema(chunks={'x': -1}) |
121 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': -1}) |
| 200 | + schema = DataArraySchema(checks=[check_foo]) |
122 | 201 | schema.validate(da)
|
123 | 202 |
|
124 |
| - # test for agnostic chunks |
125 |
| - schema = DataArraySchema(chunks=True) |
126 |
| - da = xr.DataArray(np.ones(4), dims=['x']) |
127 |
| - with pytest.raises(SchemaError, match='Schema expected DataArray to be chunked but it is not'): |
| 203 | + schema = DataArraySchema(checks=[check_bar]) |
| 204 | + with pytest.raises(AssertionError): |
128 | 205 | schema.validate(da)
|
129 | 206 |
|
130 |
| - # now try passing an irregularly chunked data array |
131 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': (1, 2, 1)}) |
| 207 | + schema = DataArraySchema(checks=[]) |
132 | 208 | schema.validate(da)
|
133 | 209 |
|
134 |
| - # test the check for regular chunk sizes |
135 |
| - schema = DataArraySchema(chunks={'x': -1}) |
136 |
| - with pytest.raises(AssertionError, match=r'.*(gracious).*'): |
137 |
| - schema.validate(da) |
138 |
| - |
139 |
| - |
140 |
| -def test_dataset_empty_constructor(): |
141 |
| - ds_schema = DatasetSchema() |
142 |
| - assert hasattr(ds_schema, 'validate') |
143 |
| - |
144 |
| - |
145 |
| -def test_dataset_example(): |
146 |
| - |
147 |
| - ds = xr.Dataset( |
148 |
| - { |
149 |
| - 'x': xr.DataArray(np.arange(4) - 2, dims='x'), |
150 |
| - 'foo': xr.DataArray(np.ones(4, dtype='i4'), dims='x'), |
151 |
| - 'bar': xr.DataArray(np.arange(8, dtype=np.float32).reshape(4, 2), dims=('x', 'y')), |
152 |
| - } |
153 |
| - ) |
154 |
| - |
155 |
| - ds_schema = DatasetSchema( |
156 |
| - { |
157 |
| - 'foo': DataArraySchema(name='foo', dtype=np.int32, dims=['x']), |
158 |
| - 'bar': DataArraySchema(name='bar', dtype=np.floating, dims=['x', 'y']), |
159 |
| - } |
160 |
| - ) |
161 |
| - ds_schema.validate(ds) |
162 |
| - |
163 |
| - |
164 |
| -def test_validate(): |
165 |
| - da = xr.DataArray(np.ones(4), dims=['x']).chunk({'x': (1, 2, 1)}) |
166 |
| - schema = DataArraySchema(chunks=False) |
167 |
| - # check that da is unchunked |
168 |
| - with pytest.raises(SchemaError, match='Schema expected unchunked DataArray but it is chunked!'): |
169 |
| - schema.validate(da) |
170 |
| - da = xr.DataArray(np.ones(4), dims=['x']) |
171 |
| - schema.validate(da) |
| 210 | + with pytest.raises(ValueError): |
| 211 | + DataArraySchema(checks=[2]) |
0 commit comments