diff --git a/virtualizarr/tests/test_readers/test_kerchunk.py b/virtualizarr/tests/test_readers/test_kerchunk.py index f693b370..dab049cb 100644 --- a/virtualizarr/tests/test_readers/test_kerchunk.py +++ b/virtualizarr/tests/test_readers/test_kerchunk.py @@ -1,51 +1,88 @@ +from pathlib import Path +from typing import Any, Callable, Generator, Optional + import numpy as np +import pytest import ujson +from virtualizarr.backend import open_virtual_dataset from virtualizarr.manifests import ManifestArray -from virtualizarr.readers.kerchunk import ( - dataset_from_kerchunk_refs, -) def gen_ds_refs( - zgroup: str = '{"zarr_format":2}', - zarray: str = '{"chunks":[2,3],"compressor":null,"dtype":" Generator[ + Callable[[Optional[Any], Optional[Any], Optional[Any], Optional[Any]], str], + None, + None, +]: + """ + Fixture which defers creation of the references file until the parameters zgroup etc. are known. + """ + + def _refs_file(zgroup=None, zarray=None, zattrs=None, chunks=None) -> str: + refs = gen_ds_refs(zgroup=zgroup, zarray=zarray, zattrs=zattrs, chunks=chunks) + filepath = tmp_path / "refs.json" + + with open(filepath, "w") as json_file: + ujson.dump(refs, json_file) + + return str(filepath) + + yield _refs_file + - assert da.data.zarray.compressor is None - assert da.data.zarray.filters is None - assert da.data.zarray.fill_value == 0 - assert da.data.zarray.order == "C" +def test_dataset_from_df_refs(refs_file_factory): + refs_file = refs_file_factory() - assert da.data.manifest.dict() == { + vds = open_virtual_dataset(refs_file, filetype="kerchunk") + + assert "a" in vds + vda = vds["a"] + assert isinstance(vda.data, ManifestArray) + assert vda.dims == ("x", "y") + assert vda.shape == (2, 3) + assert vda.chunks == (2, 3) + assert vda.dtype == np.dtype("