| 3571 | assert_identical(actual, only_new_data) |
| 3572 | |
| 3573 | def test_write_region_errors(self) -> None: |
| 3574 | data = Dataset({"u": (("x",), np.arange(5))}) |
| 3575 | data2 = Dataset({"u": (("x",), np.array([10, 11]))}) |
| 3576 | |
| 3577 | @contextlib.contextmanager |
| 3578 | def setup_and_verify_store(expected=data): |
| 3579 | with self.create_zarr_target() as store: |
| 3580 | data.to_zarr(store, **self.version_kwargs) |
| 3581 | yield store |
| 3582 | with self.open(store) as actual: |
| 3583 | assert_identical(actual, expected) |
| 3584 | |
| 3585 | # verify the base case works |
| 3586 | expected = Dataset({"u": (("x",), np.array([10, 11, 2, 3, 4]))}) |
| 3587 | with setup_and_verify_store(expected) as store: |
| 3588 | data2.to_zarr(store, region={"x": slice(2)}, **self.version_kwargs) |
| 3589 | |
| 3590 | with setup_and_verify_store() as store: |
| 3591 | with pytest.raises( |
| 3592 | ValueError, |
| 3593 | match=re.escape( |
| 3594 | "cannot set region unless mode='a', mode='a-', mode='r+' or mode=None" |
| 3595 | ), |
| 3596 | ): |
| 3597 | data.to_zarr( |
| 3598 | store, region={"x": slice(None)}, mode="w", **self.version_kwargs |
| 3599 | ) |
| 3600 | |
| 3601 | with setup_and_verify_store() as store: |
| 3602 | with pytest.raises(TypeError, match=r"must be a dict"): |
| 3603 | data.to_zarr(store, region=slice(None), **self.version_kwargs) # type: ignore[call-overload] |
| 3604 | |
| 3605 | with setup_and_verify_store() as store: |
| 3606 | with pytest.raises(TypeError, match=r"must be slice objects"): |
| 3607 | data2.to_zarr(store, region={"x": [0, 1]}, **self.version_kwargs) # type: ignore[dict-item] |
| 3608 | |
| 3609 | with setup_and_verify_store() as store: |
| 3610 | with pytest.raises(ValueError, match=r"step on all slices"): |
| 3611 | data2.to_zarr( |
| 3612 | store, region={"x": slice(None, None, 2)}, **self.version_kwargs |
| 3613 | ) |
| 3614 | |
| 3615 | with setup_and_verify_store() as store: |
| 3616 | with pytest.raises( |
| 3617 | ValueError, |
| 3618 | match=r"all keys in ``region`` are not in Dataset dimensions", |
| 3619 | ): |
| 3620 | data.to_zarr(store, region={"y": slice(None)}, **self.version_kwargs) |
| 3621 | |
| 3622 | with setup_and_verify_store() as store: |
| 3623 | with pytest.raises( |
| 3624 | ValueError, |
| 3625 | match=r"all variables in the dataset to write must have at least one dimension in common", |
| 3626 | ): |
| 3627 | data2.assign(v=2).to_zarr( |
| 3628 | store, region={"x": slice(2)}, **self.version_kwargs |
| 3629 | ) |
| 3630 | |