|
@@ -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)
|