test_google_news.py 4.34 KB
Newer Older
1
2
# -*- coding: utf-8 -*-

Cqoicebordel's avatar
Cqoicebordel committed
3
4
5
6
7
8
9
10
11
from collections import defaultdict
import mock
from searx.engines import google_news
from searx.testing import SearxTestCase


class TestGoogleNewsEngine(SearxTestCase):

    def test_request(self):
12
        google_news.supported_languages = ['en-US', 'fr-FR']
13
        google_news.language_aliases = {}
Cqoicebordel's avatar
Cqoicebordel committed
14
15
16
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
17
        dicto['language'] = 'fr-FR'
18
        dicto['time_range'] = 'w'
Cqoicebordel's avatar
Cqoicebordel committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        params = google_news.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('fr', 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), [])

36
        html = u"""
misnyo's avatar
misnyo committed
37
38
39
40
41
42
43
44
45
<h2 class="hd">Search Results</h2>
<div data-async-context="query:searx" id="ires">
    <div eid="oC2oWcGXCafR6ASkwoCwDA" id="rso">
        <div class="_NId">
            <!--m-->
            <div class="g _cy">
                <div class="ts _JGs _JHs _tJs _KGs _jHs">
                    <div class="_hJs">
                        <h3 class="r _gJs">
46
                            <a class="l lLrAF" href="https://example.com/" onmousedown="return rwt(this,'','','','11','AFQjCNEyehpzD5cJK1KUfXBx9RmsbqqG9g','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAggiKAAwAA','','',event)">Example title</a>
misnyo's avatar
misnyo committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
                        </h3>
                        <div class="slp">
                            <span class="_OHs _PHs">
                                Mac &amp; i</span>
                            <span class="_QGs">
                                -</span>
                            <span class="f nsa _QHs">
                                Mar 21, 2016</span>
                        </div>
                        <div class="st">Example description</div>
                    </div>
                </div>
            </div>
            <div class="g _cy">
                <div class="ts _JGs _JHs _oGs _KGs _jHs">
                    <a class="top _xGs _SHs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChC8iAEIJDAB','','',event)">
                        <img class="th _RGs" src="https://example2.com/image.jpg" alt="Story image for searx from Golem.de" onload="typeof google==='object'&&google.aft&&google.aft(this)">
                    </a>
                    <div class="_hJs">
                        <h3 class="r _gJs">
67
                            <a class="l lLrAF" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAgglKAAwAQ','','',event)">Example title 2</a>
misnyo's avatar
misnyo committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
                        </h3>
                        <div class="slp">
                            <span class="_OHs _PHs">
                                Golem.de</span>
                            <span class="_QGs">
                                -</span>
                            <span class="f nsa _QHs">
                                Oct 4, 2016</span>
                        </div>
                        <div class="st">Example description 2</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
83
</div>
misnyo's avatar
misnyo committed
84
85


86
87
        """  # noqa
        response = mock.Mock(text=html)
Cqoicebordel's avatar
Cqoicebordel committed
88
89
        results = google_news.response(response)
        self.assertEqual(type(results), list)
misnyo's avatar
misnyo committed
90
91
92
93
94
95
96
97
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['title'], u'Example title')
        self.assertEqual(results[0]['url'], 'https://example.com/')
        self.assertEqual(results[0]['content'], 'Example description')
        self.assertEqual(results[1]['title'], u'Example title 2')
        self.assertEqual(results[1]['url'], 'https://example2.com/')
        self.assertEqual(results[1]['content'], 'Example description 2')
        self.assertEqual(results[1]['img_src'], 'https://example2.com/image.jpg')