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

Cqoicebordel's avatar
Cqoicebordel committed
3 4 5 6 7 8 9 10 11 12 13 14
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
15
        dicto['language'] = 'fr-FR'
16
        dicto['time_range'] = 'w'
Cqoicebordel's avatar
Cqoicebordel committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
        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), [])

34
        html = u"""
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
<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">
                            <a class="l _PMs" href="https://example.com/" onmousedown="return rwt(this,'','','','11','AFQjCNEyehpzD5cJK1KUfXBx9RmsbqqG9g','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAggiKAAwAA','','',event)">Example title</a>
                        </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">
                            <a class="l _PMs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAgglKAAwAQ','','',event)">Example title 2</a>
                        </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>
81
</div>
82 83


84 85
        """  # noqa
        response = mock.Mock(text=html)
Cqoicebordel's avatar
Cqoicebordel committed
86 87
        results = google_news.response(response)
        self.assertEqual(type(results), list)
88 89 90 91 92 93 94 95
        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')