forked from geopython/pycsw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util.py
More file actions
372 lines (324 loc) · 11.7 KB
/
test_util.py
File metadata and controls
372 lines (324 loc) · 11.7 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
# =================================================================
#
# Authors: Ricardo Garcia Silva <ricardo.garcia.silva@gmail.com>
#
# Copyright (c) 2017 Ricardo Garcia Silva
#
# 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.
#
# =================================================================
"""Unit tests for pycsw.core.util"""
import datetime as dt
import os
import time
from pathlib import Path
from unittest import mock
import pytest
from shapely.wkt import loads
from pycsw.core import util
pytestmark = pytest.mark.unit
def test_get_today_and_now():
fake_now = "2017-01-01T00:00:00Z"
with mock.patch.object(util.time, "localtime") as mock_localtime:
mock_localtime.return_value = time.strptime(
fake_now,
"%Y-%m-%dT%H:%M:%SZ"
)
result = util.get_today_and_now()
assert result == fake_now
@pytest.mark.parametrize("value, expected", [
(dt.date(2017, 1, 23), "2017-01-23"),
(dt.datetime(2017, 1, 23), "2017-01-23"),
(dt.datetime(2017, 1, 23, 20, 32, 10), "2017-01-23T20:32:10Z"),
(dt.datetime(2017, 1, 23, 10), "2017-01-23T10:00:00Z"),
(dt.datetime(2017, 1, 23, 10, 20), "2017-01-23T10:20:00Z"),
])
def test_datetime2iso8601(value, expected):
result = util.datetime2iso8601(value)
assert result == expected
@pytest.mark.parametrize("version, expected", [
("2", -1),
("1.2", -1),
("5.4.3.2", -1),
("This is a regular string, not a version", -1),
("3.4.1", 30401),
])
def test_get_version_integer(version, expected):
result = util.get_version_integer(version)
assert result == expected
@pytest.mark.parametrize("invalid_version", [
2,
2.2,
None,
])
def test_get_version_integer_invalid_version(invalid_version):
with pytest.raises(RuntimeError):
util.get_version_integer(invalid_version)
@pytest.mark.parametrize("xpath_expression, expected", [
("ns1:first", "{something}first"),
("ns1:first/ns2:second", "{something}first/{other}second"),
("ns1:first/ns2:second[1]", "{something}first/{other}second[1]"),
("ns1:first/*/ns3:third", "{something}first/*/{another}third"),
("", ""),
])
def test_nspath_eval(xpath_expression, expected):
nsmap = {
"ns1": "something",
"ns2": "other",
"ns3": "another",
}
result = util.nspath_eval(xpath_expression, nsmap)
assert result == expected
def test_nspath_eval_invalid_element():
with pytest.raises(RuntimeError):
util.nspath_eval(
xpath="ns1:tag1/ns2:ns3:tag2",
nsmap={
"ns1": "something",
"ns2": "other",
"ns3": "another",
}
)
@pytest.mark.parametrize("envelope, expected", [
("ENVELOPE (-180,180,90,-90)", "-180,-90,180,90"),
(" ENVELOPE(-180,180,90,-90)", "-180,-90,180,90"),
(" ENVELOPE( -180, 180, 90, -90) ", "-180,-90,180,90"),
])
def test_wktenvelope2bbox(envelope, expected):
result = util.wktenvelope2bbox(envelope)
assert result == expected
# TODO - Add more WKT cases for other geometry types
@pytest.mark.parametrize("wkt, bounds, expected", [
("POINT (10 10)", True, (10.0, 10.0, 10.0, 10.0)),
("SRID=4326;POINT (10 10)", True, (10.0, 10.0, 10.0, 10.0)),
("POINT (10 10)", False, loads("POINT (10 10)")),
("SRID=4326;POINT (10 10)", False, loads("POINT (10 10)")),
])
def test_wkt2geom(wkt, bounds, expected):
result = util.wkt2geom(wkt, bounds=bounds)
assert result == expected
@pytest.mark.parametrize("bbox, expected", [
(
"0.0, 10.0, 30.0, 15.0",
"POLYGON((0.00 10.00, 0.00 15.00, 30.00 15.00, "
"30.00 10.00, 0.00 10.00))"
),
(
"-10.0, 10.0, 30.0, 15.0",
"POLYGON((-10.00 10.00, -10.00 15.00, 30.00 15.00, "
"30.00 10.00, -10.00 10.00))"
),
])
def test_bbox2wktpolygon(bbox, expected):
result = util.bbox2wktpolygon(bbox)
assert result == expected
def test_transform_mappings():
queryables = {
"q1": {"xpath": "p1", "dbcol": "col1"},
"q2": {"xpath": "p2", "dbcol": "col2"},
}
typename = {"q2": "q1"}
duplicate_queryables = queryables.copy()
duplicate_typename = typename.copy()
util.transform_mappings(duplicate_queryables, duplicate_typename)
assert duplicate_queryables["q1"]["xpath"] == queryables["q2"]["xpath"]
assert duplicate_queryables["q1"]["dbcol"] == queryables["q2"]["dbcol"]
@pytest.mark.parametrize("name, value, expected", [
("name", "john", "john"),
("date", dt.date(2017, 1, 1), "2017-01-01"),
("datetime", dt.datetime(2017, 1, 1, 10, 5), "2017-01-01T10:05:00Z"),
("some_callable", os.getcwd, os.getcwd()),
])
def test_getqattr_no_link(name, value, expected):
class Phony(object):
pass
instance = Phony()
setattr(instance, name, value)
result = util.getqattr(instance, name)
assert result == expected
def test_getqattr_link():
some_object = mock.MagicMock()
some_object.some_link.return_value = [
["one", "two"],
["three", "four"],
]
result = util.getqattr(some_object, "some_link")
assert result == "one,two^three,four"
def test_getqattr_invalid():
result = util.getqattr(dt.date(2017, 1, 1), "name")
assert result is None
def test_http_request_post():
# here we replace owslib.util.http_post with a mock object
# because we are not interested in testing owslib
method = "POST"
url = "some_phony_url"
request = "some_phony_request"
timeout = 40
with mock.patch("pycsw.core.util.http_post",
autospec=True) as mock_http_post:
util.http_request(
method=method,
url=url,
request=request,
timeout=timeout
)
mock_http_post.assert_called_with(url, request, timeout=timeout)
@pytest.mark.parametrize("url, expected", [
("http://host/wms", "http://host/wms?"),
("http://host/wms?foo=bar&", "http://host/wms?foo=bar&"),
("http://host/wms?foo=bar", "http://host/wms?foo=bar&"),
("http://host/wms?", "http://host/wms?"),
("http://host/wms?foo", "http://host/wms?foo&"),
])
def test_bind_url(url, expected):
result = util.bind_url(url)
assert result == expected
@pytest.mark.parametrize("ip, netmask, expected", [
("192.168.100.14", "192.168.100.0/24", True),
("192.168.100.14", "192.168.0.0/24", False),
("192.168.100.14", "192.168.0.0/16", True),
])
def test_ip_in_network_cidr(ip, netmask, expected):
result = util.ip_in_network_cidr(ip, netmask)
assert result == expected
@pytest.mark.parametrize("ip, whitelist, expected", [
("192.168.100.14", [], False),
("192.168.100.14", ["192.168.100.15"], False),
("192.168.100.14", ["192.168.100.15", "192.168.100.14"], True),
("192.168.100.14", ["192.168.100.*"], True),
("192.168.100.14", ["192.168.100.15", "192.168.100.*"], True),
("192.168.100.14", ["192.168.100.0/24"], True),
("192.168.100.14", ["192.168.100.15", "192.168.100.0/24"], True),
("192.168.10.14", ["192.168.100.15", "192.168.0.0/16"], True),
])
def test_ipaddress_in_whitelist(ip, whitelist, expected):
result = util.ipaddress_in_whitelist(ip, whitelist)
assert result == expected
@pytest.mark.parametrize("linkstr, expected", [
# old style CSV
("roads,my roads,OGC:WMS,http://example.org/wms",
[{
"name": "roads",
"description": "my roads",
"protocol": "OGC:WMS",
"url": "http://example.org/wms"
}]
),
# old style CSV with some empty tokens
(",,,http://example.org/wms",
[{
"name": None,
"description": None,
"protocol": None,
"url": "http://example.org/wms"
}]
),
# old style CSV with empty tokens
(",,,",
[{
"name": None,
"description": None,
"protocol": None,
"url": None
}]
),
# old style CSV with 2 links
("roads,my roads,OGC:WMS,http://example.org/wms^roads,my roads,OGC:WFS,http://example.org/wfs",
[{
"name": "roads",
"description": "my roads",
"protocol": "OGC:WMS",
"url": "http://example.org/wms"
}, {
"name": "roads",
"description": "my roads",
"protocol": "OGC:WFS",
"url": "http://example.org/wfs"
}]
),
# JSON style
('[{"name": "roads", "description": "my roads", "protocol": "OGC:WMS", "url": "http://example.org/wms"}]',
[{
"name": "roads",
"description": "my roads",
"protocol": "OGC:WMS",
"url": "http://example.org/wms"
}]
),
# JSON style with some empty keys
('[{"name": "roads", "description": null, "protocol": "OGC:WMS", "url": "http://example.org/wms"}]',
[{
"name": "roads",
"description": None,
"protocol": "OGC:WMS",
"url": "http://example.org/wms"
}]
),
# JSON style with multiple links
('[{"name": "roads", "description": null, "protocol": "OGC:WMS", "url": "http://example.org/wms"},'
'{"name": "roads", "description": null, "protocol": "OGC:WFS", "url": "http://example.org/wfs"}]',
[{
"name": "roads",
"description": None,
"protocol": "OGC:WMS",
"url": "http://example.org/wms"
}, {
"name": "roads",
"description": None,
"protocol": "OGC:WFS",
"url": "http://example.org/wfs"
}]
)
])
def test_jsonify_links(linkstr, expected):
result = util.jsonify_links(linkstr)
assert isinstance(result, list)
assert result == expected
@pytest.mark.parametrize("value, result", [
("foo", False),
(None, True),
('', True),
(' ', True),
(' ', True),
])
def test_is_none_or_empty(value, result):
assert util.is_none_or_empty(value) is result
@pytest.mark.parametrize("import_path, expected_attribute", [
pytest.param("itertools", "count", id="import from stdlib"),
pytest.param("pycsw.core.repository", "setup", id="dotted path import from pycsw"),
pytest.param(__file__, "test_programmatic_import", id="filesystem path import"),
])
def test_programmatic_import(import_path, expected_attribute):
imported_module = util.programmatic_import(import_path)
assert getattr(imported_module, expected_attribute)
@pytest.mark.parametrize("invalid_import_path", [
"dummy",
"dummy.submodule",
"/non-existent/path",
str(Path(__file__).parent / "invalid_path"),
])
def test_programmatic_import_with_invalid_path(invalid_import_path):
result = util.programmatic_import(invalid_import_path)
assert result is None
def test_sanitize_url():
result = util.sanitize_db_connect("postgresql://username:password@localhost/pycsw")
assert result == "postgresql://***:***@localhost/pycsw"