@@ -26,65 +26,133 @@ def setUp(self):
2626 def handle_rollback_action (self , * args ):
2727 self .records .append (args )
2828
29- def test_create_zarr (self ):
29+ def test_to_zarr (self ):
3030 target_dir = FileObj ("memory://target.zarr" )
31- ds = make_test_dataset ()
31+ ds = make_test_dataset (
32+ shape = (1 , 50 , 100 ),
33+ chunks = (2 , 50 , 50 )
34+ )
3235 ds .to_zarr (RollbackStore (target_dir .fs .get_mapper (
3336 root = target_dir .path .strip ("/" ),
3437 create = True
3538 ), self .handle_rollback_action ))
36- self .assertTrue (target_dir .exists ())
37- ds2 = xr .open_zarr (target_dir .fs .get_mapper (
39+ ds = xr .open_zarr (target_dir .fs .get_mapper (
3840 root = target_dir .path .strip ("/" ),
3941 create = False
4042 ))
41- self .assertEqual (ds . dims , ds2 .dims )
43+ self .assertEqual ({ 'time' : 1 , 'y' : 50 , 'x' : 100 }, ds .dims )
4244 self .assertEqual (
4345 {
44- ('delete_file' , '.zmetadata' ),
45- ('delete_file' , '.zgroup' ),
4646 ('delete_file' , '.zattrs' ),
47- ('delete_file' , 'x/.zarray' ),
48- ('delete_file' , 'x/.zattrs' ),
49- ('delete_file' , 'x/0' ),
50- ('delete_file' , 'y/.zarray' ),
51- ('delete_file' , 'y/.zattrs' ),
52- ('delete_file' , 'y/0' ),
53- ('delete_file' , 'time/.zarray' ),
54- ('delete_file' , 'time/.zattrs' ),
55- ('delete_file' , 'time/0' ),
47+ ('delete_file' , '.zgroup' ),
48+ ('delete_file' , '.zmetadata' ),
5649 ('delete_file' , 'chl/.zarray' ),
5750 ('delete_file' , 'chl/.zattrs' ),
5851 ('delete_file' , 'chl/0.0.0' ),
5952 ('delete_file' , 'chl/0.0.1' ),
60- ('delete_file' , 'chl/0.1.0' ),
61- ('delete_file' , 'chl/0.1.1' ),
62- ('delete_file' , 'chl/1.0.0' ),
63- ('delete_file' , 'chl/1.0.1' ),
64- ('delete_file' , 'chl/1.1.0' ),
65- ('delete_file' , 'chl/1.1.1' ),
66- ('delete_file' , 'chl/2.0.0' ),
67- ('delete_file' , 'chl/2.0.1' ),
68- ('delete_file' , 'chl/2.1.0' ),
69- ('delete_file' , 'chl/2.1.1' ),
53+ ('delete_file' , 'time/.zarray' ),
54+ ('delete_file' , 'time/.zattrs' ),
55+ ('delete_file' , 'time/0' ),
7056 ('delete_file' , 'tsm/.zarray' ),
7157 ('delete_file' , 'tsm/.zattrs' ),
7258 ('delete_file' , 'tsm/0.0.0' ),
7359 ('delete_file' , 'tsm/0.0.1' ),
60+ ('delete_file' , 'x/.zarray' ),
61+ ('delete_file' , 'x/.zattrs' ),
62+ ('delete_file' , 'x/0' ),
63+ ('delete_file' , 'y/.zarray' ),
64+ ('delete_file' , 'y/.zattrs' ),
65+ ('delete_file' , 'y/0' )
66+ },
67+ set ([r [:2 ] for r in self .records ])
68+ )
69+
70+ #####################################################################
71+ # Add slice 1
72+
73+ self .records = []
74+ slice_1 = make_test_dataset (
75+ shape = (1 , 50 , 100 ),
76+ chunks = (1 , 50 , 50 )
77+ )
78+ for k , v in slice_1 .variables .items ():
79+ v .encoding = {}
80+ v .attrs = {}
81+
82+ slice_1 .to_zarr (RollbackStore (
83+ target_dir .fs .get_mapper (
84+ root = target_dir .path .strip ("/" ),
85+ create = False
86+ ),
87+ self .handle_rollback_action ),
88+ mode = "a" ,
89+ append_dim = "time"
90+ )
91+ ds = xr .open_zarr (target_dir .fs .get_mapper (
92+ root = target_dir .path .strip ("/" ),
93+ create = False
94+ ))
95+ self .assertEqual ({'time' : 2 , 'y' : 50 , 'x' : 100 }, ds .dims )
96+ self .assertEqual (
97+ {
98+ ('replace_file' , '.zmetadata' ),
99+ ('replace_file' , '.zattrs' ),
100+ ('replace_file' , 'x/0' ),
101+ ('replace_file' , 'y/0' ),
102+ ('replace_file' , 'time/.zarray' ),
103+ ('delete_file' , 'time/1' ),
104+ ('replace_file' , 'chl/.zarray' ),
105+ ('delete_file' , 'chl/1.0.0' ),
106+ ('delete_file' , 'chl/1.0.1' ),
107+ ('replace_file' , 'tsm/.zarray' ),
74108 ('delete_file' , 'tsm/1.0.0' ),
75109 ('delete_file' , 'tsm/1.0.1' ),
76- ('delete_file' , 'tsm/1.1.0' ),
77- ('delete_file' , 'tsm/1.1.1' ),
78- ('delete_file' , 'tsm/0.1.0' ),
79- ('delete_file' , 'tsm/0.1.1' ),
110+ },
111+ set ([r [:2 ] for r in self .records ])
112+ )
113+
114+ #####################################################################
115+ # Add slice 2
116+
117+ self .records = []
118+ slice_2 = make_test_dataset (
119+ shape = (1 , 50 , 100 ),
120+ chunks = (1 , 50 , 50 )
121+ )
122+ for k , v in slice_2 .variables .items ():
123+ v .encoding = {}
124+ v .attrs = {}
125+
126+ slice_2 .to_zarr (RollbackStore (
127+ target_dir .fs .get_mapper (
128+ root = target_dir .path .strip ("/" ),
129+ create = False
130+ ),
131+ self .handle_rollback_action ),
132+ mode = "a" ,
133+ append_dim = "time"
134+ )
135+ ds = xr .open_zarr (target_dir .fs .get_mapper (
136+ root = target_dir .path .strip ("/" ),
137+ create = False
138+ ))
139+ self .assertEqual ({'time' : 3 , 'y' : 50 , 'x' : 100 }, ds .dims )
140+ self .assertEqual ({'time' : 3 , 'y' : 50 , 'x' : 100 },
141+ ds .time .encoding )
142+ self .assertEqual (
143+ {
144+ ('replace_file' , '.zmetadata' ),
145+ ('replace_file' , '.zattrs' ),
146+ ('replace_file' , 'x/0' ),
147+ ('replace_file' , 'y/0' ),
148+ ('replace_file' , 'time/.zarray' ),
149+ ('delete_file' , 'time/2' ),
150+ ('replace_file' , 'chl/.zarray' ),
151+ ('delete_file' , 'chl/2.0.0' ),
152+ ('delete_file' , 'chl/2.0.1' ),
153+ ('replace_file' , 'tsm/.zarray' ),
80154 ('delete_file' , 'tsm/2.0.0' ),
81155 ('delete_file' , 'tsm/2.0.1' ),
82- ('delete_file' , 'tsm/2.1.0' ),
83- ('delete_file' , 'tsm/2.1.1' ),
84156 },
85157 set ([r [:2 ] for r in self .records ])
86158 )
87-
88- def test_append_zarr (self ):
89- # TODO implement test for appending a zarr with overlapping chunks
90- pass
0 commit comments