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


class TestWikipediaEngine(SearxTestCase):

    def test_request(self):
11
12
        wikipedia.supported_languages = ['fr', 'en', 'no']
        wikipedia.language_aliases = {'nb': 'no'}
13

a01200356's avatar
a01200356 committed
14
15
        query = 'test_query'
        dicto = defaultdict(dict)
marc's avatar
marc committed
16
        dicto['language'] = 'fr-FR'
Adam Tauber's avatar
Adam Tauber committed
17
        params = wikipedia.request(query.encode('utf-8'), dicto)
a01200356's avatar
a01200356 committed
18
19
20
21
22
23
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('test_query', params['url'])
        self.assertIn('Test_Query', params['url'])
        self.assertIn('fr.wikipedia.org', params['url'])

Adam Tauber's avatar
Adam Tauber committed
24
25
        query = u'Test_Query'
        params = wikipedia.request(query.encode('utf-8'), dicto)
a01200356's avatar
a01200356 committed
26
27
28
        self.assertIn('Test_Query', params['url'])
        self.assertNotIn('test_query', params['url'])

29
30
31
        dicto['language'] = 'nb'
        params = wikipedia.request(query, dicto)
        self.assertIn('no.wikipedia.org', params['url'])
32
33
34
        dicto['language'] = 'all'
        params = wikipedia.request(query, dicto)
        self.assertIn('en', params['url'])
35

marc's avatar
marc committed
36
37
        dicto['language'] = 'xx'
        params = wikipedia.request(query, dicto)
38
        self.assertIn('en.wikipedia.org', params['url'])
marc's avatar
marc committed
39

a01200356's avatar
a01200356 committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    def test_response(self):
        dicto = defaultdict(dict)
        dicto['language'] = 'fr'

        self.assertRaises(AttributeError, wikipedia.response, None)
        self.assertRaises(AttributeError, wikipedia.response, [])
        self.assertRaises(AttributeError, wikipedia.response, '')
        self.assertRaises(AttributeError, wikipedia.response, '[]')

        # page not found
        json = """
        {
            "batchcomplete": "",
            "query": {
                "normalized": [],
                "pages": {
                    "-1": {
                        "ns": 0,
                        "title": "",
                        "missing": ""
                    }
                }
            }
        }"""
Adam Tauber's avatar
Adam Tauber committed
64
        response = mock.Mock(text=json, search_params=dicto)
a01200356's avatar
a01200356 committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        self.assertEqual(wikipedia.response(response), [])

        # normal case
        json = """
        {
            "batchcomplete": "",
            "query": {
                "normalized": [],
                "pages": {
                    "12345": {
                        "pageid": 12345,
                        "ns": 0,
                        "title": "The Title",
                        "extract": "The Title is...",
                        "thumbnail": {
                            "source": "img_src.jpg"
                        },
                        "pageimage": "img_name.jpg"
                    }
                }
            }
        }"""
Adam Tauber's avatar
Adam Tauber committed
87
        response = mock.Mock(text=json, search_params=dicto)
a01200356's avatar
a01200356 committed
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
        results = wikipedia.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['title'], u'The Title')
        self.assertIn('fr.wikipedia.org/wiki/The_Title', results[0]['url'])
        self.assertEqual(results[1]['infobox'], u'The Title')
        self.assertIn('fr.wikipedia.org/wiki/The_Title', results[1]['id'])
        self.assertIn('The Title is...', results[1]['content'])
        self.assertEqual(results[1]['img_src'], 'img_src.jpg')

        # disambiguation page
        json = """
        {
            "batchcomplete": "",
            "query": {
                "normalized": [],
                "pages": {
                    "12345": {
                        "pageid": 12345,
                        "ns": 0,
                        "title": "The Title",
                        "extract": "The Title can be:\\nThe Title 1\\nThe Title 2\\nThe Title 3\\nThe Title 4......................................................................................................................................." """  # noqa
        json += """
                    }
                }
            }
        }"""
Adam Tauber's avatar
Adam Tauber committed
115
        response = mock.Mock(text=json, search_params=dicto)
a01200356's avatar
a01200356 committed
116
117
        results = wikipedia.response(response)
        self.assertEqual(type(results), list)
Adam Tauber's avatar
Adam Tauber committed
118
        self.assertEqual(len(results), 2)
