123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import faroo
  5. from searx.testing import SearxTestCase
  6. class TestFarooEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. dicto['language'] = 'fr_FR'
  12. dicto['category'] = 'general'
  13. params = faroo.request(query, dicto)
  14. self.assertIn('url', params)
  15. self.assertIn(query, params['url'])
  16. self.assertIn('faroo.com', params['url'])
  17. self.assertIn('en', params['url'])
  18. self.assertIn('web', params['url'])
  19. dicto['language'] = 'all'
  20. params = faroo.request(query, dicto)
  21. self.assertIn('en', params['url'])
  22. dicto['language'] = 'de_DE'
  23. params = faroo.request(query, dicto)
  24. self.assertIn('de', params['url'])
  25. def test_response(self):
  26. self.assertRaises(AttributeError, faroo.response, None)
  27. self.assertRaises(AttributeError, faroo.response, [])
  28. self.assertRaises(AttributeError, faroo.response, '')
  29. self.assertRaises(AttributeError, faroo.response, '[]')
  30. response = mock.Mock(text='{}')
  31. self.assertEqual(faroo.response(response), [])
  32. response = mock.Mock(text='{"data": []}')
  33. self.assertEqual(faroo.response(response), [])
  34. response = mock.Mock(text='{"data": []}', status_code=429)
  35. self.assertRaises(Exception, faroo.response, response)
  36. json = """
  37. {
  38. "results": [
  39. {
  40. "title": "This is the title",
  41. "kwic": "This is the content",
  42. "content": "",
  43. "url": "http://this.is.the.url/",
  44. "iurl": "",
  45. "domain": "css3test.com",
  46. "author": "Jim Dalrymple",
  47. "news": true,
  48. "votes": "10",
  49. "date": 1360622563000,
  50. "related": []
  51. },
  52. {
  53. "title": "This is the title2",
  54. "kwic": "This is the content2",
  55. "content": "",
  56. "url": "http://this.is.the.url2/",
  57. "iurl": "",
  58. "domain": "css3test.com",
  59. "author": "Jim Dalrymple",
  60. "news": false,
  61. "votes": "10",
  62. "related": []
  63. },
  64. {
  65. "title": "This is the title3",
  66. "kwic": "This is the content3",
  67. "content": "",
  68. "url": "http://this.is.the.url3/",
  69. "iurl": "http://upload.wikimedia.org/optimized.jpg",
  70. "domain": "css3test.com",
  71. "author": "Jim Dalrymple",
  72. "news": false,
  73. "votes": "10",
  74. "related": []
  75. }
  76. ],
  77. "query": "test",
  78. "suggestions": [],
  79. "count": 100,
  80. "start": 1,
  81. "length": 10,
  82. "time": "15"
  83. }
  84. """
  85. response = mock.Mock(text=json)
  86. results = faroo.response(response)
  87. self.assertEqual(type(results), list)
  88. self.assertEqual(len(results), 3)
  89. self.assertEqual(results[0]['title'], 'This is the title')
  90. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  91. self.assertEqual(results[0]['content'], 'This is the content')
  92. self.assertEqual(results[1]['title'], 'This is the title2')
  93. self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
  94. self.assertEqual(results[1]['content'], 'This is the content2')
  95. self.assertEqual(results[2]['thumbnail'], 'http://upload.wikimedia.org/optimized.jpg')
  96. json = """
  97. {}
  98. """
  99. response = mock.Mock(text=json)
  100. results = faroo.response(response)
  101. self.assertEqual(type(results), list)
  102. self.assertEqual(len(results), 0)