|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import xarray as xr |
| 4 | + |
1 | 5 | from xarray_schema import DataArraySchema, DatasetSchema
|
| 6 | +from xarray_schema.core import SchemaError |
2 | 7 |
|
3 | 8 |
|
4 |
| -def test_empyt_constructors(): |
| 9 | +def test_dataarray_empty_constructor(): |
5 | 10 |
|
6 | 11 | da_schema = DataArraySchema()
|
7 | 12 | assert hasattr(da_schema, 'validate')
|
| 13 | + |
| 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 | + schema.validate(da) |
| 37 | + |
| 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 | + |
| 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) |
| 52 | + |
| 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 | + |
| 64 | + schema = DataArraySchema(dims=(['x', 'y'])) |
| 65 | + with pytest.raises(SchemaError, match=r'.*length of dims.*'): |
| 66 | + schema.validate(da) |
| 67 | + |
| 68 | + schema = DataArraySchema(dims=['y']) |
| 69 | + with pytest.raises(SchemaError, match=r'.*(y).*'): |
| 70 | + schema.validate(da) |
| 71 | + |
| 72 | + |
| 73 | +def test_dataset_empty_constructor(): |
8 | 74 | ds_schema = DatasetSchema()
|
9 | 75 | assert hasattr(ds_schema, 'validate')
|
| 76 | + |
| 77 | + |
| 78 | +def test_dataset_example(): |
| 79 | + |
| 80 | + ds = xr.Dataset( |
| 81 | + { |
| 82 | + 'x': xr.DataArray(np.arange(4) - 2, dims='x'), |
| 83 | + 'foo': xr.DataArray(np.ones(4, dtype='i4'), dims='x'), |
| 84 | + 'bar': xr.DataArray(np.arange(8, dtype=np.float32).reshape(4, 2), dims=('x', 'y')), |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + ds_schema = DatasetSchema( |
| 89 | + { |
| 90 | + 'foo': DataArraySchema(name='foo', dtype=np.int32, dims=['x']), |
| 91 | + 'bar': DataArraySchema(name='bar', dtype=np.floating, dims=['x', 'y']), |
| 92 | + } |
| 93 | + ) |
| 94 | + ds_schema.validate(ds) |
0 commit comments