-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathtest_oracle_provider.py
More file actions
618 lines (482 loc) · 16.3 KB
/
test_oracle_provider.py
File metadata and controls
618 lines (482 loc) · 16.3 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# =================================================================
#
# Authors: Andreas Kosubek <andreas.kosubek@ama.gv.at>
#
# Copyright (c) 2023 Andreas Kosubek
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
# Needs to be run like: python3 -m pytest
# Create testdata: python3 load_oracle_data.py
import os
import pytest
from pygeoapi.provider.base import ProviderInvalidQueryError
from pygeoapi.provider.oracle import OracleProvider
USERNAME = os.environ.get("PYGEOAPI_ORACLE_USER", "geo_test")
PASSWORD = os.environ.get("PYGEOAPI_ORACLE_PASSWD", "geo_test")
SERVICE_NAME = os.environ.get("PYGEOAPI_ORACLE_SERVICE_NAME", "XEPDB1")
HOST = os.environ.get("PYGEOAPI_ORACLE_HOST", "127.0.0.1")
PORT = os.environ.get("PYGEOAPI_ORACLE_PORT", "1521")
class SqlManipulator:
def process_query(
self,
db,
sql_query,
bind_variables,
sql_manipulator_options,
offset,
limit,
resulttype,
bbox,
datetime_,
properties,
sortby,
skip_geometry,
select_properties,
crs_transform_spec,
q,
language,
filterq,
):
sql = "ID = 10 AND :foo != :bar"
if sql_query.find(" WHERE ") == -1:
sql_query = sql_query.replace("#WHERE#", f" WHERE {sql}")
else:
sql_query = sql_query.replace("#WHERE#", f" AND {sql}")
bind_variables = {
**bind_variables,
"foo": "foo",
"bar": sql_manipulator_options.get("foo"),
}
return sql_query, bind_variables
def process_get(
self,
db,
sql_query,
bind_variables,
sql_manipulator_options,
identifier,
):
sql_query = f"{sql_query} AND 'auth' = 'you arent allowed'"
return sql_query, bind_variables
def process_create(
self,
db,
sql_query,
bind_variables,
sql_manipulator_options,
request_data,
):
bind_variables["name"] = "overwritten"
return sql_query, bind_variables
def process_update(
self,
db,
sql_query,
bind_variables,
sql_manipulator_options,
identifier,
request_data,
):
bind_variables["area"] = 42
bind_variables["volume"] = 42
return sql_query, bind_variables
def process_delete(
self,
db,
sql_query,
bind_variables,
sql_manipulator_options,
identifier,
):
sql_query = f"{sql_query} AND 'auth' = 'you arent allowed'"
return sql_query, bind_variables
@pytest.fixture()
def config():
return {
"name": "Oracle",
"type": "feature",
"data": {
"host": HOST,
"port": PORT,
"service_name": SERVICE_NAME,
"user": USERNAME,
"password": PASSWORD,
},
"id_field": "id",
"table": "lakes",
"geom_field": "geometry",
"editable": True,
}
@pytest.fixture()
def config_public_synonym():
return {
"name": "Oracle",
"type": "feature",
"data": {
"host": HOST,
"port": PORT,
"service_name": SERVICE_NAME,
"user": USERNAME,
"password": PASSWORD,
},
"id_field": "id",
"table": "lakes_public_syn",
"geom_field": "geometry",
"editable": True,
}
@pytest.fixture()
def config_private_synonym():
return {
"name": "Oracle",
"type": "feature",
"data": {
"host": HOST,
"port": PORT,
"service_name": SERVICE_NAME,
"user": USERNAME,
"password": PASSWORD,
},
"id_field": "id",
"table": "lakes_private_syn",
"geom_field": "geometry",
"editable": True,
}
@pytest.fixture()
def config_manipulator(config):
return {
**config,
"sql_manipulator": "tests.test_oracle_provider.SqlManipulator",
"sql_manipulator_options": {"foo": "bar"},
}
@pytest.fixture()
def config_properties(config):
return {
**config,
"properties": ["id", "name", "wiki_link"],
}
@pytest.fixture()
def config_extra_properties(config):
return {
**config,
"extra_properties": ["'Here the name is ' || name || '!' as tooltip"],
}
@pytest.fixture()
def create_geojson():
return {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.012050, 47.841512],
[9.803470, 47.526461],
[9.476940, 47.459178],
[8.918151, 47.693253],
[9.012050, 47.841512],
]
],
},
"properties": {
"name": "Lake Constance",
"wiki_link": "https://en.wikipedia.org/wiki/Lake_Constance",
"foo": "bar",
},
}
@pytest.fixture()
def create_point_geojson():
return {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [9.603316032965449, 47.48872063967191],
},
"properties": {"name": "Yachthafen Fischerinsel", "wiki_link": None},
}
@pytest.fixture()
def update_geojson():
return {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[9.012050, 47.841512],
[9.803470, 47.526461],
[9.476940, 47.459178],
[8.918151, 47.693253],
[9.012050, 47.841512],
]
],
},
"properties": {
"name": "Lake Constance",
"wiki_link": "https://en.wikipedia.org/wiki/Lake_Constance",
"foo": "bar",
"area": 536000,
"volume": 48000,
},
"id": 26,
}
def test_query(config):
"""Test query for a valid JSON object with geometry"""
p = OracleProvider(config)
feature_collection = p.query()
assert feature_collection.get("type") == "FeatureCollection"
features = feature_collection.get("features")
assert features is not None
feature = features[0]
properties = feature.get("properties")
assert properties is not None
geometry = feature.get("geometry")
assert geometry is not None
def test_get_fields(config):
"""Test get_fields"""
expected_fields = {
"id": {"type": "NUMBER"},
"area": {"type": "NUMBER"},
"volume": {"type": "NUMBER"},
"name": {"type": "VARCHAR2"},
"wiki_link": {"type": "VARCHAR2"},
}
provider = OracleProvider(config)
assert provider.get_fields() == expected_fields
assert provider.fields == expected_fields
def test_get_fields_private_synonym(config_private_synonym):
"""Test get_fields from private synonym"""
expected_fields = {
"id": {"type": "NUMBER"},
"area": {"type": "NUMBER"},
"volume": {"type": "NUMBER"},
"name": {"type": "VARCHAR2"},
"wiki_link": {"type": "VARCHAR2"},
}
provider = OracleProvider(config_private_synonym)
assert provider.get_fields() == expected_fields
assert provider.fields == expected_fields
def test_get_fields_public_synonym(config_public_synonym):
"""Test get_fields from public synonym"""
expected_fields = {
"id": {"type": "NUMBER"},
"area": {"type": "NUMBER"},
"volume": {"type": "NUMBER"},
"name": {"type": "VARCHAR2"},
"wiki_link": {"type": "VARCHAR2"},
}
provider = OracleProvider(config_public_synonym)
assert provider.get_fields() == expected_fields
assert provider.fields == expected_fields
def test_get_fields_properties(config_properties):
"""
Test get_fields with subset of columns.
Test of property configuration.
"""
# NOTE: properties does not influence fields because
# the fields are also used for filtering
expected_fields = {
"id": {"type": "NUMBER"},
"name": {"type": "VARCHAR2"},
"wiki_link": {"type": "VARCHAR2"},
"area": {"type": "NUMBER"},
"volume": {"type": "NUMBER"},
}
provider = OracleProvider(config_properties)
provided_fields = provider.get_fields()
assert provided_fields == expected_fields
assert provider.fields == expected_fields
def test_query_with_property_filter(config):
"""Test query valid features when filtering by property"""
p = OracleProvider(config)
feature_collection = p.query(properties=[("name", "Aral Sea")])
features = feature_collection.get("features")
assert len(features) == 1
assert features[0].get("id") == 12
def test_query_with_extra_properties(config_extra_properties):
p = OracleProvider(config_extra_properties)
feature_collection = p.query(properties=[("name", "Aral Sea")])
features = feature_collection.get("features")
assert features[0]["properties"]["tooltip"] == "Here the name is Aral Sea!"
def test_query_bbox(config):
"""Test query with a specified bounding box"""
p = OracleProvider(config)
feature_collection = p.query(bbox=[50, 40, 60, 50])
features = feature_collection.get("features")
assert len(features) == 1
assert features[0]["properties"]["name"] == "Aral Sea"
def test_query_sortby(config):
"""Test query with sorting"""
p = OracleProvider(config)
up = p.query(sortby=[{"property": "id", "order": "+"}])
assert up["features"][0]["id"] == 1
down = p.query(sortby=[{"property": "id", "order": "-"}])
assert down["features"][0]["id"] == 25
name = p.query(sortby=[{"property": "name", "order": "+"}])
assert name["features"][0]["properties"]["name"] == "Aral Sea"
name = p.query(sortby=[{"property": "name", "order": "-"}])
assert name["features"][0]["properties"]["name"] == "Vänern"
def test_query_skip_geometry(config):
"""Test query without geometry"""
p = OracleProvider(config)
result = p.query(skip_geometry=True)
feature = result["features"][0]
assert feature.get("geometry") is None
def test_query_hits(config):
"""Test query number of hits"""
p = OracleProvider(config)
result = p.query(bbox=[0, 0, 70, 60], resulttype="hits")
assert result.get("numberMatched") == 5
def test_get(config):
"""Test simple get"""
p = OracleProvider(config)
result = p.get(5)
assert result.get("id") == 5
assert result.get("prev") == 4
assert result.get("next") == 6
def test_get_with_extra_properties(config_extra_properties):
"""Test simple get"""
p = OracleProvider(config_extra_properties)
result = p.get(5)
assert (
result["properties"]["tooltip"] ==
"Here the name is L. Erie!"
)
def test_create(config, create_geojson):
"""Test simple create"""
p = OracleProvider(config)
result = p.create(create_geojson)
assert result == 26
data = p.get(26)
assert data.get("properties").get("name") == "Lake Constance"
def test_update(config, update_geojson):
"""Test simple update"""
p = OracleProvider(config)
identifier = 26
result = p.update(identifier, update_geojson)
assert result
data = p.get(identifier)
assert data.get("properties").get("area") == 536000
assert data.get("properties").get("volume") == 48000
def test_update_properties(config_properties, config, update_geojson):
"""
Test update with filtered columnlist in configuration
In this case, the columns area and volume shouldn't be updated!
"""
p = OracleProvider(config_properties)
identifier = 26
update_geojson["properties"]["area"] = 42
update_geojson["properties"]["volume"] = 42
result = p.update(identifier, update_geojson)
assert result
p2 = OracleProvider(config)
data = p2.get(identifier)
assert data.get("properties").get("area") == 536000
assert data.get("properties").get("volume") == 48000
def test_delete(config):
"""Test simple delete"""
p = OracleProvider(config)
identifier = 26
result = p.delete(identifier)
assert result
down = p.query(sortby=[{"property": "id", "order": "-"}])
assert down["features"][0]["id"] == 25
def test_query_sql_manipulator(config_manipulator):
"""Test SQL manipulator"""
p = OracleProvider(config_manipulator)
feature_collection = p.query()
features = feature_collection.get("features")
assert len(features) == 1
assert features[0].get("id") == 10
def test_get_sql_manipulator(config_manipulator):
"""
Test get with SQL manipulator that throws
an authorization error.
"""
p = OracleProvider(config_manipulator)
with pytest.raises(Exception):
p.get(5)
def test_create_sql_manipulator(config_manipulator, config, create_geojson):
"""
Test create with SQL Manipulator call.
Field name should be overwritten with the string "overwritten"
"""
expected_identifier = 27
p = OracleProvider(config_manipulator)
result = p.create(create_geojson)
assert result == expected_identifier
p2 = OracleProvider(config)
data = p2.get(expected_identifier)
assert data.get("properties").get("name") == "overwritten"
def test_update_sql_manipulator(config_manipulator, config, update_geojson):
"""
Test update with SQL Manipulator call
Field names area and volume should be overwritten with the answer to
life the universe and everything
"""
identifier = 27
p = OracleProvider(config_manipulator)
result = p.update(identifier, update_geojson)
assert result
p2 = OracleProvider(config)
data = p2.get(identifier)
assert data.get("properties").get("area") == 42
assert data.get("properties").get("volume") == 42
def test_delete_sql_manipulator(config_manipulator, config):
"""
Test for delete with SQL Manipulator call
Where clause is overwritten by the manipulator to not
match to any record. No record should be deleted.
"""
identifier = 27
p = OracleProvider(config_manipulator)
result = p.delete(identifier)
assert not result
p2 = OracleProvider(config)
down = p2.query(sortby=[{"property": "id", "order": "-"}])
assert down["features"][0]["id"] == identifier
def test_create_point(config, create_point_geojson):
"""Test simple create"""
p = OracleProvider(config)
result = p.create(create_point_geojson)
assert result == 28
data = p.get(28)
assert data.get("geometry").get("type") == "Point"
def test_query_can_mandate_properties_which_are_not_returned(config):
config = {
**config,
# 'name' has to be filtered, but only 'wiki_link' is returned
"properties": ["id", "wiki_link"],
"mandatory_properties": ["name"]
}
p = OracleProvider(config)
result = p.query(properties=[("name", "Aral Sea")])
(feature,) = result['features']
# id is handled separately, so only wiki link and not name must be here
assert feature['properties'].keys() == {"wiki_link"}
def test_query_mandatory_properties_must_be_specified(config):
config = {
**config,
"mandatory_properties": ["name"]
}
p = OracleProvider(config)
with pytest.raises(ProviderInvalidQueryError):
p.query(properties=[("id", "123")])