forked from craigloftus/wikipedia-osm-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
272 lines (229 loc) · 8.25 KB
/
tests.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
import unittest
import wikipedia_osm_check
class TestSanitise(unittest.TestCase):
def setUp(self):
self.obj = wikipedia_osm_check.WikipediaOSMCheck('en')
def test_dot_removal(self):
dirty = 'foo.bar.fish.'
self.assertEqual(self.obj._sanitise_name(dirty), 'foobarfish')
def test_lowercase(self):
dirty = 'FooBarFISH'
self.assertEqual(self.obj._sanitise_name(dirty), 'foobarfish')
def test_comma_stripping(self):
dirty = 'Foo, Bar'
self.assertEqual(self.obj._sanitise_name(dirty), 'foo')
def test_multi_comma(self):
dirty = 'Foo, Bar, Fish'
self.assertEqual(self.obj._sanitise_name(dirty), 'foo')
class TestPlaceNameFinding(unittest.TestCase):
def setUp(self):
self.obj = wikipedia_osm_check.WikipediaOSMCheck('en')
def test_nothing(self):
place = {'id':12234, 'tags':{}}
self.assertEqual(self.obj._find_names(place), [])
def test_name_tag(self):
place = {'tags': {'name': 'fish'}}
self.assertEqual(self.obj._find_names(place), ['fish'])
def test_place_name_tag(self):
place = {'tags': {'place_name': 'fish'}}
self.assertEqual(self.obj._find_names(place), ['fish'])
def test_multiple_names(self):
place = {'tags': {'name': 'fish', 'place_name': 'dog', 'alt_name': 'horse'}}
self.assertEqual(self.obj._find_names(place), ['fish', 'dog', 'horse'])
def test_splitting_names(self):
place = {'tags': {'name': 'fish; bat', 'place_name': 'dog'}}
self.assertEqual(self.obj._find_names(place), ['fish', ' bat', 'dog'])
class TestPlaceComparison(unittest.TestCase):
def setUp(self):
self.obj = wikipedia_osm_check.WikipediaOSMCheck('en')
def test_empty_sets(self):
missing = self.obj.find_missing([], [])
self.assertEqual(missing, set([]))
def test_empty_existing(self):
places = [
'tusmore',
'aston view',
'smiths'
]
missing = self.obj.find_missing(places, [])
self.assertEqual(missing, set(places))
def test_empty_places(self):
existing = [
'tusmore',
'sandford on thames',
'broughton poggs'
]
missing = self.obj.find_missing([], existing)
self.assertEqual(missing, set([]))
def test_missing(self):
places = [
'tusmore',
'aston view',
'smiths'
]
existing = [
'tusmore',
'sandford on thames',
'broughton poggs',
'aston view',
'smiths estate'
]
missing = self.obj.find_missing(places, existing)
self.assertEqual(missing, set(['smiths']))
class MockWikipediaOSMCheck(wikipedia_osm_check.WikipediaOSMCheck):
def _request_category(self, category_name):
return self.mock_data
def _request_existing(self, region_name):
return self.mock_data
class TestLoadWikipediaNames(unittest.TestCase):
def setUp(self):
self.obj = MockWikipediaOSMCheck('en')
def test_loading_names(self):
self.obj.mock_data = {
"limits":{"categorymembers":500},
"query":{"categorymembers":[
{"ns":0,"title": "Milton-under-Wychwood"},
{"ns":0,"title": "Tiddington, Oxfordshire"},
{"ns":0,"title":"Islip, Oxfordshire"},
{"ns":0,"title":"Hardwick, West Oxfordshire"}
]}
}
names = self.obj.load_wikipedia_names('Category')
self.assertEqual(names, ['milton under wychwood', 'tiddington', 'islip', 'hardwick'])
def test_no_members(self):
self.obj.mock_data = {
"limits":{"categorymembers":500},
"query":{"categorymembers":[
]}
}
names = self.obj.load_wikipedia_names('Category')
self.assertEqual(names, [])
class TestLoadOverpassElements(unittest.TestCase):
def setUp(self):
self.obj = MockWikipediaOSMCheck('en')
def test_load_elements(self):
self.obj.mock_data = [
{
"type": "node",
"id": 814072,
"lat": 51.5845416,
"lon": -1.0913475,
"tags": {
"name": "Cart Gap",
"place": "locality",
"source": "NPE"
}
},
{
"type": "node",
"id": 2510592,
"lat": 51.5375786,
"lon": -0.9050287,
"tags": {
"name": "Henley-on-Thames",
"place": "town"
}
},
{
"type": "node",
"id": 5287295,
"lat": 51.8224193,
"lon": -1.3175230,
"tags": {
"is_in": "Oxfordshire, England, UK",
"name": "Begbroke",
"place": "village"
}
}]
out = self.obj.load_existing_names('region')
expected = ['cart gap', 'henley on thames', 'begbroke']
self.assertEqual(out, expected)
def test_no_elements(self):
self.obj.mock_data = []
self.assertEqual(self.obj.load_existing_names('region'), [])
class MockRequestWikipediaOSMCheck(wikipedia_osm_check.WikipediaOSMCheck):
def _request(self, *args, **kwargs):
return self.mock_data
class TestRequestExisting(unittest.TestCase):
def setUp(self):
self.obj = MockRequestWikipediaOSMCheck('en')
def test_request_existing(self):
self.obj.mock_data = {
"version": 0.6,
"generator": "Overpass API",
"osm3s": {
"timestamp_osm_base": "2013-04-11T10:39:04Z",
"timestamp_areas_base": "2013-04-11T04:42:03Z",
"copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
},
"elements": [
{
"type": "node",
"id": 814072,
"lat": 51.5845416,
"lon": -1.0913475,
"tags": {
"name": "Cart Gap",
"place": "locality",
"source": "NPE"
}
},
{
"type": "node",
"id": 2510592,
"lat": 51.5375786,
"lon": -0.9050287,
"tags": {
"name": "Henley-on-Thames",
"place": "town"
}
},
{
"type": "node",
"id": 5287295,
"lat": 51.8224193,
"lon": -1.3175230,
"tags": {
"is_in": "Oxfordshire, England, UK",
"name": "Begbroke",
"place": "village"
}
}
]}
out = self.obj._request_existing('Region')
self.assertEqual(out, self.obj.mock_data['elements'])
class MockRunWikipediaOSMCheck(wikipedia_osm_check.WikipediaOSMCheck):
def report(self):
return
def load_wikipedia_names(self, category_name):
return self.mock_places
def load_existing_names(self, region_name, types=None):
return self.mock_existing
class TestRun(unittest.TestCase):
def setUp(self):
self.obj = MockRunWikipediaOSMCheck('en')
def test_empty_places(self):
import sys
import StringIO
self.obj.mock_places = []
out = StringIO.StringIO()
sys.stdout = out
self.assertEqual(self.obj.run('category', 'region'), None)
self.assertEqual(out.getvalue().strip(), "No places found in category")
def test_empty_existing(self):
import sys
import StringIO
self.obj.mock_places = ['fish', 'dog', 'horse']
self.obj.mock_existing = []
out = StringIO.StringIO()
sys.stdout = out
self.obj.run('category', 'region')
self.assertEqual(out.getvalue().strip(), "No existing places found in region")
self.assertEqual(self.obj.missing, set(self.obj.mock_places))
def test_run(self):
self.obj.mock_places = ['fish', 'dog', 'horse']
self.obj.mock_existing = ['dog']
self.obj.run('category', 'region')
self.assertEqual(self.obj.missing, set(['fish', 'horse']))
if __name__ == '__main__':
unittest.main()