| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 | # -*- coding: utf-8 -*-from collections import defaultdictimport mockfrom searx.engines import openstreetmapfrom searx.testing import SearxTestCaseclass TestOpenstreetmapEngine(SearxTestCase):    def test_request(self):        query = 'test_query'        dicto = defaultdict(dict)        dicto['pageno'] = 1        params = openstreetmap.request(query, dicto)        self.assertIn('url', params)        self.assertIn(query, params['url'])        self.assertIn('openstreetmap.org', params['url'])    def test_response(self):        self.assertRaises(AttributeError, openstreetmap.response, None)        self.assertRaises(AttributeError, openstreetmap.response, [])        self.assertRaises(AttributeError, openstreetmap.response, '')        self.assertRaises(AttributeError, openstreetmap.response, '[]')        response = mock.Mock(text='{}')        self.assertEqual(openstreetmap.response(response), [])        response = mock.Mock(text='{"data": []}')        self.assertEqual(openstreetmap.response(response), [])        json = """        [          {            "place_id": "127732055",            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",            "osm_type": "relation",            "osm_id": "7444",            "boundingbox": [              "48.8155755",              "48.902156",              "2.224122",              "2.4697602"            ],            "lat": "48.8565056",            "lon": "2.3521334",            "display_name": "This is the title",            "class": "place",            "type": "city",            "importance": 0.96893459932191,            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",            "address": {              "city": "Paris",              "county": "Paris",              "state": "Île-de-France",              "country": "France",              "country_code": "fr"            },            "geojson": {              "type": "Polygon",              "coordinates": [                [                  [                    2.224122,                    48.854199                  ]                ]              ]            }          }        ]        """        response = mock.Mock(text=json)        results = openstreetmap.response(response)        self.assertEqual(type(results), list)        self.assertEqual(len(results), 1)        self.assertEqual(results[0]['title'], 'This is the title')        self.assertEqual(results[0]['url'], 'https://openstreetmap.org/relation/7444')        self.assertIn('coordinates', results[0]['geojson'])        self.assertEqual(results[0]['geojson']['coordinates'][0][0][0], 2.224122)        self.assertEqual(results[0]['geojson']['coordinates'][0][0][1], 48.854199)        self.assertEqual(results[0]['address'], None)        self.assertIn('48.8155755', results[0]['boundingbox'])        self.assertIn('48.902156', results[0]['boundingbox'])        self.assertIn('2.224122', results[0]['boundingbox'])        self.assertIn('2.4697602', results[0]['boundingbox'])        json = """        [          {            "place_id": "127732055",            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",            "osm_type": "relation",            "osm_id": "7444",            "boundingbox": [              "48.8155755",              "48.902156",              "2.224122",              "2.4697602"            ],            "lat": "48.8565056",            "lon": "2.3521334",            "display_name": "This is the title",            "class": "tourism",            "type": "city",            "importance": 0.96893459932191,            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",            "address": {              "city": "Paris",              "county": "Paris",              "state": "Île-de-France",              "country": "France",              "country_code": "fr",              "address29": "Address"            },            "geojson": {              "type": "Polygon",              "coordinates": [                [                  [                    2.224122,                    48.854199                  ]                ]              ]            }          },          {            "place_id": "127732055",            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",            "osm_type": "relation",            "osm_id": "7444",            "boundingbox": [              "48.8155755",              "48.902156",              "2.224122",              "2.4697602"            ],            "lat": "48.8565056",            "lon": "2.3521334",            "display_name": "This is the title",            "class": "tourism",            "type": "city",            "importance": 0.96893459932191,            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",            "address": {              "city": "Paris",              "county": "Paris",              "state": "Île-de-France",              "country": "France",              "postcode": 75000,              "country_code": "fr"            },            "geojson": {              "type": "Polygon",              "coordinates": [                [                  [                    2.224122,                    48.854199                  ]                ]              ]            }          },          {            "place_id": "127732055",            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",            "osm_type": "node",            "osm_id": "7444",            "boundingbox": [              "48.8155755",              "48.902156",              "2.224122",              "2.4697602"            ],            "lat": "48.8565056",            "lon": "2.3521334",            "display_name": "This is the title",            "class": "tourism",            "type": "city",            "importance": 0.96893459932191,            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",            "address": {              "city": "Paris",              "county": "Paris",              "state": "Île-de-France",              "country": "France",              "country_code": "fr",              "address29": "Address"            }          }        ]        """        response = mock.Mock(text=json)        results = openstreetmap.response(response)        self.assertEqual(type(results), list)        self.assertEqual(len(results), 3)        self.assertIn('48.8565056', results[2]['geojson']['coordinates'])        self.assertIn('2.3521334', results[2]['geojson']['coordinates'])
 |