-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest_storepool.py
359 lines (321 loc) · 14.1 KB
/
test_storepool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import json
import os
import unittest
import jsonschema
import yaml
from xcube.core.store import DataStore
from xcube.core.store import DataStoreConfig
from xcube.core.store import DataStoreError
from xcube.core.store import DataStoreInstance
from xcube.core.store import DataStorePool
from xcube.core.store import get_data_store_instance
class GetDataStoreTest(unittest.TestCase):
def test_get_data_store_instance_new_inst(self):
instance = get_data_store_instance('memory')
self.assertIsInstance(instance, DataStoreInstance)
self.assertIsInstance(instance.store, DataStore)
instance2 = get_data_store_instance('memory')
self.assertIsNot(instance, instance2)
self.assertIsNot(instance.store, instance2.store)
def test_get_data_store_instance_from_pool(self):
pool = DataStorePool({
'dir': DataStoreConfig('file',
store_params=dict(root='.'))
})
instance = get_data_store_instance('@dir', store_pool=pool)
self.assertTrue(hasattr(instance.store, 'root'))
# noinspection PyUnresolvedReferences
self.assertTrue(os.path.isabs(instance.store.root))
self.assertTrue(os.path.isdir(instance.store.root))
instance2 = get_data_store_instance('@dir', store_pool=pool)
self.assertIs(instance, instance2)
def test_get_data_store_instance_from_pool_with_params(self):
pool = DataStorePool({
'@dir': DataStoreConfig('file',
store_params=dict(root='.'))
})
with self.assertRaises(ValueError) as cm:
get_data_store_instance(
'@dir', store_pool=pool, store_params={'auto_mkdir': True}
)
self.assertEqual('store_params cannot be given,'
' with store_id ("@dir") referring'
' to a configured store',
f'{cm.exception}')
def test_get_data_store_instance_from_pool_without_pool(self):
with self.assertRaises(ValueError) as cm:
get_data_store_instance('@dir')
self.assertEqual('store_pool must be given,'
' with store_id ("@dir") referring'
' to a configured store',
f'{cm.exception}')
def test_normalize(self):
pool = DataStorePool({
'@dir': DataStoreConfig('directory',
store_params=dict(root='.'))
})
file_path = '_test-data-stores-pool.json'
with open(file_path, 'w') as fp:
json.dump(pool.to_dict(), fp)
try:
pool_1 = DataStorePool.normalize(file_path)
self.assertIsInstance(pool_1, DataStorePool)
pool_2 = DataStorePool.normalize(pool_1)
self.assertIs(pool_2, pool_1)
pool_3 = DataStorePool.normalize(pool_2.to_dict())
self.assertIsInstance(pool_3, DataStorePool)
finally:
os.remove(file_path)
with self.assertRaises(TypeError):
# noinspection PyTypeChecker
DataStorePool.normalize(42)
class DataStoreConfigTest(unittest.TestCase):
def test_constructor_and_instance_props(self):
store_config = DataStoreConfig('file',
store_params={'root': '.'},
title='Local',
description='Local files')
self.assertEqual('file', store_config.store_id)
self.assertEqual({'root': '.'}, store_config.store_params)
self.assertEqual('Local', store_config.title)
self.assertEqual('Local files', store_config.description)
def test_constructor_asserts(self):
with self.assertRaises(ValueError) as cm:
DataStoreConfig('')
self.assertEqual('store_id must be given', f'{cm.exception}')
with self.assertRaises(TypeError) as cm:
# noinspection PyTypeChecker
DataStoreConfig('directory', store_params=[1, 'B'])
self.assertEqual("store_params must be an instance"
" of <class 'dict'>, was <class 'list'>",
f'{cm.exception}')
def test_to_dict(self):
store_config = DataStoreConfig('directory',
store_params={'base_dir': '.'},
title='Local',
description='Local files')
self.assertEqual({'description': 'Local files',
'name': 'Local',
'store_id': 'directory',
'store_params': {'base_dir': '.'}},
store_config.to_dict())
def test_from_dict(self):
store_config = DataStoreConfig.from_dict({
'description': 'Local files',
'title': 'Local',
'store_id': 'file',
'store_params': {'root': '.'}
})
self.assertIsInstance(store_config, DataStoreConfig)
self.assertEqual('file', store_config.store_id)
self.assertEqual({'root': '.'}, store_config.store_params)
self.assertEqual('Local', store_config.title)
self.assertEqual('Local files', store_config.description)
def test_from_dict_with_valid_cost_params(self):
store_config = DataStoreConfig.from_dict({
'description': 'Local files',
'title': 'Local',
'store_id': 'file',
'store_params': {'root': '.'},
'cost_params': {
'input_pixels_per_punit': 500,
'output_pixels_per_punit': 100,
'input_punits_weight': 1.1,
}
})
self.assertIsInstance(store_config, DataStoreConfig)
self.assertEqual('file', store_config.store_id)
self.assertEqual({'root': '.'}, store_config.store_params)
self.assertEqual('Local', store_config.title)
self.assertEqual('Local files', store_config.description)
def test_from_dict_with_invalid_cost_params(self):
with self.assertRaises(DataStoreError):
DataStoreConfig.from_dict({'description': 'Local files',
'title': 'Local',
'store_id': 'file',
'store_params': {'root': '.'},
'cost_params': {
# Required:
# 'input_pixels_per_punit': 10,
# 'output_pixels_per_punit': 20,
}})
class DataStorePoolTest(unittest.TestCase):
def test_default_constr(self):
pool = DataStorePool()
self.assertEqual([], pool.store_instance_ids)
self.assertEqual([], pool.store_configs)
def test_from_dict_empty(self):
pool = DataStorePool.from_dict({})
self.assertIsInstance(pool, DataStorePool)
self.assertEqual([], pool.store_instance_ids)
self.assertEqual([], pool.store_configs)
def test_from_dict_no_store_params(self):
store_configs = {
"ram-1": {
"store_id": "memory"
}
}
pool = DataStorePool.from_dict(store_configs)
self.assertIsInstance(pool, DataStorePool)
self.assertEqual(["ram-1"], pool.store_instance_ids)
self.assertIsInstance(pool.get_store_config('ram-1'), DataStoreConfig)
def test_from_dict_with_bad_dicts(self):
store_configs = {
"dir": {
}
}
with self.assertRaises(jsonschema.exceptions.ValidationError) as cm:
DataStorePool.from_dict(store_configs)
self.assertTrue("'store_id' is a required property" in f'{cm.exception}', msg=f'{cm.exception}')
store_configs = {
"dir": {
"store_id": 10
}
}
with self.assertRaises(jsonschema.exceptions.ValidationError) as cm:
DataStorePool.from_dict(store_configs)
self.assertTrue("Failed validating 'type' in schema" in f'{cm.exception}', msg=f'{cm.exception}')
def test_from_json_file(self):
store_configs = {
"ram-1": {
"store_id": "memory"
},
"ram-2": {
"store_id": "memory"
}
}
path = 'test-store-configs.json'
with open(path, 'w') as fp:
json.dump(store_configs, fp, indent=2)
try:
pool = DataStorePool.from_file(path)
self.assertIsInstance(pool, DataStorePool)
self.assertEqual(['ram-1', 'ram-2'], pool.store_instance_ids)
finally:
import os
os.remove(path)
def test_from_yaml_file(self):
store_configs = {
"ram-1": {
"store_id": "memory"
},
"ram-2": {
"store_id": "memory"
}
}
path = 'test-store-configs.yaml'
with open(path, 'w') as fp:
yaml.dump(store_configs, fp, indent=2)
try:
pool = DataStorePool.from_file(path)
self.assertIsInstance(pool, DataStorePool)
self.assertEqual(['ram-1', 'ram-2'], pool.store_instance_ids)
finally:
import os
os.remove(path)
def test_get_store(self):
store_configs = {
"dir-1": {
"store_id": "file",
"store_params": {
"root": "./bibo"
}
},
}
pool = DataStorePool.from_dict(store_configs)
store = pool.get_store('dir-1')
self.assertTrue(hasattr(store, 'root'))
# noinspection PyUnresolvedReferences
self.assertTrue(os.path.isabs(store.root))
self.assertFalse(os.path.exists(store.root))
# Should stay same instance
self.assertIs(store, pool.get_store('dir-1'))
self.assertIs(store, pool.get_store('dir-1'))
def test_get_store_error(self):
pool = DataStorePool()
with self.assertRaises(DataStoreError) as cm:
pool.get_store('dir-1')
self.assertEqual('Configured data store instance "dir-1" not found.', f'{cm.exception}')
def test_to_dict(self):
self.assertEqual({}, DataStorePool().to_dict())
self.assertEqual({'ram': {'store_id': 'memory'},
'dir': {'store_id': 'directory',
'store_params': {'base_dir': 'bibo'}}},
DataStorePool({'ram': DataStoreConfig(store_id='memory'),
'dir': DataStoreConfig(store_id='directory',
store_params=dict(base_dir="bibo"))
}).to_dict())
def test_close_all_stores(self):
store_configs = {
"ram-1": {
"store_id": "memory",
},
}
pool = DataStorePool.from_dict(store_configs)
# Smoke test, we do not expect any visible state changes after close_all_stores()
pool.close_all_stores()
def test_add_remove_store_config(self):
pool = DataStorePool()
self.assertEqual([], pool.store_instance_ids)
pool.add_store_config('mem-1', DataStoreConfig('memory'))
self.assertEqual(['mem-1'], pool.store_instance_ids)
pool.add_store_config('mem-2', DataStoreConfig('memory'))
self.assertEqual(['mem-1', 'mem-2'], pool.store_instance_ids)
pool.add_store_config('mem-1', DataStoreConfig('memory'))
self.assertEqual(['mem-1', 'mem-2'], pool.store_instance_ids)
pool.remove_store_config('mem-1')
self.assertEqual(['mem-2'], pool.store_instance_ids)
pool.remove_store_config('mem-2')
self.assertEqual([], pool.store_instance_ids)
def test_multi_stores_with_params(self):
"""Just test many stores at once"""
store_configs = {
"ram-1": {
"store_id": "memory",
},
"ram-2": {
"store_id": "memory",
},
"local-1": {
"store_id": "file",
"store_params": {
"root": "/home/bibo/datacubes-1",
}
},
"local-2": {
"store_id": "file",
"store_params": {
"root": "/home/bibo/datacubes-2",
}
},
}
pool = DataStorePool.from_dict(store_configs)
self.assertIsInstance(pool, DataStorePool)
self.assertEqual(["local-1", "local-2", "ram-1", "ram-2"], pool.store_instance_ids)
for instance_id in pool.store_instance_ids:
self.assertTrue(pool.has_store_instance(instance_id))
self.assertIsInstance(pool.get_store_config(instance_id), DataStoreConfig)
self.assertIsInstance(pool.get_store(instance_id), DataStore)
def test_get_store_instance_id(self):
store_params_1 = {
"root": "./bibo"
}
ds_config_1 = DataStoreConfig(store_id='file',
store_params=store_params_1)
ds_configs = {'dir-1': ds_config_1}
pool = DataStorePool(ds_configs)
store_params_2 = {
"root": "./babo"
}
ds_config_2 = DataStoreConfig(store_id='file',
store_params=store_params_2)
ds_config_3 = DataStoreConfig(store_id='file',
store_params=store_params_1,
title='A third configuration')
self.assertEqual('dir-1', pool.get_store_instance_id(ds_config_1))
self.assertEqual('dir-1', pool.get_store_instance_id(ds_config_1,
strict_check=True))
self.assertIsNone(pool.get_store_instance_id(ds_config_2))
self.assertEqual('dir-1', pool.get_store_instance_id(ds_config_3))
self.assertIsNone(pool.get_store_instance_id(ds_config_3,
strict_check=True))