summaryrefslogtreecommitdiff
path: root/searx/tests/engines/test_google_news.py
blob: 31d67412103fa2dd6c0be1d7191b6b6a05f439ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from collections import defaultdict
import mock
from searx.engines import google_news
from searx.testing import SearxTestCase


class TestGoogleNewsEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        dicto['language'] = 'fr_FR'
        params = google_news.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('googleapis.com', params['url'])
        self.assertIn('fr', params['url'])

        dicto['language'] = 'all'
        params = google_news.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn('en', params['url'])

    def test_response(self):
        self.assertRaises(AttributeError, google_news.response, None)
        self.assertRaises(AttributeError, google_news.response, [])
        self.assertRaises(AttributeError, google_news.response, '')
        self.assertRaises(AttributeError, google_news.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(google_news.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(google_news.response(response), [])

        json = """
        {
        "responseData": {
            "results": [
            {
                "GsearchResultClass": "GnewsSearch",
                "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
                "content": "This is the content",
                "unescapedUrl": "http://this.is.the.url",
                "url": "http://this.is.the.url",
                "title": "This is the title",
                "titleNoFormatting": "This is the title",
                "location": "",
                "publisher": "Jeux Actu",
                "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
                "signedRedirectUrl": "http://news.google.com/",
                "language": "fr",
                "image": {
                "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
                "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcSF4yYrs9Ycw23DGiOSAZ-5SEPXYwG3LNs",
                "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
                "publisher": "Jeux Actu",
                "tbWidth": 80,
                "tbHeight": 30
                },
                "relatedStories": [
                {
                    "unescapedUrl": "http://www.jeuxvideo.com/test/415823/dying-light.htm",
                    "url": "http%3A%2F%2Fwww.jeuxvideo.com%2Ftest%2F415823%2Fdying-light.htm",
                    "title": "<b>Test</b> du jeu Dying Light - jeuxvideo.com",
                    "titleNoFormatting": "Test du jeu Dying Light - jeuxvideo.com",
                    "location": "",
                    "publisher": "JeuxVideo.com",
                    "publishedDate": "Fri, 30 Jan 2015 08:52:30 -0800",
                    "signedRedirectUrl": "http://news.google.com/news/url?sa=T&",
                    "language": "fr"
                }
                ]
            }
            ]
        },
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_news.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'], 'http://this.is.the.url')
        self.assertEqual(results[0]['content'], 'This is the content')

        json = """
        {
        "responseData": {
            "results": [
            {
                "GsearchResultClass": "GnewsSearch",
                "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr",
                "content": "This is the content",
                "unescapedUrl": "http://this.is.the.url",
                "title": "This is the title",
                "titleNoFormatting": "This is the title",
                "location": "",
                "publisher": "Jeux Actu",
                "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800",
                "signedRedirectUrl": "http://news.google.com/news/",
                "language": "fr",
                "image": {
                "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg",
                "tbUrl": "http://t1.gstatic.com/images?q=tbn:b_6f-OSAZ-5SEPXYwG3LNs",
                "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm",
                "publisher": "Jeux Actu",
                "tbWidth": 80,
                "tbHeight": 30
                }
            }
            ]
        },
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_news.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = """
        {
        "responseData": {},
        "responseDetails": null,
        "responseStatus": 200
        }
        """
        response = mock.Mock(text=json)
        results = google_news.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)