logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_openstreetmap.py (6887B)


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