test_yacy.py 3.8 KB

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