2015-09-07 21:13:04 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-02-02 16:55:39 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import mock
|
|
|
|
from searx.engines import duckduckgo
|
|
|
|
from searx.testing import SearxTestCase
|
|
|
|
|
|
|
|
|
2015-02-02 17:39:50 +00:00
|
|
|
class TestDuckduckgoEngine(SearxTestCase):
|
2015-02-02 16:55:39 +00:00
|
|
|
|
|
|
|
def test_request(self):
|
|
|
|
query = 'test_query'
|
|
|
|
dicto = defaultdict(dict)
|
|
|
|
dicto['pageno'] = 1
|
2016-10-30 02:04:01 +00:00
|
|
|
dicto['language'] = 'de-CH'
|
2016-07-25 22:22:05 +00:00
|
|
|
dicto['time_range'] = ''
|
2015-02-02 16:55:39 +00:00
|
|
|
params = duckduckgo.request(query, dicto)
|
|
|
|
self.assertIn('url', params)
|
|
|
|
self.assertIn(query, params['url'])
|
|
|
|
self.assertIn('duckduckgo.com', params['url'])
|
2016-06-03 05:14:23 +00:00
|
|
|
self.assertIn('ch-de', params['url'])
|
2015-02-02 16:55:39 +00:00
|
|
|
|
2016-12-14 05:51:15 +00:00
|
|
|
# when ddg uses non standard code
|
|
|
|
dicto['language'] = 'en-GB'
|
|
|
|
params = duckduckgo.request(query, dicto)
|
|
|
|
self.assertIn('uk-en', params['url'])
|
|
|
|
|
|
|
|
# no country given
|
|
|
|
duckduckgo.supported_languages = ['de-CH', 'en-US']
|
|
|
|
dicto['language'] = 'de'
|
|
|
|
params = duckduckgo.request(query, dicto)
|
|
|
|
self.assertIn('ch-de', params['url'])
|
|
|
|
|
2016-12-11 15:41:14 +00:00
|
|
|
def test_no_url_in_request_year_time_range(self):
|
|
|
|
dicto = defaultdict(dict)
|
|
|
|
query = 'test_query'
|
|
|
|
dicto['time_range'] = 'year'
|
|
|
|
params = duckduckgo.request(query, dicto)
|
|
|
|
self.assertEqual({}, params['url'])
|
|
|
|
|
2015-02-02 16:55:39 +00:00
|
|
|
def test_response(self):
|
|
|
|
self.assertRaises(AttributeError, duckduckgo.response, None)
|
|
|
|
self.assertRaises(AttributeError, duckduckgo.response, [])
|
|
|
|
self.assertRaises(AttributeError, duckduckgo.response, '')
|
|
|
|
self.assertRaises(AttributeError, duckduckgo.response, '[]')
|
|
|
|
|
|
|
|
response = mock.Mock(text='<html></html>')
|
|
|
|
self.assertEqual(duckduckgo.response(response), [])
|
|
|
|
|
2015-09-07 21:13:04 +00:00
|
|
|
html = u"""
|
2016-03-22 02:19:13 +00:00
|
|
|
<div class="result results_links results_links_deep web-result result--no-result">
|
|
|
|
<div class="links_main links_deep result__body">
|
|
|
|
<h2 class="result__title">
|
|
|
|
</h2>
|
|
|
|
<div class="no-results">No results</div>
|
|
|
|
<div class="result__extras">
|
2015-02-02 16:55:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
response = mock.Mock(text=html)
|
|
|
|
results = duckduckgo.response(response)
|
2016-03-22 02:19:13 +00:00
|
|
|
self.assertEqual(duckduckgo.response(response), [])
|
2015-02-02 16:55:39 +00:00
|
|
|
|
2016-03-22 02:19:13 +00:00
|
|
|
html = u"""
|
|
|
|
<div class="result results_links results_links_deep web-result ">
|
|
|
|
<div class="links_main links_deep result__body">
|
|
|
|
<h2 class="result__title">
|
|
|
|
<a rel="nofollow" class="result__a" href="http://this.should.be.the.link/ű">
|
|
|
|
This <b>is</b> <b>the</b> title
|
|
|
|
</a>
|
|
|
|
</h2>
|
|
|
|
<a class="result__snippet" href="http://this.should.be.the.link/ű">
|
|
|
|
<b>This</b> should be the content.
|
2015-02-02 16:55:39 +00:00
|
|
|
</a>
|
2016-03-22 02:19:13 +00:00
|
|
|
<div class="result__extras">
|
2015-02-02 16:55:39 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
response = mock.Mock(text=html)
|
|
|
|
results = duckduckgo.response(response)
|
|
|
|
self.assertEqual(type(results), list)
|
2016-03-22 02:19:13 +00:00
|
|
|
self.assertEqual(len(results), 1)
|
|
|
|
self.assertEqual(results[0]['title'], 'This is the title')
|
|
|
|
self.assertEqual(results[0]['url'], u'http://this.should.be.the.link/ű')
|
|
|
|
self.assertEqual(results[0]['content'], 'This should be the content.')
|
2016-12-15 06:34:43 +00:00
|
|
|
|
|
|
|
def test_fetch_supported_languages(self):
|
|
|
|
js = """some code...regions:{
|
|
|
|
"wt-wt":"All Results","ar-es":"Argentina","au-en":"Australia","at-de":"Austria","be-fr":"Belgium (fr)"
|
|
|
|
}some more code..."""
|
|
|
|
response = mock.Mock(text=js)
|
2016-11-30 17:43:03 +00:00
|
|
|
languages = list(duckduckgo._fetch_supported_languages(response))
|
2016-12-15 06:34:43 +00:00
|
|
|
self.assertEqual(len(languages), 5)
|
|
|
|
self.assertIn('wt-WT', languages)
|
|
|
|
self.assertIn('es-AR', languages)
|
|
|
|
self.assertIn('en-AU', languages)
|
|
|
|
self.assertIn('de-AT', languages)
|
|
|
|
self.assertIn('fr-BE', languages)
|