Browse Source

Google news' unit test

Cqoicebordel 10 years ago
parent
commit
b7dc1fb9d5
3 changed files with 141 additions and 2 deletions
  1. 4
    2
      searx/engines/google_news.py
  2. 136
    0
      searx/tests/engines/test_google_news.py
  3. 1
    0
      searx/tests/test_engines.py

+ 4
- 2
searx/engines/google_news.py View File

@@ -20,7 +20,7 @@ language_support = True
20 20
 
21 21
 # engine dependent config
22 22
 url = 'https://ajax.googleapis.com/'
23
-search_url = url + 'ajax/services/search/news?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={language}'  # noqa
23
+search_url = url + 'ajax/services/search/news?v=2.0&start={offset}&rsz=large&safe=off&filter=off&{query}&hl={lang}'
24 24
 
25 25
 
26 26
 # do search-request
@@ -33,7 +33,7 @@ def request(query, params):
33 33
 
34 34
     params['url'] = search_url.format(offset=offset,
35 35
                                       query=urlencode({'q': query}),
36
-                                      language=language)
36
+                                      lang=language)
37 37
 
38 38
     return params
39 39
 
@@ -52,6 +52,8 @@ def response(resp):
52 52
     for result in search_res['responseData']['results']:
53 53
         # parse publishedDate
54 54
         publishedDate = parser.parse(result['publishedDate'])
55
+        if 'url' not in result:
56
+            continue
55 57
 
56 58
         # append result
57 59
         results.append({'url': result['unescapedUrl'],

+ 136
- 0
searx/tests/engines/test_google_news.py View File

@@ -0,0 +1,136 @@
1
+from collections import defaultdict
2
+import mock
3
+from searx.engines import google_news
4
+from searx.testing import SearxTestCase
5
+
6
+
7
+class TestGoogleNewsEngine(SearxTestCase):
8
+
9
+    def test_request(self):
10
+        query = 'test_query'
11
+        dicto = defaultdict(dict)
12
+        dicto['pageno'] = 1
13
+        dicto['language'] = 'fr_FR'
14
+        params = google_news.request(query, dicto)
15
+        self.assertIn('url', params)
16
+        self.assertIn(query, params['url'])
17
+        self.assertIn('googleapis.com', params['url'])
18
+        self.assertIn('fr', params['url'])
19
+
20
+        dicto['language'] = 'all'
21
+        params = google_news.request(query, dicto)
22
+        self.assertIn('url', params)
23
+        self.assertIn('en', params['url'])
24
+
25
+    def test_response(self):
26
+        self.assertRaises(AttributeError, google_news.response, None)
27
+        self.assertRaises(AttributeError, google_news.response, [])
28
+        self.assertRaises(AttributeError, google_news.response, '')
29
+        self.assertRaises(AttributeError, google_news.response, '[]')
30
+
31
+        response = mock.Mock(text='{}')
32
+        self.assertEqual(google_news.response(response), [])
33
+
34
+        response = mock.Mock(text='{"data": []}')
35
+        self.assertEqual(google_news.response(response), [])
36
+
37
+        json = """
38
+        {
39
+        "responseData": {
40
+            "results": [
41
+            {
42
+                "GsearchResultClass": "GnewsSearch",
43
+                "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
44
+                "content": "This is the content",
45
+                "unescapedUrl": "http://this.is.the.url",
46
+                "url": "http://this.is.the.url",
47
+                "title": "This is the title",
48
+                "titleNoFormatting": "This is the title",
49
+                "location": "",
50
+                "publisher": "Jeux Actu",
51
+                "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
52
+                "signedRedirectUrl": "http://news.google.com/",
53
+                "language": "fr",
54
+                "image": {
55
+                "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
56
+                "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcSF4yYrs9Ycw23DGiOSAZ-5SEPXYwG3LNs",
57
+                "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
58
+                "publisher": "Jeux Actu",
59
+                "tbWidth": 80,
60
+                "tbHeight": 30
61
+                },
62
+                "relatedStories": [
63
+                {
64
+                    "unescapedUrl": "http://www.jeuxvideo.com/test/415823/dying-light.htm",
65
+                    "url": "http%3A%2F%2Fwww.jeuxvideo.com%2Ftest%2F415823%2Fdying-light.htm",
66
+                    "title": "<b>Test</b> du jeu Dying Light - jeuxvideo.com",
67
+                    "titleNoFormatting": "Test du jeu Dying Light - jeuxvideo.com",
68
+                    "location": "",
69
+                    "publisher": "JeuxVideo.com",
70
+                    "publishedDate": "Fri, 30 Jan 2015 08:52:30 -0800",
71
+                    "signedRedirectUrl": "http://news.google.com/news/url?sa=T&",
72
+                    "language": "fr"
73
+                }
74
+                ]
75
+            }
76
+            ]
77
+        },
78
+        "responseDetails": null,
79
+        "responseStatus": 200
80
+        }
81
+        """
82
+        response = mock.Mock(text=json)
83
+        results = google_news.response(response)
84
+        self.assertEqual(type(results), list)
85
+        self.assertEqual(len(results), 1)
86
+        self.assertEqual(results[0]['title'], 'This is the title')
87
+        self.assertEqual(results[0]['url'], 'http://this.is.the.url')
88
+        self.assertEqual(results[0]['content'], 'This is the content')
89
+
90
+        json = """
91
+        {
92
+        "responseData": {
93
+            "results": [
94
+            {
95
+                "GsearchResultClass": "GnewsSearch",
96
+                "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
97
+                "content": "This is the content",
98
+                "unescapedUrl": "http://this.is.the.url",
99
+                "title": "This is the title",
100
+                "titleNoFormatting": "This is the title",
101
+                "location": "",
102
+                "publisher": "Jeux Actu",
103
+                "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
104
+                "signedRedirectUrl": "http://news.google.com/news/",
105
+                "language": "fr",
106
+                "image": {
107
+                "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
108
+                "tbUrl": "http://t1.gstatic.com/images?q=tbn:b_6f-OSAZ-5SEPXYwG3LNs",
109
+                "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
110
+                "publisher": "Jeux Actu",
111
+                "tbWidth": 80,
112
+                "tbHeight": 30
113
+                }
114
+            }
115
+            ]
116
+        },
117
+        "responseDetails": null,
118
+        "responseStatus": 200
119
+        }
120
+        """
121
+        response = mock.Mock(text=json)
122
+        results = google_news.response(response)
123
+        self.assertEqual(type(results), list)
124
+        self.assertEqual(len(results), 0)
125
+
126
+        json = """
127
+        {
128
+        "responseData": {},
129
+        "responseDetails": null,
130
+        "responseStatus": 200
131
+        }
132
+        """
133
+        response = mock.Mock(text=json)
134
+        results = google_news.response(response)
135
+        self.assertEqual(type(results), list)
136
+        self.assertEqual(len(results), 0)

+ 1
- 0
searx/tests/test_engines.py View File

@@ -10,6 +10,7 @@ from searx.tests.engines.test_dummy import *  # noqa
10 10
 from searx.tests.engines.test_flickr import *  # noqa
11 11
 from searx.tests.engines.test_github import *  # noqa
12 12
 from searx.tests.engines.test_google_images import *  # noqa
13
+from searx.tests.engines.test_google_news import *  # noqa
13 14
 from searx.tests.engines.test_kickass import *  # noqa
14 15
 from searx.tests.engines.test_mixcloud import *  # noqa
15 16
 from searx.tests.engines.test_searchcode_code import *  # noqa