|
| 1 | +# Copyright © 2024 Norman Fomferra |
| 2 | +# Permissions are hereby granted under the terms of the MIT License: |
| 3 | +# https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import shutil |
| 6 | +import unittest |
| 7 | +import warnings |
| 8 | + |
| 9 | +import pytest |
| 10 | +import xarray as xr |
| 11 | + |
| 12 | +from zappend.context import Context |
| 13 | +from zappend.fsutil.fileobj import FileObj |
| 14 | +from zappend.slice.cm import SliceSourceContextManager |
| 15 | +from zappend.slice.cm import open_slice_dataset |
| 16 | +from zappend.slice.sources.memory import MemorySliceSource |
| 17 | +from zappend.slice.sources.persistent import PersistentSliceSource |
| 18 | +from zappend.slice.sources.temporary import TemporarySliceSource |
| 19 | +from tests.helpers import clear_memory_fs |
| 20 | +from tests.helpers import make_test_dataset |
| 21 | + |
| 22 | + |
| 23 | +# noinspection PyUnusedLocal |
| 24 | + |
| 25 | + |
| 26 | +# noinspection PyShadowingBuiltins |
| 27 | +class OpenSliceDatasetTest(unittest.TestCase): |
| 28 | + def setUp(self): |
| 29 | + clear_memory_fs() |
| 30 | + |
| 31 | + # noinspection PyMethodMayBeStatic |
| 32 | + def test_slice_item_is_slice_source(self): |
| 33 | + dataset = make_test_dataset() |
| 34 | + ctx = Context(dict(target_dir="memory://target.zarr")) |
| 35 | + slice_item = MemorySliceSource(dataset, 0) |
| 36 | + slice_cm = open_slice_dataset(ctx, slice_item) |
| 37 | + self.assertIs(slice_item, slice_cm.slice_source) |
| 38 | + |
| 39 | + def test_slice_item_is_dataset(self): |
| 40 | + dataset = make_test_dataset() |
| 41 | + ctx = Context(dict(target_dir="memory://target.zarr")) |
| 42 | + slice_cm = open_slice_dataset(ctx, dataset) |
| 43 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 44 | + self.assertIsInstance(slice_cm.slice_source, MemorySliceSource) |
| 45 | + with slice_cm as slice_ds: |
| 46 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 47 | + |
| 48 | + def test_slice_item_is_persisted_dataset(self): |
| 49 | + dataset = make_test_dataset() |
| 50 | + ctx = Context(dict(target_dir="memory://target.zarr", persist_mem_slices=True)) |
| 51 | + slice_cm = open_slice_dataset(ctx, dataset) |
| 52 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 53 | + self.assertIsInstance(slice_cm.slice_source, TemporarySliceSource) |
| 54 | + with slice_cm as slice_ds: |
| 55 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 56 | + |
| 57 | + def test_slice_item_is_file_obj(self): |
| 58 | + slice_dir = FileObj("memory://slice.zarr") |
| 59 | + make_test_dataset(uri=slice_dir.uri) |
| 60 | + ctx = Context(dict(target_dir="memory://target.zarr")) |
| 61 | + slice_cm = open_slice_dataset(ctx, slice_dir) |
| 62 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 63 | + with slice_cm as slice_ds: |
| 64 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 65 | + |
| 66 | + def test_slice_item_is_memory_uri(self): |
| 67 | + slice_dir = FileObj("memory://slice.zarr") |
| 68 | + make_test_dataset(uri=slice_dir.uri) |
| 69 | + ctx = Context(dict(target_dir="memory://target.zarr")) |
| 70 | + slice_cm = open_slice_dataset(ctx, slice_dir.uri) |
| 71 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 72 | + with slice_cm as slice_ds: |
| 73 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 74 | + |
| 75 | + def test_slice_item_is_uri_of_memory_nc(self): |
| 76 | + engine = "scipy" |
| 77 | + format = "NETCDF3_CLASSIC" |
| 78 | + slice_ds = make_test_dataset() |
| 79 | + slice_file = FileObj("memory:///slice.nc") |
| 80 | + with slice_file.fs.open(slice_file.path, "wb") as stream: |
| 81 | + # noinspection PyTypeChecker |
| 82 | + slice_ds.to_netcdf(stream, engine=engine, format=format) |
| 83 | + ctx = Context(dict(target_dir="memory://target.zarr", slice_engine=engine)) |
| 84 | + slice_cm = open_slice_dataset(ctx, slice_file.uri) |
| 85 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 86 | + self.assertIsInstance(slice_cm.slice_source, PersistentSliceSource) |
| 87 | + try: |
| 88 | + with slice_cm as slice_ds: |
| 89 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 90 | + except KeyError as e: |
| 91 | + # This is unexpected! xarray cannot open the NetCDF file it just |
| 92 | + # created. Maybe report a xarray issue once we can isolate the |
| 93 | + # root cause. But it may be related to just the memory FS. |
| 94 | + warnings.warn(f"received known exception from to_netcdf(): {e}") |
| 95 | + |
| 96 | + def test_slice_item_is_uri_of_local_fs_nc(self): |
| 97 | + engine = "h5netcdf" |
| 98 | + format = "NETCDF4" |
| 99 | + target_dir = FileObj("./target.zarr") |
| 100 | + ctx = Context(dict(target_dir=target_dir.path, slice_engine=engine)) |
| 101 | + slice_ds = make_test_dataset() |
| 102 | + slice_file = FileObj("./slice.nc") |
| 103 | + # noinspection PyTypeChecker |
| 104 | + slice_ds.to_netcdf(slice_file.path, engine=engine, format=format) |
| 105 | + try: |
| 106 | + slice_cm = open_slice_dataset(ctx, slice_file.uri) |
| 107 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 108 | + self.assertIsInstance(slice_cm.slice_source, PersistentSliceSource) |
| 109 | + with slice_cm as slice_ds: |
| 110 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 111 | + finally: |
| 112 | + shutil.rmtree(target_dir.path, ignore_errors=True) |
| 113 | + slice_file.delete() |
| 114 | + |
| 115 | + def test_slice_item_is_uri_with_polling_ok(self): |
| 116 | + slice_dir = FileObj("memory://slice.zarr") |
| 117 | + make_test_dataset(uri=slice_dir.uri) |
| 118 | + ctx = Context( |
| 119 | + dict( |
| 120 | + target_dir="memory://target.zarr", |
| 121 | + slice_polling=dict(timeout=0.1, interval=0.02), |
| 122 | + ) |
| 123 | + ) |
| 124 | + slice_cm = open_slice_dataset(ctx, slice_dir.uri) |
| 125 | + self.assertIsInstance(slice_cm, SliceSourceContextManager) |
| 126 | + self.assertIsInstance(slice_cm.slice_source, PersistentSliceSource) |
| 127 | + with slice_cm as slice_ds: |
| 128 | + self.assertIsInstance(slice_ds, xr.Dataset) |
| 129 | + |
| 130 | + # noinspection PyMethodMayBeStatic |
| 131 | + def test_slice_item_is_uri_with_polling_fail(self): |
| 132 | + slice_dir = FileObj("memory://slice.zarr") |
| 133 | + ctx = Context( |
| 134 | + dict( |
| 135 | + target_dir="memory://target.zarr", |
| 136 | + slice_polling=dict(timeout=0.1, interval=0.02), |
| 137 | + ) |
| 138 | + ) |
| 139 | + slice_cm = open_slice_dataset(ctx, slice_dir.uri) |
| 140 | + with pytest.raises(FileNotFoundError, match=slice_dir.uri): |
| 141 | + with slice_cm: |
| 142 | + pass |
0 commit comments