Mediawiki's unit test

This commit is contained in:
Cqoicebordel 2015-02-03 19:56:26 +01:00
parent 1ea5bc63a5
commit a96208be96
3 changed files with 134 additions and 6 deletions

View File

@ -13,12 +13,9 @@ def request(query, params):
if not m:
# wrong query
return params
try:
ammount, from_currency, to_currency = m.groups()
ammount = float(ammount)
except:
# wrong params
return params
ammount, from_currency, to_currency = m.groups()
ammount = float(ammount)
q = (from_currency + to_currency).upper()

View File

@ -0,0 +1,130 @@
# -*- 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'])
dicto['language'] = 'all'
params = mediawiki.request(query, dicto)
self.assertIn('en', 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 = """
{"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)

View File

@ -16,6 +16,7 @@ from searx.tests.engines.test_www1x import * # noqa
from searx.tests.engines.test_google_images import * # noqa
from searx.tests.engines.test_google_news import * # noqa
from searx.tests.engines.test_kickass import * # noqa
from searx.tests.engines.test_mediawiki import * # noqa
from searx.tests.engines.test_mixcloud import * # noqa
from searx.tests.engines.test_piratebay import * # noqa
from searx.tests.engines.test_searchcode_code import * # noqa