66import unittest
77
88import numpy as np
9+ import pytest
910import xarray as xr
1011
1112from zappend .api import FileObj
@@ -82,7 +83,7 @@ def test_some_slices_with_inc_append_step(self):
8283 make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
8384 make_test_dataset (index = 2 , shape = (1 , 50 , 100 )),
8485 ]
85- zappend (slices , target_dir = target_dir , append_step = "1d " )
86+ zappend (slices , target_dir = target_dir , append_step = "1D " )
8687 ds = xr .open_zarr (target_dir )
8788 np .testing .assert_array_equal (
8889 ds .time .values ,
@@ -96,57 +97,118 @@ def test_some_slices_with_dec_append_step(self):
9697 make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
9798 make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
9899 ]
99- zappend (slices , target_dir = target_dir , append_step = "-1d " )
100+ zappend (slices , target_dir = target_dir , append_step = "-1D " )
100101 ds = xr .open_zarr (target_dir )
101102 np .testing .assert_array_equal (
102103 ds .time .values ,
103104 np .array (["2024-01-03" , "2024-01-02" , "2024-01-01" ], dtype = np .datetime64 ),
104105 )
105106
106- def test_some_slices_with_one_missing_append_step (self ):
107+ # # See https://github.com/bcdev/zappend/issues/21
108+ #
109+ # def test_some_slices_with_one_missing_append_step(self):
110+ # target_dir = "memory://target.zarr"
111+ # slices = [
112+ # make_test_dataset(index=0, shape=(1, 50, 100)),
113+ # make_test_dataset(index=2, shape=(1, 50, 100)),
114+ # ]
115+ # zappend(slices, target_dir=target_dir, append_step="1D")
116+ # ds = xr.open_zarr(target_dir)
117+ # np.testing.assert_array_equal(
118+ # ds.time.values,
119+ # np.array(
120+ # ["2024-01-01", "2024-01-02", "2024-01-03"], dtype="datetime64[ns]"
121+ # ),
122+ # )
123+
124+ # # See https://github.com/bcdev/zappend/issues/21
125+ #
126+ # def test_some_slices_with_three_missing_append_steps(self):
127+ # target_dir = "memory://target.zarr"
128+ # slices = [
129+ # make_test_dataset(index=0, shape=(1, 50, 100)),
130+ # make_test_dataset(index=4, shape=(1, 50, 100)),
131+ # ]
132+ # zappend(slices, target_dir=target_dir, append_step="1D")
133+ # ds = xr.open_zarr(target_dir)
134+ # np.testing.assert_array_equal(
135+ # ds.time.values,
136+ # np.array(
137+ # [
138+ # "2024-01-01",
139+ # "2024-01-02",
140+ # "2024-01-03",
141+ # "2024-01-04",
142+ # "2024-01-05",
143+ # ],
144+ # dtype="datetime64[ns]",
145+ # ),
146+ # )
147+
148+ def test_it_raises_for_wrong_append_step (self ):
149+ target_dir = "memory://target.zarr"
150+ slices = [
151+ make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
152+ make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
153+ ]
154+ with pytest .raises (
155+ ValueError ,
156+ match = (
157+ "Cannot append slice because this would result in"
158+ " an invalid step size."
159+ ),
160+ ):
161+ zappend (slices , target_dir = target_dir , append_step = "2D" )
162+
163+ def test_some_slices_with_inc_append_labels (self ):
164+ append_step = "+"
165+
107166 target_dir = "memory://target.zarr"
108167 slices = [
109168 make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
169+ make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
110170 make_test_dataset (index = 2 , shape = (1 , 50 , 100 )),
111171 ]
112- zappend (slices , target_dir = target_dir , append_step = "1d" )
113- ds = xr .open_zarr (target_dir )
114- np .testing .assert_array_equal (
115- ds .time .values ,
116- np .array (["2024-01-01" , "2024-01-02" , "2024-01-03" ], dtype = np .datetime64 ),
117- )
172+ # OK!
173+ zappend (slices , target_dir = target_dir , append_step = append_step )
118174
119- def test_some_slices_with_three_missing_append_steps (self ):
120175 target_dir = "memory://target.zarr"
121176 slices = [
177+ make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
122178 make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
123- make_test_dataset (index = 4 , shape = (1 , 50 , 100 )),
124179 ]
125- zappend (slices , target_dir = target_dir , append_step = "1d" )
126- ds = xr .open_zarr (target_dir )
127- np .testing .assert_array_equal (
128- ds .time .values ,
129- np .array (
130- ["2024-01-01" , "2024-01-02" , "2024-01-03" , "2024-01-04" , "2024-01-05" ],
131- dtype = np .datetime64 ,
180+ with pytest .raises (
181+ ValueError ,
182+ match = (
183+ "Cannot append slice because labels must be monotonically increasing"
132184 ),
133- )
134-
135- def test_it_raises_for_wrong_append_step (self ):
136- # TODO: implement me
137- pass
138-
139- def test_some_slices_with_inc_append_labels (self ):
140- # TODO: implement me
141- pass
185+ ):
186+ zappend (slices , target_dir = target_dir , append_step = append_step )
142187
143188 def test_some_slices_with_dec_append_labels (self ):
144- # TODO: implement me
145- pass
189+ append_step = "-"
146190
147- def test_it_raises_for_none_inc_append_labels (self ):
148- # TODO: implement me
149- pass
191+ target_dir = "memory://target.zarr"
192+ slices = [
193+ make_test_dataset (index = 2 , shape = (1 , 50 , 100 )),
194+ make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
195+ make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
196+ ]
197+ # OK!
198+ zappend (slices , target_dir = target_dir , append_step = append_step )
199+
200+ target_dir = "memory://target.zarr"
201+ slices = [
202+ make_test_dataset (index = 0 , shape = (1 , 50 , 100 )),
203+ make_test_dataset (index = 1 , shape = (1 , 50 , 100 )),
204+ ]
205+ with pytest .raises (
206+ ValueError ,
207+ match = (
208+ "Cannot append slice because labels must be monotonically decreasing"
209+ ),
210+ ):
211+ zappend (slices , target_dir = target_dir , append_step = append_step )
150212
151213 def test_some_slices_with_profiling (self ):
152214 target_dir = "memory://target.zarr"
0 commit comments