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
|
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import mediawiki
from searx.testing import SearxTestCase
class TestMediawikiEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['language'] = 'fr_FR'
params = mediawiki.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('wikipedia.org', params['url'])
self.assertIn('fr', params['url'])
mediawiki.base_url = "http://test.url/"
mediawiki.search_url = mediawiki.base_url +\
'w/api.php?action=query'\
'&list=search'\
'&{query}'\
'&srprop=timestamp'\
'&format=json'\
'&sroffset={offset}'\
'&srlimit={limit}' # noqa
params = mediawiki.request(query, dicto)
self.assertIn('test.url', params['url'])
def test_response(self):
dicto = defaultdict(dict)
dicto['language'] = 'fr'
mediawiki.base_url = "https://{language}.wikipedia.org/"
self.assertRaises(AttributeError, mediawiki.response, None)
self.assertRaises(AttributeError, mediawiki.response, [])
self.assertRaises(AttributeError, mediawiki.response, '')
self.assertRaises(AttributeError, mediawiki.response, '[]')
response = mock.Mock(text='{}', search_params=dicto)
self.assertEqual(mediawiki.response(response), [])
response = mock.Mock(text='{"data": []}', search_params=dicto)
self.assertEqual(mediawiki.response(response), [])
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
"searchinfo": {
"totalhits": 29721
},
"search": [
{
"ns": 0,
"title": "This is the title étude",
"timestamp": "2014-12-19T17:42:52Z"
}
]
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], u'This is the title étude')
self.assertIn('fr.wikipedia.org', results[0]['url'])
self.assertIn('This_is_the_title', results[0]['url'])
self.assertIn('%C3%A9tude', results[0]['url'])
self.assertEqual(results[0]['content'], '')
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
"searchinfo": {
"totalhits": 29721
},
"search": [
]
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = """
{
"query-continue": {
"search": {
"sroffset": 1
}
},
"query": {
}
}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
json = r"""
{"toto":[
{"id":200,"name":"Artist Name",
"link":"http:\/\/www.mediawiki.com\/artist\/1217","type":"artist"}
]}
"""
response = mock.Mock(text=json, search_params=dicto)
results = mediawiki.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
|