From f703a77fc9d44e1a2718a4c26a507973f3bf976d Mon Sep 17 00:00:00 2001
From: Cqoicebordel <Cqoicebordel@users.noreply.github.com>
Date: Tue, 10 Feb 2015 18:44:49 +0100
Subject: [PATCH] Photon's unit test As in Flickr, a continue is not detected
 by coverage as covered but it is.

---
 searx/engines/photon.py            |   2 +-
 searx/tests/engines/test_photon.py | 166 +++++++++++++++++++++++++++++
 searx/tests/test_engines.py        |   1 +
 3 files changed, 168 insertions(+), 1 deletion(-)
 create mode 100644 searx/tests/engines/test_photon.py

diff --git a/searx/engines/photon.py b/searx/engines/photon.py
index 16340d24a..a9c558c4b 100644
--- a/searx/engines/photon.py
+++ b/searx/engines/photon.py
@@ -61,7 +61,7 @@ def response(resp):
             continue
 
         # get title
-        title = properties['name']
+        title = properties.get('name')
 
         # get osm-type
         if properties.get('osm_type') == 'N':
diff --git a/searx/tests/engines/test_photon.py b/searx/tests/engines/test_photon.py
new file mode 100644
index 000000000..734497884
--- /dev/null
+++ b/searx/tests/engines/test_photon.py
@@ -0,0 +1,166 @@
+# -*- coding: utf-8 -*-
+from collections import defaultdict
+import mock
+from searx.engines import photon
+from searx.testing import SearxTestCase
+
+
+class TestPhotonEngine(SearxTestCase):
+
+    def test_request(self):
+        query = 'test_query'
+        dicto = defaultdict(dict)
+        dicto['pageno'] = 1
+        dicto['language'] = 'all'
+        params = photon.request(query, dicto)
+        self.assertIn('url', params)
+        self.assertIn(query, params['url'])
+        self.assertIn('photon.komoot.de', params['url'])
+
+        dicto['language'] = 'all'
+        params = photon.request(query, dicto)
+        self.assertNotIn('lang', params['url'])
+
+        dicto['language'] = 'al'
+        params = photon.request(query, dicto)
+        self.assertNotIn('lang', params['url'])
+
+        dicto['language'] = 'fr'
+        params = photon.request(query, dicto)
+        self.assertIn('fr', params['url'])
+
+    def test_response(self):
+        self.assertRaises(AttributeError, photon.response, None)
+        self.assertRaises(AttributeError, photon.response, [])
+        self.assertRaises(AttributeError, photon.response, '')
+        self.assertRaises(AttributeError, photon.response, '[]')
+
+        response = mock.Mock(text='{}')
+        self.assertEqual(photon.response(response), [])
+
+        response = mock.Mock(text='{"data": []}')
+        self.assertEqual(photon.response(response), [])
+
+        json = """
+        {
+          "features": [
+            {
+              "properties": {
+                "osm_key": "waterway",
+                "extent": [
+                  -1.4508446,
+                  51.1614997,
+                  -1.4408036,
+                  51.1525635
+                ],
+                "name": "This is the title",
+                "state": "England",
+                "osm_id": 114823817,
+                "osm_type": "W",
+                "osm_value": "river",
+                "city": "Test Valley",
+                "country": "United Kingdom"
+              },
+              "type": "Feature",
+              "geometry": {
+                "type": "Point",
+                "coordinates": [
+                  -1.4458571,
+                  51.1576661
+                ]
+              }
+            },
+            {
+              "properties": {
+                "osm_key": "place",
+                "street": "Rue",
+                "state": "Ile-de-France",
+                "osm_id": 129211377,
+                "osm_type": "R",
+                "housenumber": "10",
+                "postcode": "75011",
+                "osm_value": "house",
+                "city": "Paris",
+                "country": "France"
+              },
+              "type": "Feature",
+              "geometry": {
+                "type": "Point",
+                "coordinates": [
+                  2.3725025,
+                  48.8654481
+                ]
+              }
+            },
+            {
+              "properties": {
+                "osm_key": "amenity",
+                "street": "Allée",
+                "name": "Bibliothèque",
+                "state": "Ile-de-France",
+                "osm_id": 1028573132,
+                "osm_type": "N",
+                "postcode": "75001",
+                "osm_value": "library",
+                "city": "Paris",
+                "country": "France"
+              },
+              "type": "Feature",
+              "geometry": {
+                "type": "Point",
+                "coordinates": [
+                  2.3445634,
+                  48.862494
+                ]
+              }
+            },
+            {
+              "properties": {
+                "osm_key": "amenity",
+                "osm_id": 1028573132,
+                "osm_type": "Y",
+                "postcode": "75001",
+                "osm_value": "library",
+                "city": "Paris",
+                "country": "France"
+              },
+              "type": "Feature",
+              "geometry": {
+                "type": "Point",
+                "coordinates": [
+                  2.3445634,
+                  48.862494
+                ]
+              }
+            },
+            {
+            }
+        ],
+          "type": "FeatureCollection"
+        }
+        """
+        response = mock.Mock(text=json)
+        results = photon.response(response)
+        self.assertEqual(type(results), list)
+        self.assertEqual(len(results), 3)
+        self.assertEqual(results[0]['title'], 'This is the title')
+        self.assertEqual(results[0]['content'], '')
+        self.assertEqual(results[0]['longitude'], -1.4458571)
+        self.assertEqual(results[0]['latitude'], 51.1576661)
+        self.assertIn(-1.4508446, results[0]['boundingbox'])
+        self.assertIn(51.1614997, results[0]['boundingbox'])
+        self.assertIn(-1.4408036, results[0]['boundingbox'])
+        self.assertIn(51.1525635, results[0]['boundingbox'])
+        self.assertIn('type', results[0]['geojson'])
+        self.assertEqual(results[0]['geojson']['type'], 'Point')
+        self.assertEqual(results[0]['address'], None)
+        self.assertEqual(results[0]['osm']['type'], 'way')
+        self.assertEqual(results[0]['osm']['id'], 114823817)
+        self.assertEqual(results[0]['url'], 'https://openstreetmap.org/way/114823817')
+        self.assertEqual(results[1]['osm']['type'], 'relation')
+        self.assertEqual(results[2]['address']['name'], u'Bibliothèque')
+        self.assertEqual(results[2]['address']['house_number'], None)
+        self.assertEqual(results[2]['address']['locality'], 'Paris')
+        self.assertEqual(results[2]['address']['postcode'], '75001')
+        self.assertEqual(results[2]['address']['country'], 'France')
+        self.assertEqual(results[2]['osm']['type'], 'node')
diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py
index 0570a5296..966b5f1be 100644
--- a/searx/tests/test_engines.py
+++ b/searx/tests/test_engines.py
@@ -21,6 +21,7 @@ from searx.tests.engines.test_kickass import *  # noqa
 from searx.tests.engines.test_mediawiki import *  # noqa
 from searx.tests.engines.test_mixcloud import *  # noqa
 from searx.tests.engines.test_openstreetmap import *  # noqa
+from searx.tests.engines.test_photon import *  # noqa
 from searx.tests.engines.test_piratebay import *  # noqa
 from searx.tests.engines.test_searchcode_code import *  # noqa
 from searx.tests.engines.test_searchcode_doc import *  # noqa