a01200356's avatar
a01200356 committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

        # no image
        json = """
        {
            "batchcomplete": "",
            "query": {
                "normalized": [],
                "pages": {
                    "12345": {
                        "pageid": 12345,
                        "ns": 0,
                        "title": "The Title",
                        "extract": "The Title is......................................................................................................................................................................................." """  # noqa
        json += """
                    }
                }
            }
        }"""
Adam Tauber's avatar
Adam Tauber committed
137
        response = mock.Mock(text=json, search_params=dicto)
a01200356's avatar
a01200356 committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        results = wikipedia.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertIn('The Title is...', results[1]['content'])
        self.assertEqual(results[1]['img_src'], None)

        # title not in first paragraph
        json = u"""
        {
            "batchcomplete": "",
            "query": {
                "normalized": [],
                "pages": {
                    "12345": {
                        "pageid": 12345,
                        "ns": 0,
                        "title": "披頭四樂隊",
                        "extract": "披头士乐队....................................................................................................................................................................................................\\n披頭四樂隊...", """  # noqa
        json += """
                        "thumbnail": {
                            "source": "img_src.jpg"
                        },
                        "pageimage": "img_name.jpg"
                    }
                }
            }
        }"""
Adam Tauber's avatar
Adam Tauber committed
165
        response = mock.Mock(text=json, search_params=dicto)
a01200356's avatar
a01200356 committed
166
167
168
169
170
        results = wikipedia.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[1]['infobox'], u'披頭四樂隊')
        self.assertIn(u'披头士乐队...', results[1]['content'])
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

    def test_fetch_supported_languages(self):
        html = u"""<html></html>"""
        response = mock.Mock(text=html)
        languages = wikipedia._fetch_supported_languages(response)
        self.assertEqual(type(languages), dict)
        self.assertEqual(len(languages), 0)

        html = u"""
        <html>
            <body>
                <div>
                    <div>
                        <h3>Table header</h3>
                        <table class="sortable jquery-tablesorter">
                            <thead>
                                <tr>
                                    <th>N</th>
                                    <th>Language</th>
                                    <th>Language (local)</th>
                                    <th>Wiki</th>
                                    <th>Articles</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>2</td>
                                    <td><a>Swedish</a></td>
                                    <td><a>Svenska</a></td>
                                    <td><a>sv</a></td>
                                    <td><a><b>3000000</b></a></td>
                                </tr>
                                <tr>
                                    <td>3</td>
                                    <td><a>Cebuano</a></td>
                                    <td><a>Sinugboanong Binisaya</a></td>
                                    <td><a>ceb</a></td>
                                    <td><a><b>3000000</b></a></td>
                                </tr>
                            </tbody>
                        </table>
                        <h3>Table header</h3>
                        <table class="sortable jquery-tablesorter">
                            <thead>
                                <tr>
                                    <th>N</th>
                                    <th>Language</th>
                                    <th>Language (local)</th>
                                    <th>Wiki</th>
                                    <th>Articles</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>2</td>
                                    <td><a>Norwegian (Bokmål)</a></td>
                                    <td><a>Norsk (Bokmål)</a></td>
                                    <td><a>no</a></td>
                                    <td><a><b>100000</b></a></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </body>
        </html>
        """
        response = mock.Mock(text=html)
        languages = wikipedia._fetch_supported_languages(response)
        self.assertEqual(type(languages), dict)
        self.assertEqual(len(languages), 3)

        self.assertIn('sv', languages)
        self.assertIn('ceb', languages)
        self.assertIn('no', languages)

        self.assertEqual(type(languages['sv']), dict)
        self.assertEqual(type(languages['ceb']), dict)
        self.assertEqual(type(languages['no']), dict)

        self.assertIn('name', languages['sv'])
        self.assertIn('english_name', languages['sv'])
        self.assertIn('articles', languages['sv'])

        self.assertEqual(languages['sv']['name'], 'Svenska')
        self.assertEqual(languages['sv']['english_name'], 'Swedish')
        self.assertEqual(languages['sv']['articles'], 3000000)
        self.assertEqual(languages['ceb']['name'], 'Sinugboanong Binisaya')
        self.assertEqual(languages['ceb']['english_name'], 'Cebuano')
        self.assertEqual(languages['ceb']['articles'], 3000000)
        self.assertEqual(languages['no']['name'], u'Norsk (Bokmål)')
        self.assertEqual(languages['no']['english_name'], u'Norwegian (Bokmål)')
        self.assertEqual(languages['no']['articles'], 100000)