Skip to content

Commit

Permalink
minimalist zarr test
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Durant committed Sep 7, 2017
1 parent f305c25 commit 6c1fb6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 6 additions & 1 deletion xarray/backends/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ZarrStore(AbstractWritableDataStore, DataStorePickleMixin):

# need some special secret attributes to tell us the dimensions
_dimension_key = '_XARRAY_DIMENSIONS'
default_arguments = dict(compressor='default', order='C', synchronizer=None,
filters=None, cache_metadata=True, kwargs={})

def __init__(self, store=None, overwrite=False, chunk_store=None,
synchronizer=None, path=None, writer=None, autoclose=False):
Expand Down Expand Up @@ -158,8 +160,11 @@ def prepare_variable(self, name, variable, check_encoding=False,

# TODO: figure out how to pass along all those other arguments

kwargs = self.default_arguments.copy()
kwargs.update(kwargs.pop('kwargs'))
zarr_array = self.ds.create(name, shape=shape, dtype=dtype,
chunks=chunks, fill_value=fill_value)
chunks=chunks, fill_value=fill_value,
**kwargs)
zarr_array.attrs[self._dimension_key] = dims
_, attributes = _get_zarr_dims_and_attrs(zarr_array,
self._dimension_key)
Expand Down
9 changes: 8 additions & 1 deletion xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
except ImportError:
has_rasterio = False

try:
import zarr
has_zarr = True
except ImportError:
has_zarr = False

# slighly simpler construction that the full functions.
# Generally `pytest.importorskip('package')` inline is even easier
requires_matplotlib = pytest.mark.skipif(
Expand All @@ -105,7 +111,8 @@
not has_bottleneck, reason='requires bottleneck')
requires_rasterio = pytest.mark.skipif(
not has_rasterio, reason='requires rasterio')

requires_zarr = pytest.mark.skipif(
not has_zarr, reason='requires zarr')

try:
_SKIP_FLAKY = not pytest.config.getoption("--run-flaky")
Expand Down
19 changes: 18 additions & 1 deletion xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
open_mfdataset, backends, save_mfdataset)
from xarray.backends.common import robust_getitem
from xarray.backends.netCDF4_ import _extract_nc4_variable_encoding
from xarray.backends.zarr import ZarrStore
from xarray.core import indexing
from xarray.core.pycompat import iteritems, PY2, ExitStack, basestring

from . import (TestCase, requires_scipy, requires_netCDF4, requires_pydap,
requires_scipy_or_netCDF4, requires_dask, requires_h5netcdf,
requires_pynio, has_netCDF4, has_scipy, assert_allclose,
flaky, network, requires_rasterio, assert_identical)
flaky, network, requires_rasterio, assert_identical,
requires_zarr)
from .test_dataset import create_test_data

try:
Expand Down Expand Up @@ -1934,3 +1936,18 @@ def test_open_dataarray_options(self):
expected = data.drop('y')
with open_dataarray(tmp, drop_variables=['y']) as loaded:
self.assertDataArrayIdentical(expected, loaded)


@requires_zarr
class TestZarr(TestCase):

def test_roundtrip(self):
data = create_test_data()
del data.coords['time']
with create_tmp_file() as tmp:
z = ZarrStore(store=tmp, overwrite=True)
z.store_dataset(data)

z2 = open_dataset(ZarrStore(store=tmp))
for k in data:

This comment has been minimized.

Copy link
@shoyer

shoyer Sep 7, 2017

Use xarray.testing.assert_identical(data, z2) here

This comment has been minimized.

Copy link
@martindurant

martindurant Sep 7, 2017

Owner

It doesn't work yet :) I would have assumed self.assertDatasetEqual.

This comment has been minimized.

Copy link
@shoyer

shoyer Sep 7, 2017

That's an alias for assert_equal()

self.assertTrue((data[k].values == z2[k].values).all())

0 comments on commit 6c1fb6b

Please sign in to comment.