1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# ~*~ coding: utf-8 ~*~ |
3
|
|
|
#- |
4
|
|
|
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge |
5
|
|
|
# Copyright (c) 2016 Dominik George <[email protected]> |
6
|
|
|
# |
7
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
# in the Software without restriction, including without limitation the rights |
10
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
# furnished to do so, subject to the following conditions: |
13
|
|
|
# |
14
|
|
|
# The above copyright notice and this permission notice shall be included in all |
15
|
|
|
# copies or substantial portions of the Software. |
16
|
|
|
# |
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
# SOFTWARE. |
24
|
|
|
# |
25
|
|
|
# Alternatively, you are free to use OSMAlchemy under Simplified BSD, The |
26
|
|
|
# MirOS Licence, GPL-2+, LGPL-2.1+, AGPL-3+ or the same terms as Python |
27
|
|
|
# itself. |
28
|
|
|
|
29
|
|
|
""" Tests concerning OSMAlchemy online utility code. """ |
30
|
|
|
|
31
|
|
|
# Standard unit testing framework |
32
|
|
|
import unittest |
33
|
|
|
|
34
|
|
|
class OSMAlchemyUtilOnlineSQLToOverpassQLTests(unittest.TestCase): |
35
|
|
|
""" Tests for SQL to overpass conversion """ |
36
|
|
|
|
37
|
|
|
def test_trees_to_overpassql_bbox_node(self): |
38
|
|
|
# Import function to test |
39
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
40
|
|
|
|
41
|
|
|
# Test input |
42
|
|
|
tree = {"OSMNode": ("&&", [(">", "latitude", 51.0), ("<", "latitude", 52.0), |
43
|
|
|
(">", "longitude", 7.0), ("<", "longitude", 8.0)])} |
44
|
|
|
|
45
|
|
|
# Expected result regex |
46
|
|
|
expected = r"^node\(51\.0,7\.0,52\.0,8\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
47
|
|
|
|
48
|
|
|
# Run transformation |
49
|
|
|
res = _trees_to_overpassql(tree) |
50
|
|
|
|
51
|
|
|
# Check result |
52
|
|
|
self.assertRegexpMatches(res, expected) |
53
|
|
|
|
54
|
|
|
def test_trees_to_overpassql_bbox_node_missing_east(self): |
55
|
|
|
# Import function to test |
56
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
57
|
|
|
|
58
|
|
|
# Test input |
59
|
|
|
tree = {"OSMNode": ("&&", [(">", "latitude", 51.0), ("<", "latitude", 52.0), |
60
|
|
|
(">", "longitude", 7.0)])} |
61
|
|
|
|
62
|
|
|
# Expected result regex |
63
|
|
|
expected = r"^node\(51\.0,7\.0,52\.0,180\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
64
|
|
|
|
65
|
|
|
# Run transformation |
66
|
|
|
res = _trees_to_overpassql(tree) |
67
|
|
|
|
68
|
|
|
# Check result |
69
|
|
|
self.assertRegexpMatches(res, expected) |
70
|
|
|
|
71
|
|
|
def test_trees_to_overpassql_bbox_node_missing_west(self): |
72
|
|
|
# Import function to test |
73
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
74
|
|
|
|
75
|
|
|
# Test input |
76
|
|
|
tree = {"OSMNode": ("&&", [(">", "latitude", 51.0), ("<", "latitude", 52.0), |
77
|
|
|
("<", "longitude", 8.0)])} |
78
|
|
|
|
79
|
|
|
# Expected result regex |
80
|
|
|
expected = r"^node\(51\.0,-180\.0,52\.0,8\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
81
|
|
|
|
82
|
|
|
# Run transformation |
83
|
|
|
res = _trees_to_overpassql(tree) |
84
|
|
|
|
85
|
|
|
# Check result |
86
|
|
|
self.assertRegexpMatches(res, expected) |
87
|
|
|
|
88
|
|
|
def test_trees_to_overpassql_bbox_node_missing_north(self): |
89
|
|
|
# Import function to test |
90
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
91
|
|
|
|
92
|
|
|
# Test input |
93
|
|
|
tree = {"OSMNode": ("&&", [(">", "latitude", 51.0), |
94
|
|
|
(">", "longitude", 7.0), ("<", "longitude", 8.0)])} |
95
|
|
|
|
96
|
|
|
# Expected result regex |
97
|
|
|
expected = r"^node\(51\.0,7\.0,90\.0,8\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
98
|
|
|
|
99
|
|
|
# Run transformation |
100
|
|
|
res = _trees_to_overpassql(tree) |
101
|
|
|
|
102
|
|
|
# Check result |
103
|
|
|
self.assertRegexpMatches(res, expected) |
104
|
|
|
|
105
|
|
|
def test_trees_to_overpassql_bbox_node_missing_south(self): |
106
|
|
|
# Import function to test |
107
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
108
|
|
|
|
109
|
|
|
# Test input |
110
|
|
|
tree = {"OSMNode": ("&&", [("<", "latitude", 52.0), |
111
|
|
|
(">", "longitude", 7.0), ("<", "longitude", 8.0)])} |
112
|
|
|
|
113
|
|
|
# Expected result regex |
114
|
|
|
expected = r"^node\(-90\.0,7\.0,52\.0,8\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
115
|
|
|
|
116
|
|
|
# Run transformation |
117
|
|
|
res = _trees_to_overpassql(tree) |
118
|
|
|
|
119
|
|
|
# Check result |
120
|
|
|
self.assertRegexpMatches(res, expected) |
121
|
|
|
|
122
|
|
|
def test_trees_to_overpassql_id_node(self): |
123
|
|
|
# Import function to test |
124
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
125
|
|
|
|
126
|
|
|
# Test input |
127
|
|
|
tree = {"OSMNode": ("==", "id", 1145698)} |
128
|
|
|
|
129
|
|
|
# Expected result regex |
130
|
|
|
expected = r"^node\(1145698\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
131
|
|
|
|
132
|
|
|
# Run transformation |
133
|
|
|
res = _trees_to_overpassql(tree) |
134
|
|
|
|
135
|
|
|
# Check result |
136
|
|
|
self.assertRegexpMatches(res, expected) |
137
|
|
|
|
138
|
|
|
def test_trees_to_overpassql_id_node_invalid(self): |
139
|
|
|
# Import function to test |
140
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
141
|
|
|
|
142
|
|
|
# Test input |
143
|
|
|
tree = {"OSMNode": (">", "id", 1145698)} |
144
|
|
|
|
145
|
|
|
# Expect it to throw an exception |
146
|
|
|
with self.assertRaises(ValueError): |
147
|
|
|
# Run transformation |
148
|
|
|
res = _trees_to_overpassql(tree) |
149
|
|
|
|
150
|
|
|
def test_trees_to_overpassql_two_tags_node(self): |
151
|
|
|
# Import function to test |
152
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
153
|
|
|
|
154
|
|
|
# Test input |
155
|
|
|
tree = {"OSMNode": ("&&", [("==", "amenity", "pub"), ("==", "name", "Bruchbude")])} |
156
|
|
|
|
157
|
|
|
# Expected result regex |
158
|
|
|
expected = r"^node\[\"amenity\"=\"pub\"\]\[\"name\"=\"Bruchbude\"\]->\.s[0-9]+;\(\.s[0-9]+;\);$" |
159
|
|
|
|
160
|
|
|
# Run transformation |
161
|
|
|
res = _trees_to_overpassql(tree) |
162
|
|
|
|
163
|
|
|
# Check result |
164
|
|
|
self.assertRegexpMatches(res, expected) |
165
|
|
|
|
166
|
|
|
def test_trees_to_overpassql_any_of_two_tags_node(self): |
167
|
|
|
# Import function to test |
168
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
169
|
|
|
|
170
|
|
|
# Test input |
171
|
|
|
tree = {"OSMNode": ("||", [("==", "amenity", "pub"), ("==", "name", "Bruchbude")])} |
172
|
|
|
|
173
|
|
|
# Expected result regex |
174
|
|
|
expected = r"^node\[\"amenity\"=\"pub\"\]->\.s[0-9]+;node\[\"name\"=\"Bruchbude\"\]->\.s[0-9]+;\(\.s[0-9]+;\.s[0-9]+;\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
175
|
|
|
|
176
|
|
|
# Run transformation |
177
|
|
|
res = _trees_to_overpassql(tree) |
178
|
|
|
|
179
|
|
|
# Check result |
180
|
|
|
self.assertRegexpMatches(res, expected) |
181
|
|
|
|
182
|
|
|
def test_trees_to_overpassql_two_tags_in_bbox_node(self): |
183
|
|
|
# Import function to test |
184
|
|
|
from osmalchemy.util.online import _trees_to_overpassql |
185
|
|
|
|
186
|
|
|
# Test input |
187
|
|
|
tree = {"OSMNode": ("&&", [("==", "amenity", "pub"), ("==", "name", "Bruchbude"), |
188
|
|
|
(">", "latitude", 51.0), ("<", "latitude", 52.0), |
189
|
|
|
(">", "longitude", 7.0), ("<", "longitude", 8.0)])} |
190
|
|
|
|
191
|
|
|
# Expected result regex |
192
|
|
|
expected = r"^node\[\"amenity\"=\"pub\"\]\[\"name\"=\"Bruchbude\"\]\(51\.0,7\.0,52\.0,8\.0\)->\.s[0-9]+;\(\.s[0-9]+;\);$" |
193
|
|
|
|
194
|
|
|
# Run transformation |
195
|
|
|
res = _trees_to_overpassql(tree) |
196
|
|
|
|
197
|
|
|
# Check result |
198
|
|
|
self.assertRegexpMatches(res, expected) |
199
|
|
|
|
200
|
|
|
def test_normalise_overpassql(self): |
201
|
|
|
# Import function to test |
202
|
|
|
from osmalchemy.util.online import _normalise_overpassql |
203
|
|
|
|
204
|
|
|
# Test string |
205
|
|
|
oql = 'node(50.0,6.0,51.0,8.0)->.s1875312;node["name"="Schwarzreindorf Kirche"]->.s95682773;(.s1875312; .s95682773)->.s173859;.s173859 out meta;' |
206
|
|
|
|
207
|
|
|
# Expected result |
208
|
|
|
normalised_oql = 'node(50.0,6.0,51.0,8.0)->.s;node["name"="Schwarzreindorf Kirche"]->.s;(.s; .s)->.s;.s out meta;' |
209
|
|
|
|
210
|
|
|
# Run function |
211
|
|
|
res = _normalise_overpassql(oql) |
212
|
|
|
|
213
|
|
|
# Check result |
214
|
|
|
self.assertEqual(res, normalised_oql) |
215
|
|
|
|
216
|
|
|
def test_normalise_overpassql_with_catchy_string(self): |
217
|
|
|
# Import function to test |
218
|
|
|
from osmalchemy.util.online import _normalise_overpassql |
219
|
|
|
|
220
|
|
|
# Test string |
221
|
|
|
oql = 'node(50.0,6.0,51.0,8.0)->.s1875312;node["name"="Whatever.s192837465"]->.s95682773;(.s1875312; .s95682773)->.s173859;.s173859 out meta;' |
222
|
|
|
|
223
|
|
|
# Expected result |
224
|
|
|
normalised_oql = 'node(50.0,6.0,51.0,8.0)->.s;node["name"="Whatever.s192837465"]->.s;(.s; .s)->.s;.s out meta;' |
225
|
|
|
|
226
|
|
|
# Run function |
227
|
|
|
res = _normalise_overpassql(oql) |
228
|
|
|
|
229
|
|
|
# Check result |
230
|
|
|
self.assertEqual(res, normalised_oql) |
231
|
|
|
|
232
|
|
|
# Make runnable as standalone script |
233
|
|
|
if __name__ == "__main__": |
234
|
|
|
unittest.main() |
235
|
|
|
|