|
| 1 | +import sys |
| 2 | + |
| 3 | +import cubed |
| 4 | +import pytest |
1 | 5 | import xarray as xr
|
| 6 | +from cubed.runtime.create import create_executor |
2 | 7 | from xarray.namedarray.parallelcompat import list_chunkmanagers
|
3 |
| -import cubed |
| 8 | +from xarray.tests import assert_allclose, create_test_data |
4 | 9 |
|
5 | 10 | from cubed_xarray.cubedmanager import CubedManager
|
6 | 11 |
|
| 12 | +EXECUTORS = [create_executor("single-threaded")] |
| 13 | + |
| 14 | +if sys.version_info >= (3, 11): |
| 15 | + EXECUTORS.append(create_executor("processes")) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture( |
| 19 | + scope="module", |
| 20 | + params=EXECUTORS, |
| 21 | + ids=[executor.name for executor in EXECUTORS], |
| 22 | +) |
| 23 | +def executor(request): |
| 24 | + return request.param |
| 25 | + |
7 | 26 |
|
8 | 27 | class TestDiscoverCubedManager:
|
9 | 28 | def test_list_cubedmanager(self):
|
10 | 29 | chunkmanagers = list_chunkmanagers()
|
11 |
| - assert 'cubed' in chunkmanagers |
12 |
| - assert isinstance(chunkmanagers['cubed'], CubedManager) |
| 30 | + assert "cubed" in chunkmanagers |
| 31 | + assert isinstance(chunkmanagers["cubed"], CubedManager) |
13 | 32 |
|
14 | 33 | def test_chunk(self):
|
15 |
| - da = xr.DataArray([1, 2], dims='x') |
16 |
| - chunked = da.chunk(x=1, chunked_array_type='cubed') |
| 34 | + da = xr.DataArray([1, 2], dims="x") |
| 35 | + chunked = da.chunk(x=1, chunked_array_type="cubed") |
17 | 36 | assert isinstance(chunked.data, cubed.Array)
|
18 |
| - assert chunked.chunksizes == {'x': (1, 1)} |
| 37 | + assert chunked.chunksizes == {"x": (1, 1)} |
19 | 38 |
|
20 | 39 | # TODO test cubed is default when dask not installed
|
21 | 40 |
|
22 | 41 | # TODO test dask is default over cubed when both installed
|
| 42 | + |
| 43 | + |
| 44 | +def test_to_zarr(tmpdir, executor): |
| 45 | + spec = cubed.Spec(allowed_mem="200MB", executor=executor) |
| 46 | + |
| 47 | + original = create_test_data().chunk( |
| 48 | + chunked_array_type="cubed", from_array_kwargs={"spec": spec} |
| 49 | + ) |
| 50 | + |
| 51 | + filename = tmpdir / "out.zarr" |
| 52 | + original.to_zarr(filename) |
| 53 | + |
| 54 | + with xr.open_dataset( |
| 55 | + filename, |
| 56 | + chunks="auto", |
| 57 | + engine="zarr", |
| 58 | + chunked_array_type="cubed", |
| 59 | + from_array_kwargs={"spec": spec}, |
| 60 | + ) as restored: |
| 61 | + assert isinstance(restored.var1.data, cubed.Array) |
| 62 | + computed = restored.compute() |
| 63 | + assert_allclose(original, computed) |
0 commit comments