Skip to content

Implement coords validation #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,29 +222,42 @@ def test_dataset_empty_constructor():

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)

ds['foo'] = ds.foo.astype('float32')
ds2 = ds.copy()
ds2['foo'] = ds2.foo.astype('float32')
with pytest.raises(SchemaError, match='dtype'):
ds_schema.validate(ds)
ds_schema.validate(ds2)

ds = ds.drop_vars('foo')
ds2 = ds2.drop_vars('foo')
with pytest.raises(SchemaError, match='variable foo'):
ds_schema.validate(ds)
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)
rt_schema.json == ds_schema.json
assert rt_schema.json == ds_schema.json


def test_checks_ds(ds):
Expand Down
8 changes: 4 additions & 4 deletions xarray_schema/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def from_json(cls, obj: dict):
k: DataArraySchema.from_json(v) for k, v in obj['data_vars'].items()
}
if 'coords' in obj:
kwargs['coords'] = {k: CoordsSchema.from_json(v) for k, v in obj['coords'].items()}
kwargs['coords'] = CoordsSchema.from_json(obj['coords'])
if 'attrs' in obj:
kwargs['attrs'] = {k: AttrsSchema.from_json(v) for k, v in obj['attrs'].items()}
kwargs['attrs'] = AttrsSchema.from_json(obj['attrs'])

return cls(**kwargs)

Expand Down Expand Up @@ -79,8 +79,8 @@ def validate(self, ds: xr.Dataset) -> None:
else:
da_schema.validate(ds.data_vars[key])

if self.coords is not None: # pragma: no cover
raise NotImplementedError('coords schema not implemented yet')
if self.coords is not None:
self.coords.validate(ds.coords)

if self.attrs:
self.attrs.validate(ds.attrs)
Expand Down