12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import yacy
  4. from searx.testing import SearxTestCase
  5. class TestYacyEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. dicto['language'] = 'fr_FR'
  11. params = yacy.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('localhost', params['url'])
  15. self.assertIn('fr', params['url'])
  16. def test_response(self):
  17. self.assertRaises(AttributeError, yacy.response, None)
  18. self.assertRaises(AttributeError, yacy.response, [])
  19. self.assertRaises(AttributeError, yacy.response, '')
  20. self.assertRaises(AttributeError, yacy.response, '[]')
  21. response = mock.Mock(text='{}')
  22. self.assertEqual(yacy.response(response), [])
  23. response = mock.Mock(text='{"data": []}')
  24. self.assertEqual(yacy.response(response), [])
  25. json = """
  26. {
  27. "channels": [
  28. {
  29. "title": "YaCy P2P-Search for test",
  30. "description": "Search for test",
  31. "link": "http://search.yacy.de:7001/yacysearch.html?query=test&resource=global&contentdom=0",
  32. "image": {
  33. "url": "http://search.yacy.de:7001/env/grafics/yacy.png",
  34. "title": "Search for test",
  35. "link": "http://search.yacy.de:7001/yacysearch.html?query=test&resource=global&contentdom=0"
  36. },
  37. "totalResults": "249",
  38. "startIndex": "0",
  39. "itemsPerPage": "5",
  40. "searchTerms": "test",
  41. "items": [
  42. {
  43. "title": "This is the title",
  44. "link": "http://this.is.the.url",
  45. "code": "",
  46. "description": "This should be the content",
  47. "pubDate": "Sat, 08 Jun 2013 02:00:00 +0200",
  48. "size": "44213",
  49. "sizename": "43 kbyte",
  50. "guid": "lzh_1T_5FP-A",
  51. "faviconCode": "XTS4uQ_5FP-A",
  52. "host": "www.gamestar.de",
  53. "path": "/spiele/city-of-heroes-freedom/47019.html",
  54. "file": "47019.html",
  55. "urlhash": "lzh_1T_5FP-A",
  56. "ranking": "0.20106804"
  57. },
  58. {
  59. "title": "This is the title2",
  60. "icon": "/ViewImage.png?maxwidth=96&maxheight=96&code=7EbAbW6BpPOA",
  61. "image": "http://image.url/image.png",
  62. "cache": "/ViewImage.png?quadratic=&url=http://golem.ivwbox.de/cgi-bin/ivw/CP/G_INET?d=14071378",
  63. "url": "http://this.is.the.url",
  64. "urlhash": "7EbAbW6BpPOA",
  65. "host": "www.golem.de",
  66. "width": "-1",
  67. "height": "-1"
  68. }
  69. ]
  70. }
  71. ]
  72. }
  73. """
  74. response = mock.Mock(text=json)
  75. results = yacy.response(response)
  76. self.assertEqual(type(results), list)
  77. self.assertEqual(len(results), 2)
  78. self.assertEqual(results[0]['title'], 'This is the title')
  79. self.assertEqual(results[0]['url'], 'http://this.is.the.url')
  80. self.assertEqual(results[0]['content'], 'This should be the content')
  81. self.assertEqual(results[1]['img_src'], 'http://image.url/image.png')
  82. self.assertEqual(results[1]['content'], '')
  83. self.assertEqual(results[1]['url'], 'http://this.is.the.url')
  84. self.assertEqual(results[1]['title'], 'This is the title2')