123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- from collections import defaultdict
- import mock
- from searx.engines import qwant
- from searx.testing import SearxTestCase
-
-
- class TestQwantEngine(SearxTestCase):
-
- def test_request(self):
- qwant.supported_languages = ['en-US', 'fr-CA', 'fr-FR']
- query = 'test_query'
- dicto = defaultdict(dict)
- dicto['pageno'] = 0
- dicto['language'] = 'fr-FR'
- qwant.categories = ['']
- params = qwant.request(query, dicto)
- self.assertIn('url', params)
- self.assertIn(query, params['url'])
- self.assertIn('web', params['url'])
- self.assertIn('qwant.com', params['url'])
- self.assertIn('fr_fr', params['url'])
-
- dicto['language'] = 'en-US'
- qwant.categories = ['news']
- params = qwant.request(query, dicto)
- self.assertFalse('fr' in params['url'])
- self.assertIn('en_us', params['url'])
- self.assertIn('news', params['url'])
-
- dicto['language'] = 'fr'
- params = qwant.request(query, dicto)
- self.assertIn('fr_fr', params['url'])
-
- def test_response(self):
- self.assertRaises(AttributeError, qwant.response, None)
- self.assertRaises(AttributeError, qwant.response, [])
- self.assertRaises(AttributeError, qwant.response, '')
- self.assertRaises(AttributeError, qwant.response, '[]')
-
- response = mock.Mock(text='{}')
- self.assertEqual(qwant.response(response), [])
-
- response = mock.Mock(text='{"data": {}}')
- self.assertEqual(qwant.response(response), [])
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "items": [
- {
- "title": "Title",
- "score": 9999,
- "url": "http://www.url.xyz",
- "source": "...",
- "desc": "Description",
- "date": "",
- "_id": "db0aadd62c2a8565567ffc382f5c61fa",
- "favicon": "https://s.qwant.com/fav.ico"
- }
- ],
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- qwant.categories = ['general']
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 1)
- self.assertEqual(results[0]['title'], 'Title')
- self.assertEqual(results[0]['url'], 'http://www.url.xyz')
- self.assertEqual(results[0]['content'], 'Description')
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "items": [
- {
- "title": "Title",
- "score": 9999,
- "url": "http://www.url.xyz",
- "source": "...",
- "media": "http://image.jpg",
- "desc": "",
- "thumbnail": "http://thumbnail.jpg",
- "date": "",
- "_id": "db0aadd62c2a8565567ffc382f5c61fa",
- "favicon": "https://s.qwant.com/fav.ico"
- }
- ],
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- qwant.categories = ['images']
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 1)
- self.assertEqual(results[0]['title'], 'Title')
- self.assertEqual(results[0]['url'], 'http://www.url.xyz')
- self.assertEqual(results[0]['content'], '')
- self.assertEqual(results[0]['thumbnail_src'], 'http://thumbnail.jpg')
- self.assertEqual(results[0]['img_src'], 'http://image.jpg')
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "items": [
- {
- "title": "Title",
- "score": 9999,
- "url": "http://www.url.xyz",
- "source": "...",
- "desc": "Description",
- "date": 1433260920,
- "_id": "db0aadd62c2a8565567ffc382f5c61fa",
- "favicon": "https://s.qwant.com/fav.ico"
- }
- ],
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- qwant.categories = ['news']
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 1)
- self.assertEqual(results[0]['title'], 'Title')
- self.assertEqual(results[0]['url'], 'http://www.url.xyz')
- self.assertEqual(results[0]['content'], 'Description')
- self.assertIn('publishedDate', results[0])
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "items": [
- {
- "title": "Title",
- "score": 9999,
- "url": "http://www.url.xyz",
- "source": "...",
- "desc": "Description",
- "date": 1433260920,
- "_id": "db0aadd62c2a8565567ffc382f5c61fa",
- "favicon": "https://s.qwant.com/fav.ico"
- }
- ],
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- qwant.categories = ['social media']
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 1)
- self.assertEqual(results[0]['title'], 'Title')
- self.assertEqual(results[0]['url'], 'http://www.url.xyz')
- self.assertEqual(results[0]['content'], 'Description')
- self.assertIn('publishedDate', results[0])
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "items": [
- {
- "title": "Title",
- "score": 9999,
- "url": "http://www.url.xyz",
- "source": "...",
- "desc": "Description",
- "date": 1433260920,
- "_id": "db0aadd62c2a8565567ffc382f5c61fa",
- "favicon": "https://s.qwant.com/fav.ico"
- }
- ],
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- qwant.categories = ['']
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 0)
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "result": {
- "filters": []
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 0)
-
- json = """
- {
- "status": "success",
- "data": {
- "query": {
- "locale": "en_us",
- "query": "Test",
- "offset": 10
- },
- "cache": {
- "key": "e66aa864c00147a0e3a16ff7a5efafde",
- "created": 1433092754,
- "expiration": 259200,
- "status": "miss",
- "age": 0
- }
- }
- }
- """
- response = mock.Mock(text=json)
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 0)
-
- json = """
- {
- "status": "success"
- }
- """
- response = mock.Mock(text=json)
- results = qwant.response(response)
- self.assertEqual(type(results), list)
- self.assertEqual(len(results), 0)
-
- def test_fetch_supported_languages(self):
- page = """some code...
- config_set('project.regionalisation', {"continents":{},"languages":
- {"de":{"code":"de","name":"Deutsch","countries":["DE","CH","AT"]},
- "it":{"code":"it","name":"Italiano","countries":["IT","CH"]}}});
- some more code..."""
- response = mock.Mock(text=page)
- languages = qwant._fetch_supported_languages(response)
- self.assertEqual(type(languages), list)
- self.assertEqual(len(languages), 5)
- self.assertIn('de-DE', languages)
- self.assertIn('de-CH', languages)
- self.assertIn('de-AT', languages)
- self.assertIn('it-IT', languages)
- self.assertIn('it-CH', languages)
|