瀏覽代碼

Openstreetmap's unit test

Cqoicebordel 10 年之前
父節點
當前提交
d0a1df881a
共有 3 個檔案被更改,包括 205 行新增4 行删除
  1. 5
    4
      searx/engines/openstreetmap.py
  2. 199
    0
      searx/tests/engines/test_openstreetmap.py
  3. 1
    0
      searx/tests/test_engines.py

+ 5
- 4
searx/engines/openstreetmap.py 查看文件

@@ -38,6 +38,9 @@ def response(resp):
38 38
 
39 39
     # parse results
40 40
     for r in json:
41
+        if 'display_name' not in r:
42
+            continue
43
+
41 44
         title = r['display_name']
42 45
         osm_type = r.get('osm_type', r.get('type'))
43 46
         url = result_base_url.format(osm_type=osm_type,
@@ -49,10 +52,8 @@ def response(resp):
49 52
         geojson = r.get('geojson')
50 53
 
51 54
         # if no geojson is found and osm_type is a node, add geojson Point
52
-        if not geojson and\
53
-           osm_type == 'node':
54
-            geojson = {u'type': u'Point',
55
-                       u'coordinates': [r['lon'], r['lat']]}
55
+        if not geojson and osm_type == 'node':
56
+            geojson = {u'type': u'Point', u'coordinates': [r['lon'], r['lat']]}
56 57
 
57 58
         address_raw = r.get('address')
58 59
         address = {}

+ 199
- 0
searx/tests/engines/test_openstreetmap.py 查看文件

@@ -0,0 +1,199 @@
1
+# -*- coding: utf-8 -*-
2
+from collections import defaultdict
3
+import mock
4
+from searx.engines import openstreetmap
5
+from searx.testing import SearxTestCase
6
+
7
+
8
+class TestOpenstreetmapEngine(SearxTestCase):
9
+
10
+    def test_request(self):
11
+        query = 'test_query'
12
+        dicto = defaultdict(dict)
13
+        dicto['pageno'] = 1
14
+        params = openstreetmap.request(query, dicto)
15
+        self.assertIn('url', params)
16
+        self.assertIn(query, params['url'])
17
+        self.assertIn('openstreetmap.org', params['url'])
18
+
19
+    def test_response(self):
20
+        self.assertRaises(AttributeError, openstreetmap.response, None)
21
+        self.assertRaises(AttributeError, openstreetmap.response, [])
22
+        self.assertRaises(AttributeError, openstreetmap.response, '')
23
+        self.assertRaises(AttributeError, openstreetmap.response, '[]')
24
+
25
+        response = mock.Mock(text='{}')
26
+        self.assertEqual(openstreetmap.response(response), [])
27
+
28
+        response = mock.Mock(text='{"data": []}')
29
+        self.assertEqual(openstreetmap.response(response), [])
30
+
31
+        json = """
32
+        [
33
+          {
34
+            "place_id": "127732055",
35
+            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
36
+            "osm_type": "relation",
37
+            "osm_id": "7444",
38
+            "boundingbox": [
39
+              "48.8155755",
40
+              "48.902156",
41
+              "2.224122",
42
+              "2.4697602"
43
+            ],
44
+            "lat": "48.8565056",
45
+            "lon": "2.3521334",
46
+            "display_name": "This is the title",
47
+            "class": "place",
48
+            "type": "city",
49
+            "importance": 0.96893459932191,
50
+            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
51
+            "address": {
52
+              "city": "Paris",
53
+              "county": "Paris",
54
+              "state": "Île-de-France",
55
+              "country": "France",
56
+              "country_code": "fr"
57
+            },
58
+            "geojson": {
59
+              "type": "Polygon",
60
+              "coordinates": [
61
+                [
62
+                  [
63
+                    2.224122,
64
+                    48.854199
65
+                  ]
66
+                ]
67
+              ]
68
+            }
69
+          }
70
+        ]
71
+        """
72
+        response = mock.Mock(text=json)
73
+        results = openstreetmap.response(response)
74
+        self.assertEqual(type(results), list)
75
+        self.assertEqual(len(results), 1)
76
+        self.assertEqual(results[0]['title'], 'This is the title')
77
+        self.assertEqual(results[0]['url'], 'https://openstreetmap.org/relation/7444')
78
+        self.assertIn('coordinates', results[0]['geojson'])
79
+        self.assertEqual(results[0]['geojson']['coordinates'][0][0][0], 2.224122)
80
+        self.assertEqual(results[0]['geojson']['coordinates'][0][0][1], 48.854199)
81
+        self.assertEqual(results[0]['address'], None)
82
+        self.assertIn('48.8155755', results[0]['boundingbox'])
83
+        self.assertIn('48.902156', results[0]['boundingbox'])
84
+        self.assertIn('2.224122', results[0]['boundingbox'])
85
+        self.assertIn('2.4697602', results[0]['boundingbox'])
86
+
87
+        json = """
88
+        [
89
+          {
90
+            "place_id": "127732055",
91
+            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
92
+            "osm_type": "relation",
93
+            "osm_id": "7444",
94
+            "boundingbox": [
95
+              "48.8155755",
96
+              "48.902156",
97
+              "2.224122",
98
+              "2.4697602"
99
+            ],
100
+            "lat": "48.8565056",
101
+            "lon": "2.3521334",
102
+            "display_name": "This is the title",
103
+            "class": "tourism",
104
+            "type": "city",
105
+            "importance": 0.96893459932191,
106
+            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
107
+            "address": {
108
+              "city": "Paris",
109
+              "county": "Paris",
110
+              "state": "Île-de-France",
111
+              "country": "France",
112
+              "country_code": "fr",
113
+              "address29": "Address"
114
+            },
115
+            "geojson": {
116
+              "type": "Polygon",
117
+              "coordinates": [
118
+                [
119
+                  [
120
+                    2.224122,
121
+                    48.854199
122
+                  ]
123
+                ]
124
+              ]
125
+            }
126
+          },
127
+          {
128
+            "place_id": "127732055",
129
+            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
130
+            "osm_type": "relation",
131
+            "osm_id": "7444",
132
+            "boundingbox": [
133
+              "48.8155755",
134
+              "48.902156",
135
+              "2.224122",
136
+              "2.4697602"
137
+            ],
138
+            "lat": "48.8565056",
139
+            "lon": "2.3521334",
140
+            "display_name": "This is the title",
141
+            "class": "tourism",
142
+            "type": "city",
143
+            "importance": 0.96893459932191,
144
+            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
145
+            "address": {
146
+              "city": "Paris",
147
+              "county": "Paris",
148
+              "state": "Île-de-France",
149
+              "country": "France",
150
+              "postcode": 75000,
151
+              "country_code": "fr"
152
+            },
153
+            "geojson": {
154
+              "type": "Polygon",
155
+              "coordinates": [
156
+                [
157
+                  [
158
+                    2.224122,
159
+                    48.854199
160
+                  ]
161
+                ]
162
+              ]
163
+            }
164
+          },
165
+          {
166
+            "place_id": "127732055",
167
+            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
168
+            "osm_type": "node",
169
+            "osm_id": "7444",
170
+            "boundingbox": [
171
+              "48.8155755",
172
+              "48.902156",
173
+              "2.224122",
174
+              "2.4697602"
175
+            ],
176
+            "lat": "48.8565056",
177
+            "lon": "2.3521334",
178
+            "display_name": "This is the title",
179
+            "class": "tourism",
180
+            "type": "city",
181
+            "importance": 0.96893459932191,
182
+            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
183
+            "address": {
184
+              "city": "Paris",
185
+              "county": "Paris",
186
+              "state": "Île-de-France",
187
+              "country": "France",
188
+              "country_code": "fr",
189
+              "address29": "Address"
190
+            }
191
+          }
192
+        ]
193
+        """
194
+        response = mock.Mock(text=json)
195
+        results = openstreetmap.response(response)
196
+        self.assertEqual(type(results), list)
197
+        self.assertEqual(len(results), 3)
198
+        self.assertIn('48.8565056', results[2]['geojson']['coordinates'])
199
+        self.assertIn('2.3521334', results[2]['geojson']['coordinates'])

+ 1
- 0
searx/tests/test_engines.py 查看文件

@@ -18,6 +18,7 @@ from searx.tests.engines.test_google_news import *  # noqa
18 18
 from searx.tests.engines.test_kickass import *  # noqa
19 19
 from searx.tests.engines.test_mediawiki import *  # noqa
20 20
 from searx.tests.engines.test_mixcloud import *  # noqa
21
+from searx.tests.engines.test_openstreetmap import *  # noqa
21 22
 from searx.tests.engines.test_piratebay import *  # noqa
22 23
 from searx.tests.engines.test_searchcode_code import *  # noqa
23 24
 from searx.tests.engines.test_searchcode_doc import *  # noqa