/
test_errors.py
294 lines (237 loc) · 9.7 KB
/
test_errors.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
#!/usr/bin/env python
"""Test custom Mapchete errors."""
import pytest
from copy import deepcopy
import mapchete
from mapchete.config import MapcheteConfig, validate_values
from mapchete._processing import Executor
from mapchete.tile import BufferedTilePyramid
from mapchete import errors
def test_mapchete_init():
"""Raise TypeError if not MapcheteConfig object is passed."""
with pytest.raises(TypeError):
mapchete.Mapchete("wrong_type")
def test_config_modes(example_mapchete):
"""Assert process mode is handled correctly."""
# invalid mode
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(example_mapchete.path, mode="invalid")
def test_execute(example_mapchete):
"""Mapchete execute() errors."""
# in readonly mode
with mapchete.open(example_mapchete.path, mode="readonly") as mp:
with pytest.raises(ValueError):
mp.execute(next(mp.get_process_tiles()))
# wrong tile type
with mapchete.open(example_mapchete.path) as mp:
with pytest.raises(TypeError):
mp.execute("invalid")
def test_read(example_mapchete):
"""Mapchete read() errors."""
# in memory mode
with mapchete.open(example_mapchete.path, mode="memory") as mp:
with pytest.raises(ValueError):
mp.read(next(mp.get_process_tiles()))
# wrong tile type
with mapchete.open(example_mapchete.path) as mp:
with pytest.raises(TypeError):
mp.read("invalid")
def test_write(cleantopo_tl):
"""Test write function when passing an invalid process_tile."""
with mapchete.open(cleantopo_tl.path) as mp:
# process and save
with pytest.raises(TypeError):
mp.write("invalid tile", None)
def test_get_raw_output(example_mapchete):
"""Mapchete get_raw_output() errors."""
with mapchete.open(example_mapchete.path) as mp:
# wrong tile type
with pytest.raises(TypeError):
mp.get_raw_output("invalid")
# not matching CRSes
tile = BufferedTilePyramid("mercator").tile(7, 1, 1)
with pytest.raises(NotImplementedError):
mp.get_raw_output(tile)
def test_process_tile_write(example_mapchete):
"""Raise DeprecationWarning on MapcheteProcess.write()."""
config = MapcheteConfig(example_mapchete.path)
tile = BufferedTilePyramid("mercator").tile(7, 1, 1)
user_process = mapchete.MapcheteProcess(
tile=tile,
params=config.params_at_zoom(tile.zoom),
input=config.get_inputs_for_tile(tile),
)
with pytest.raises(DeprecationWarning):
user_process.write("data")
def test_process_tile_open(example_mapchete):
"""Raise ValueError on MapcheteProcess.open()."""
config = MapcheteConfig(example_mapchete.path)
tile = BufferedTilePyramid("mercator").tile(7, 1, 1)
user_process = mapchete.MapcheteProcess(
tile=tile,
params=config.params_at_zoom(tile.zoom),
input=config.get_inputs_for_tile(tile),
)
with pytest.raises(ValueError):
user_process.open("nonexisting_id")
def test_process_tile_read(example_mapchete):
"""Raise ValueError on MapcheteProcess.open()."""
config = MapcheteConfig(example_mapchete.path)
tile = BufferedTilePyramid("mercator").tile(7, 1, 1)
user_process = mapchete.MapcheteProcess(
tile=tile,
params=config.params_at_zoom(tile.zoom),
input=config.get_inputs_for_tile(tile),
)
with pytest.raises(DeprecationWarning):
user_process.read()
def test_metatiles(example_mapchete):
"""Assert metatile sizes are checked."""
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config["pyramid"].update(metatiling=1)
config["output"].update(metatiling=2)
MapcheteConfig(config)
def test_no_cli_input_file(example_mapchete):
"""Assert input file from command line is checked."""
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.update(input="from_command_line")
MapcheteConfig(config)
def test_wrong_bounds(example_mapchete):
"""Wrong bounds number raises error."""
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(example_mapchete.dict, bounds=[2, 3])
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(dict(example_mapchete.dict, process_bounds=[2, 3]))
def test_empty_input_files(example_mapchete):
"""Assert empty input files raises error."""
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
del config["input"]
MapcheteConfig(config)
def test_mandatory_params(example_mapchete):
"""Check availability of mandatory parameters."""
for param in ["process", "input", "output"]:
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
del config[param]
MapcheteConfig(config)
# invalid path
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.update(process="invalid/path.py")
MapcheteConfig(config).process
# no config dir given
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.pop("config_dir")
MapcheteConfig(config).process
def test_invalid_output_params(example_mapchete):
"""Check on invalid configuration."""
# missing or invalid params
for param in ["format"]:
config = deepcopy(example_mapchete.dict)
with pytest.raises(errors.MapcheteConfigError):
# invalid
config["output"][param] = "invalid"
MapcheteConfig(config)
with pytest.raises(errors.MapcheteConfigError):
# missing
config["output"].pop(param)
MapcheteConfig(config)
def test_invalid_zoom_levels(example_mapchete):
"""Check on invalid zoom configuration."""
# process zooms
# no zoom levels given
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.pop("zoom_levels")
MapcheteConfig(config)
# invalid single zoom level
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.update(zoom_levels=-5)
MapcheteConfig(config)
# invalid zoom level in pair
with pytest.raises(errors.MapcheteConfigError):
config = deepcopy(example_mapchete.dict)
config.update(zoom_levels=[-5, 0])
MapcheteConfig(config)
# min or max missing
config = deepcopy(example_mapchete.dict)
config.update(zoom_levels=dict(min=0))
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(config)
config.update(zoom_levels=dict(max=5))
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(config)
# min bigger than max
config = deepcopy(example_mapchete.dict)
config.update(zoom_levels=dict(min=5, max=0))
# init zooms
# invalid single zoom level
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(config, zoom=-5)
# invalid zoom level in pair
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(config, zoom=[-5, 0])
# not a subset
with pytest.raises(errors.MapcheteConfigError):
MapcheteConfig(config, zoom=[0, 20])
def test_zoom_dependend_functions(cleantopo_br):
with mapchete.open(cleantopo_br.dict) as mp:
with pytest.raises(ValueError):
mp.config.params_at_zoom(20)
with pytest.raises(ValueError):
mp.config.area_at_zoom(20)
def test_validate_values():
with pytest.raises(TypeError):
validate_values(None, None)
def test_input_error(cleantopo_br_tiledir):
config = deepcopy(cleantopo_br_tiledir.dict)
config["input"].update(file1=dict(format="TileDirectory"))
with pytest.raises(errors.MapcheteDriverError):
MapcheteConfig(config)
def test_import_error(mp_tmpdir, cleantopo_br, import_error_py):
"""Assert import error is raised."""
config = cleantopo_br.dict
config.update(process=import_error_py)
with pytest.raises(errors.MapcheteProcessImportError):
mapchete.open(config)
def test_malformed_process(cleantopo_br, malformed_py):
"""Assert import error is raised."""
config = cleantopo_br.dict
config.update(process=malformed_py)
with pytest.raises(errors.MapcheteProcessImportError):
mapchete.open(config)
def test_syntax_error(mp_tmpdir, cleantopo_br, syntax_error_py):
"""Assert syntax error is raised."""
config = cleantopo_br.dict
config.update(process=syntax_error_py)
with pytest.raises(errors.MapcheteProcessSyntaxError):
mapchete.open(config)
def test_process_exception(mp_tmpdir, cleantopo_br, process_error_py):
"""Assert process exception is raised."""
config = cleantopo_br.dict
config.update(process=process_error_py)
with mapchete.open(config) as mp:
with pytest.raises(errors.MapcheteProcessException):
mp.execute((5, 0, 0))
def test_output_error(mp_tmpdir, cleantopo_br, output_error_py):
"""Assert output error is raised."""
config = cleantopo_br.dict
config.update(process=output_error_py)
with mapchete.open(config) as mp:
with pytest.raises(errors.MapcheteProcessOutputError):
mp.execute((5, 0, 0))
def _raise_error(i):
"""Helper function for test_finished_task()"""
1/0
def test_finished_task():
"""Encapsulating exceptions test."""
task = next(Executor().as_completed(func=_raise_error, iterable=[0]))
assert task.exception()
with pytest.raises(ZeroDivisionError):
task.result()
assert "FinishedTask" in str(task)