test_bing_images.py 4.5 KB
Newer Older
Cqoicebordel's avatar
Cqoicebordel committed
1
2
3
4
5
6
7
8
9
10
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import bing_images
from searx.testing import SearxTestCase


class TestBingImagesEngine(SearxTestCase):

    def test_request(self):
11
        bing_images.supported_languages = ['fr-FR', 'en-US']
12
        bing_images.language_aliases = {}
Cqoicebordel's avatar
Cqoicebordel committed
13
14
15
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
16
        dicto['language'] = 'fr-FR'
Thomas Pointhuber's avatar
Thomas Pointhuber committed
17
        dicto['safesearch'] = 1
18
        dicto['time_range'] = ''
Cqoicebordel's avatar
Cqoicebordel committed
19
20
21
22
23
        params = bing_images.request(query, dicto)
        self.assertTrue('url' in params)
        self.assertTrue(query in params['url'])
        self.assertTrue('bing.com' in params['url'])
        self.assertTrue('SRCHHPGUSR' in params['cookies'])
24
25
26
        self.assertTrue('DEMOTE' in params['cookies']['SRCHHPGUSR'])
        self.assertTrue('_EDGE_S' in params['cookies'])
        self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])
Cqoicebordel's avatar
Cqoicebordel committed
27

28
29
30
31
32
        dicto['language'] = 'fr'
        params = bing_images.request(query, dicto)
        self.assertTrue('_EDGE_S' in params['cookies'])
        self.assertTrue('fr-fr' in params['cookies']['_EDGE_S'])

Cqoicebordel's avatar
Cqoicebordel committed
33
34
        dicto['language'] = 'all'
        params = bing_images.request(query, dicto)
35
        self.assertTrue('_EDGE_S' in params['cookies'])
36
        self.assertTrue('en-us' in params['cookies']['_EDGE_S'])
Cqoicebordel's avatar
Cqoicebordel committed
37
38
39
40
41
42
43

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

44
        response = mock.Mock(text='<html></html>')
Cqoicebordel's avatar
Cqoicebordel committed
45
46
        self.assertEqual(bing_images.response(response), [])

47
        response = mock.Mock(text='<html></html>')
Cqoicebordel's avatar
Cqoicebordel committed
48
49
50
        self.assertEqual(bing_images.response(response), [])

        html = """
marc's avatar
marc committed
51
52
53
54
55
        <div id="mmComponent_images_1">
            <ul>
                <li>
                    <div>
                        <div class="imgpt">
56
                            <a m='{"purl":"page_url","murl":"img_url","turl":"thumb_url"}'>
marc's avatar
marc committed
57
58
59
60
61
62
63
                                <img src="" alt="alt text" />
                            </a>
                        </div>
                        <div></div>
                    </div>
                    <div>
                        <div class="imgpt">
64
                            <a m='{"purl":"page_url2","murl":"img_url2","turl":"thumb_url2"}'>
marc's avatar
marc committed
65
66
67
68
69
70
71
72
73
74
                                <img src="" alt="alt text 2" />
                            </a>
                        </div>
                    </div>
                </li>
            </ul>
            <ul>
                <li>
                    <div>
                        <div class="imgpt">
75
                            <a m='{"purl":"page_url3","murl":"img_url3","turl":"thumb_url3"}'>
marc's avatar
marc committed
76
77
78
79
80
81
82
                                <img src="" alt="alt text 3" />
                            </a>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
Cqoicebordel's avatar
Cqoicebordel committed
83
84
        """
        html = html.replace('\r\n', '').replace('\n', '').replace('\r', '')
85
        response = mock.Mock(text=html)
Cqoicebordel's avatar
Cqoicebordel committed
86
87
        results = bing_images.response(response)
        self.assertEqual(type(results), list)
Adam Tauber's avatar
Adam Tauber committed
88
        self.assertEqual(len(results), 3)
marc's avatar
marc committed
89
90
91
92
93
        self.assertEqual(results[0]['title'], 'alt text')
        self.assertEqual(results[0]['url'], 'page_url')
        self.assertEqual(results[0]['content'], '')
        self.assertEqual(results[0]['thumbnail_src'], 'thumb_url')
        self.assertEqual(results[0]['img_src'], 'img_url')
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

    def test_fetch_supported_languages(self):
        html = """
        <div>
            <div id="region-section-content">
                <ul class="b_vList">
                    <li>
                        <a href="https://bing...&setmkt=de-DE&s...">Germany</a>
                        <a href="https://bing...&setmkt=nb-NO&s...">Norway</a>
                    </li>
                </ul>
                <ul class="b_vList">
                    <li>
                        <a href="https://bing...&setmkt=es-AR&s...">Argentina</a>
                    </li>
                </ul>
            </div>
        </div>
        """
        response = mock.Mock(text=html)
        languages = list(bing_images._fetch_supported_languages(response))
        self.assertEqual(len(languages), 3)
        self.assertIn('de-DE', languages)
        self.assertIn('no-NO', languages)
        self.assertIn('es-AR', languages)