99import xarray as xr
1010
1111from zappend .fsutil import FileObj
12- from zappend .levels import write_levels
13- from zappend .levels import get_variables_config
14- from .helpers import clear_memory_fs
15- from .helpers import make_test_dataset
12+ from zappend .contrib import write_levels
13+ from zappend .contrib . levels import get_variables_config
14+ from tests .helpers import clear_memory_fs
15+ from tests .helpers import make_test_dataset
1616
1717try :
1818 # noinspection PyUnresolvedReferences
@@ -71,29 +71,30 @@ def test_variables_given(self):
7171 )
7272
7373
74+ source_path = "memory://source.zarr"
75+ target_path = "memory://target.levels"
76+
77+
78+ # noinspection PyMethodMayBeStatic
7479@unittest .skipIf (xcube is None , reason = "xcube is not installed" )
75- class WriteLevelsTest (unittest .TestCase ):
80+ class WriteLevelsArgsTest (unittest .TestCase ):
7681 def setUp (self ):
7782 clear_memory_fs ()
78-
79- # noinspection PyMethodMayBeStatic
80- def test_args_validation (self ):
81- source_path = "memory://source.zarr"
82- target_path = "memory://target.levels"
83-
8483 make_test_dataset (
8584 uri = source_path ,
8685 dims = ("time" , "lat" , "lon" ),
8786 shape = (3 , 1024 , 2048 ),
8887 chunks = (1 , 128 , 256 ),
8988 )
9089
90+ def test_target_path_not_given (self ):
9191 with pytest .raises (
9292 ValueError ,
9393 match = "missing 'target_path' argument" ,
9494 ):
9595 write_levels (source_path = source_path )
9696
97+ def test_target_dir_and_target_path_given (self ):
9798 with pytest .raises (
9899 ValueError ,
99100 match = "either 'target_dir' or 'target_path' can be given, not both" ,
@@ -104,6 +105,7 @@ def test_args_validation(self):
104105 target_dir = target_path ,
105106 )
106107
108+ def test_config_given (self ):
107109 with pytest .raises (
108110 TypeError ,
109111 match = "write_levels\\ (\\ ) got an unexpected keyword argument 'config'" ,
@@ -114,6 +116,7 @@ def test_args_validation(self):
114116 config = {"dry_run" : True },
115117 )
116118
119+ def test_dry_run_and_use_saved_levels_given (self ):
117120 with pytest .raises (
118121 FileNotFoundError ,
119122 match = "Target parent directory does not exist: /target.levels" ,
@@ -129,8 +132,60 @@ def test_args_validation(self):
129132 use_saved_levels = True ,
130133 )
131134
135+ def test_source_path_and_source_ds_not_given (self ):
136+ with pytest .raises (
137+ TypeError ,
138+ match = "'source_ds' argument must be of type 'xarray.Dataset',"
139+ " but was 'NoneType'" ,
140+ ):
141+ write_levels (
142+ target_path = target_path ,
143+ )
144+
145+ def test_source_path_not_given_and_link_level_zero_is_true (self ):
146+ with pytest .raises (
147+ ValueError ,
148+ match = "'source_path' argument must be provided"
149+ " if 'link_level_zero' is used" ,
150+ ):
151+ write_levels (
152+ source_ds = xr .Dataset (), target_path = target_path , link_level_zero = True
153+ )
154+
155+ def test_source_append_offset_not_int (self ):
156+ with pytest .raises (
157+ TypeError ,
158+ match = "'source_append_offset' argument must be of type 'int',"
159+ " but was 'str'" ,
160+ ):
161+ # noinspection PyTypeChecker
162+ write_levels (
163+ source_ds = xr .open_zarr ("memory://source.zarr" ),
164+ source_append_offset = "2" ,
165+ target_path = target_path ,
166+ )
167+
168+ def test_source_append_offset_out_of_range (self ):
169+ with pytest .raises (
170+ ValueError ,
171+ match = "'source_append_offset' argument must be >=0 and <3, but was 13" ,
172+ ):
173+ # noinspection PyTypeChecker
174+ write_levels (
175+ source_ds = xr .open_zarr ("memory://source.zarr" ),
176+ source_append_offset = 13 ,
177+ target_path = target_path ,
178+ )
179+
180+
181+ @unittest .skipIf (xcube is None , reason = "xcube is not installed" )
182+ class WriteLevelsTest (unittest .TestCase ):
183+ def setUp (self ):
184+ clear_memory_fs ()
185+
186+ # noinspection PyMethodMayBeStatic
187+
132188 def test_force_new (self ):
133- source_path = "memory://source.zarr"
134189 make_test_dataset (
135190 uri = source_path ,
136191 dims = ("time" , "lat" , "lon" ),
@@ -145,12 +200,13 @@ def test_force_new(self):
145200 (target_dir / "0.zarr" / ".zgroup" ).write ("{}" )
146201 self .assertTrue (target_dir .exists ())
147202
148- write_levels (source_path = source_path , target_path = target_dir .uri , force_new = True )
203+ write_levels (
204+ source_path = source_path , target_path = target_dir .uri , force_new = True
205+ )
149206
150207 self .assertTrue (target_dir .exists ())
151208
152209 def test_default_x_y_with_crs (self ):
153- source_path = "memory://source.zarr"
154210 make_test_dataset (
155211 uri = source_path ,
156212 dims = ("time" , "y" , "x" ),
@@ -185,7 +241,6 @@ def test_default_x_y_with_crs(self):
185241 self .assert_level (target_dir .uri + "/3.zarr" , 3 , has_crs = True )
186242
187243 def test_default_lon_lat_no_crs (self ):
188- source_path = "memory://source.zarr"
189244 make_test_dataset (
190245 uri = source_path ,
191246 dims = ("time" , "lat" , "lon" ),
@@ -219,6 +274,78 @@ def test_default_lon_lat_no_crs(self):
219274 self .assert_level (target_dir .uri + "/2.zarr" , 2 , xy_dims = xy_dims )
220275 self .assert_level (target_dir .uri + "/3.zarr" , 3 , xy_dims = xy_dims )
221276
277+ def test_default_lon_lat_no_crs_from_source_ds (self ):
278+ source_ds = make_test_dataset (
279+ uri = source_path ,
280+ dims = ("time" , "lat" , "lon" ),
281+ shape = (3 , 1024 , 2048 ),
282+ chunks = (1 , 128 , 256 ),
283+ )
284+
285+ target_dir = FileObj ("memory://target.levels" )
286+ self .assertFalse (target_dir .exists ())
287+
288+ write_levels (source_ds = source_ds , target_path = target_dir .uri )
289+
290+ self .assertTrue (target_dir .exists ())
291+
292+ levels_file = target_dir .for_path (".zlevels" )
293+ self .assertTrue (levels_file .exists ())
294+ levels_info = json .loads (levels_file .read ())
295+ self .assertEqual (
296+ {
297+ "version" : "1.0" ,
298+ "num_levels" : 4 ,
299+ "agg_methods" : {"chl" : "mean" , "tsm" : "mean" },
300+ "use_saved_levels" : False ,
301+ },
302+ levels_info ,
303+ )
304+
305+ xy_dims = "lon" , "lat"
306+ self .assert_level (target_dir .uri + "/0.zarr" , 0 , xy_dims = xy_dims )
307+ self .assert_level (target_dir .uri + "/1.zarr" , 1 , xy_dims = xy_dims )
308+ self .assert_level (target_dir .uri + "/2.zarr" , 2 , xy_dims = xy_dims )
309+ self .assert_level (target_dir .uri + "/3.zarr" , 3 , xy_dims = xy_dims )
310+
311+ def test_default_lon_lat_no_crs_from_source_ds_with_offset (self ):
312+ source_ds = make_test_dataset (
313+ uri = source_path ,
314+ dims = ("time" , "lat" , "lon" ),
315+ shape = (10 , 1024 , 2048 ),
316+ chunks = (1 , 128 , 256 ),
317+ )
318+
319+ target_dir = FileObj ("memory://target.levels" )
320+ self .assertFalse (target_dir .exists ())
321+
322+ write_levels (
323+ source_ds = source_ds ,
324+ source_append_offset = 7 ,
325+ target_path = target_dir .uri ,
326+ )
327+
328+ self .assertTrue (target_dir .exists ())
329+
330+ levels_file = target_dir .for_path (".zlevels" )
331+ self .assertTrue (levels_file .exists ())
332+ levels_info = json .loads (levels_file .read ())
333+ self .assertEqual (
334+ {
335+ "version" : "1.0" ,
336+ "num_levels" : 4 ,
337+ "agg_methods" : {"chl" : "mean" , "tsm" : "mean" },
338+ "use_saved_levels" : False ,
339+ },
340+ levels_info ,
341+ )
342+
343+ xy_dims = "lon" , "lat"
344+ self .assert_level (target_dir .uri + "/0.zarr" , 0 , xy_dims = xy_dims )
345+ self .assert_level (target_dir .uri + "/1.zarr" , 1 , xy_dims = xy_dims )
346+ self .assert_level (target_dir .uri + "/2.zarr" , 2 , xy_dims = xy_dims )
347+ self .assert_level (target_dir .uri + "/3.zarr" , 3 , xy_dims = xy_dims )
348+
222349 def test_link_level_zero (self ):
223350 source_dir = FileObj ("memory://source.zarr" )
224351 make_test_dataset (
0 commit comments