test_photon.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import photon
  5. from searx.testing import SearxTestCase
  6. class TestPhotonEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'all'
  12. params = photon.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('photon.komoot.de', params['url'])
  16. dicto['language'] = 'all'
  17. params = photon.request(query, dicto)
  18. self.assertNotIn('lang', params['url'])
  19. dicto['language'] = 'al'
  20. params = photon.request(query, dicto)
  21. self.assertNotIn('lang', params['url'])
  22. dicto['language'] = 'fr'
  23. params = photon.request(query, dicto)
  24. self.assertIn('fr', params['url'])
  25. def test_response(self):
  26. self.assertRaises(AttributeError, photon.response, None)
  27. self.assertRaises(AttributeError, photon.response, [])
  28. self.assertRaises(AttributeError, photon.response, '')
  29. self.assertRaises(AttributeError, photon.response, '[]')
  30. response = mock.Mock(text='{}')
  31. self.assertEqual(photon.response(response), [])
  32. response = mock.Mock(text='{"data": []}')
  33. self.assertEqual(photon.response(response), [])
  34. json = """
  35. {
  36. "features": [
  37. {
  38. "properties": {
  39. "osm_key": "waterway",
  40. "extent": [
  41. -1.4508446,
  42. 51.1614997,
  43. -1.4408036,
  44. 51.1525635
  45. ],
  46. "name": "This is the title",
  47. "state": "England",
  48. "osm_id": 114823817,
  49. "osm_type": "W",
  50. "osm_value": "river",
  51. "city": "Test Valley",
  52. "country": "United Kingdom"
  53. },
  54. "type": "Feature",
  55. "geometry": {
  56. "type": "Point",
  57. "coordinates": [
  58. -1.4458571,
  59. 51.1576661
  60. ]
  61. }
  62. },
  63. {
  64. "properties": {
  65. "osm_key": "place",
  66. "street": "Rue",
  67. "state": "Ile-de-France",
  68. "osm_id": 129211377,
  69. "osm_type": "R",
  70. "housenumber": "10",
  71. "postcode": "75011",
  72. "osm_value": "house",
  73. "city": "Paris",
  74. "country": "France"
  75. },
  76. "type": "Feature",
  77. "geometry": {
  78. "type": "Point",
  79. "coordinates": [
  80. 2.3725025,
  81. 48.8654481
  82. ]
  83. }
  84. },
  85. {
  86. "properties": {
  87. "osm_key": "amenity",
  88. "street": "Allée",
  89. "name": "Bibliothèque",
  90. "state": "Ile-de-France",
  91. "osm_id": 1028573132,
  92. "osm_type": "N",
  93. "postcode": "75001",
  94. "osm_value": "library",
  95. "city": "Paris",
  96. "country": "France"
  97. },
  98. "type": "Feature",
  99. "geometry": {
  100. "type": "Point",
  101. "coordinates": [
  102. 2.3445634,
  103. 48.862494
  104. ]
  105. }
  106. },
  107. {
  108. "properties": {
  109. "osm_key": "amenity",
  110. "osm_id": 1028573132,
  111. "osm_type": "Y",
  112. "postcode": "75001",
  113. "osm_value": "library",
  114. "city": "Paris",
  115. "country": "France"
  116. },
  117. "type": "Feature",
  118. "geometry": {
  119. "type": "Point",
  120. "coordinates": [
  121. 2.3445634,
  122. 48.862494
  123. ]
  124. }
  125. },
  126. {
  127. }
  128. ],
  129. "type": "FeatureCollection"
  130. }
  131. """
  132. response = mock.Mock(text=json)
  133. results = photon.response(response)
  134. self.assertEqual(type(results), list)
  135. self.assertEqual(len(results), 3)
  136. self.assertEqual(results[0]['title'], 'This is the title')
  137. self.assertEqual(results[0]['content'], '')
  138. self.assertEqual(results[0]['longitude'], -1.4458571)
  139. self.assertEqual(results[0]['latitude'], 51.1576661)
  140. self.assertIn(-1.4508446, results[0]['boundingbox'])
  141. self.assertIn(51.1614997, results[0]['boundingbox'])
  142. self.assertIn(-1.4408036, results[0]['boundingbox'])
  143. self.assertIn(51.1525635, results[0]['boundingbox'])
  144. self.assertIn('type', results[0]['geojson'])
  145. self.assertEqual(results[0]['geojson']['type'], 'Point')
  146. self.assertEqual(results[0]['address'], None)
  147. self.assertEqual(results[0]['osm']['type'], 'way')
  148. self.assertEqual(results[0]['osm']['id'], 114823817)
  149. self.assertEqual(results[0]['url'], 'https://openstreetmap.org/way/114823817')
  150. self.assertEqual(results[1]['osm']['type'], 'relation')
  151. self.assertEqual(results[2]['address']['name'], u'Bibliothèque')
  152. self.assertEqual(results[2]['address']['house_number'], None)
  153. self.assertEqual(results[2]['address']['locality'], 'Paris')
  154. self.assertEqual(results[2]['address']['postcode'], '75001')
  155. self.assertEqual(results[2]['address']['country'], 'France')
  156. self.assertEqual(results[2]['osm']['type'], 'node')