-
Notifications
You must be signed in to change notification settings - Fork 201
/
test_drvsupport.py
332 lines (264 loc) · 11.1 KB
/
test_drvsupport.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
"""Tests of driver support"""
import logging
import pytest
from .conftest import requires_gdal24, get_temp_filename
from fiona.drvsupport import supported_drivers, driver_mode_mingdal
import fiona.drvsupport
from fiona.env import GDALVersion
from fiona._env import calc_gdal_version_num, get_gdal_version_num
from fiona.errors import DriverError
log = logging.getLogger()
@requires_gdal24
@pytest.mark.gdal
@pytest.mark.parametrize("format", ["GeoJSON", "ESRIJSON", "TopoJSON", "GeoJSONSeq"])
def test_geojsonseq(format):
"""Format is available"""
assert format in fiona.drvsupport.supported_drivers.keys()
@pytest.mark.parametrize(
"driver", [driver for driver, raw in supported_drivers.items() if "w" in raw]
)
@pytest.mark.gdal
def test_write_or_driver_error(tmpdir, driver, testdata_generator):
"""Test if write mode works."""
if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
pytest.skip("BNA driver segfaults with gdal 1.11")
schema, crs, records1, _, _ = testdata_generator(driver, range(0, 10), [])
path = str(tmpdir.join(get_temp_filename(driver)))
if driver in driver_mode_mingdal[
"w"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["w"][driver]
):
# Test if DriverError is raised for gdal < driver_mode_mingdal
with pytest.raises(DriverError):
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
else:
# Test if we can write
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
if driver in {"FileGDB", "OpenFileGDB"}:
open_driver = driver
else:
open_driver = None
with fiona.open(path, driver=open_driver) as collection:
assert collection.driver == driver
assert len(list(collection)) == len(records1)
# If this test fails, it should be considered to update
# driver_mode_mingdal in drvsupport.py.
@pytest.mark.parametrize(
"driver", [driver for driver in driver_mode_mingdal["w"].keys()]
)
@pytest.mark.gdal
def test_write_does_not_work_when_gdal_smaller_mingdal(
tmpdir, driver, testdata_generator, monkeypatch
):
"""Test if driver really can't write for gdal < driver_mode_mingdal."""
if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
pytest.skip("BNA driver segfaults with gdal 1.11")
if driver == "FlatGeobuf" and calc_gdal_version_num(
3, 1, 0
) <= get_gdal_version_num() < calc_gdal_version_num(3, 1, 3):
pytest.skip("See https://github.com/Toblerity/Fiona/pull/924")
schema, crs, records1, _, _ = testdata_generator(driver, range(0, 10), [])
path = str(tmpdir.join(get_temp_filename(driver)))
if driver in driver_mode_mingdal[
"w"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["w"][driver]
):
monkeypatch.delitem(fiona.drvsupport.driver_mode_mingdal["w"], driver)
with pytest.raises(Exception):
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
# Some driver only allow a specific schema. These drivers can be
# excluded by adding them to blacklist_append_drivers.
@pytest.mark.parametrize(
"driver", [driver for driver, raw in supported_drivers.items() if "a" in raw]
)
@pytest.mark.gdal
def test_append_or_driver_error(tmpdir, testdata_generator, driver):
"""Test if driver supports append mode."""
if driver == "DGN":
pytest.xfail("DGN schema has changed")
if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
pytest.skip("BNA driver segfaults with gdal 1.11")
path = str(tmpdir.join(get_temp_filename(driver)))
schema, crs, records1, records2, _ = testdata_generator(
driver, range(0, 5), range(5, 10)
)
# If driver is not able to write, we cannot test append
if driver in driver_mode_mingdal[
"w"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["w"][driver]
):
return
# Create test file to append to
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
if driver in driver_mode_mingdal[
"a"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["a"][driver]
):
# Test if DriverError is raised for gdal < driver_mode_mingdal
with pytest.raises(DriverError):
with fiona.open(path, "a", driver=driver) as c:
c.writerecords(records2)
else:
# Test if we can append
with fiona.open(path, "a", driver=driver) as c:
c.writerecords(records2)
if driver in {"FileGDB", "OpenFileGDB"}:
open_driver = driver
else:
open_driver = None
with fiona.open(path, driver=open_driver) as collection:
assert collection.driver == driver
assert len(list(collection)) == len(records1) + len(records2)
# If this test fails, it should be considered to update
# driver_mode_mingdal in drvsupport.py.
@pytest.mark.parametrize(
"driver",
[
driver
for driver in driver_mode_mingdal["a"].keys()
if driver in supported_drivers
],
)
@pytest.mark.gdal
def test_append_does_not_work_when_gdal_smaller_mingdal(
tmpdir, driver, testdata_generator, monkeypatch
):
"""Test if driver supports append mode."""
if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
pytest.skip("BNA driver segfaults with gdal 1.11")
if driver == "FlatGeobuf" and GDALVersion.runtime() < GDALVersion(3, 5):
pytest.skip("FlatGeobuf segfaults with GDAL < 3.5.1")
path = str(tmpdir.join(get_temp_filename(driver)))
schema, crs, records1, records2, _ = testdata_generator(
driver, range(0, 5), range(5, 10)
)
# If driver is not able to write, we cannot test append
if driver in driver_mode_mingdal[
"w"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["w"][driver]
):
return
# Create test file to append to
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
if driver in driver_mode_mingdal[
"a"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["a"][driver]
):
# Test if driver really can't append for gdal < driver_mode_mingdal
monkeypatch.delitem(fiona.drvsupport.driver_mode_mingdal["a"], driver)
with pytest.raises(Exception):
with fiona.open(path, "a", driver=driver) as c:
c.writerecords(records2)
if driver in {"FileGDB", "OpenFileGDB"}:
open_driver = driver
else:
open_driver = None
with fiona.open(path, driver=open_driver) as collection:
assert collection.driver == driver
assert len(list(collection)) == len(records1) + len(records2)
# If this test fails, it should be considered to enable write support
# for the respective driver in drvsupport.py.
@pytest.mark.parametrize(
"driver", [driver for driver, raw in supported_drivers.items() if raw == "r"]
)
@pytest.mark.gdal
def test_no_write_driver_cannot_write(tmpdir, driver, testdata_generator, monkeypatch):
"""Test if read only driver cannot write."""
monkeypatch.setitem(fiona.drvsupport.supported_drivers, driver, "rw")
schema, crs, records1, _, _ = testdata_generator(driver, range(0, 5), [])
if driver == "BNA" and GDALVersion.runtime() < GDALVersion(2, 0):
pytest.skip("BNA driver segfaults with gdal 1.11")
if driver == "FlatGeobuf":
pytest.xfail("FlatGeobuf doesn't raise an error but doesn't have write support")
path = str(tmpdir.join(get_temp_filename(driver)))
with pytest.raises(Exception):
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
# If this test fails, it should be considered to enable append support
# for the respective driver in drvsupport.py.
@pytest.mark.parametrize(
"driver",
[
driver
for driver, raw in supported_drivers.items()
if "w" in raw and "a" not in raw
],
)
@pytest.mark.gdal
def test_no_append_driver_cannot_append(
tmpdir, driver, testdata_generator, monkeypatch
):
"""Test if a driver that supports write and not append cannot also append."""
monkeypatch.setitem(fiona.drvsupport.supported_drivers, driver, "raw")
if driver == "FlatGeobuf" and get_gdal_version_num() == calc_gdal_version_num(3, 5, 0):
pytest.skip("FlatGeobuf driver segfaults with gdal 3.5.0")
path = str(tmpdir.join(get_temp_filename(driver)))
schema, crs, records1, records2, _ = testdata_generator(
driver, range(0, 5), range(5, 10)
)
# If driver is not able to write, we cannot test append
if driver in driver_mode_mingdal[
"w"
] and get_gdal_version_num() < calc_gdal_version_num(
*driver_mode_mingdal["w"][driver]
):
return
# Create test file to append to
with fiona.open(path, "w", driver=driver, crs=crs, schema=schema) as c:
c.writerecords(records1)
try:
with fiona.open(path, "a", driver=driver) as c:
c.writerecords(records2)
except Exception:
log.exception("Caught exception in trying to append.")
return
if driver in {"FileGDB", "OpenFileGDB"}:
open_driver = driver
else:
open_driver = None
with fiona.open(path, driver=open_driver) as collection:
assert collection.driver == driver
assert len(list(collection)) == len(records1)
def test_mingdal_drivers_are_supported():
"""Test if mode and driver is enabled in supported_drivers"""
for mode in driver_mode_mingdal:
for driver in driver_mode_mingdal[mode]:
# we cannot test drivers that are not present in the gdal installation
if driver in supported_drivers:
assert mode in supported_drivers[driver]
def test_allow_unsupported_drivers(monkeypatch, tmpdir):
"""Test if allow unsupported drivers works as expected"""
# We delete a known working driver from fiona.drvsupport so that we can use it
monkeypatch.delitem(fiona.drvsupport.supported_drivers, "GPKG")
schema = {"geometry": "Polygon", "properties": {}}
# Test that indeed we can't create a file without allow_unsupported_drivers
path1 = str(tmpdir.join("test1.gpkg"))
with pytest.raises(DriverError):
with fiona.open(path1, mode="w", driver="GPKG", schema=schema):
pass
# Test that we can create file with allow_unsupported_drivers=True
path2 = str(tmpdir.join("test2.gpkg"))
try:
with fiona.open(
path2,
mode="w",
driver="GPKG",
schema=schema,
allow_unsupported_drivers=True,
):
pass
except Exception as e:
assert (
False
), f"Using allow_unsupported_drivers=True should not raise an exception: {e}"