test_faroo.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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'] = 'de-DE'
  20. params = faroo.request(query, dicto)
  21. self.assertIn('de', params['url'])
  22. def test_response(self):
  23. self.assertRaises(AttributeError, faroo.response, None)
  24. self.assertRaises(AttributeError, faroo.response, [])
  25. self.assertRaises(AttributeError, faroo.response, '')
  26. self.assertRaises(AttributeError, faroo.response, '[]')
  27. response = mock.Mock(text='{}')
  28. self.assertEqual(faroo.response(response), [])
  29. response = mock.Mock(text='{"data": []}')
  30. self.assertEqual(faroo.response(response), [])
  31. response = mock.Mock(text='{"data": []}', status_code=429)
  32. self.assertRaises(Exception, faroo.response, response)
  33. json = """
  34. {
  35. "results": [
  36. {
  37. "title": "This is the title",
  38. "kwic": "This is the content",
  39. "content": "",
  40. "url": "http://this.is.the.url/",
  41. "iurl": "",
  42. "domain": "css3test.com",
  43. "author": "Jim Dalrymple",
  44. "news": true,
  45. "votes": "10",
  46. "date": 1360622563000,
  47. "related": []
  48. },
  49. {
  50. "title": "This is the title2",
  51. "kwic": "This is the content2",
  52. "content": "",
  53. "url": "http://this.is.the.url2/",
  54. "iurl": "",
  55. "domain": "css3test.com",
  56. "author": "Jim Dalrymple",
  57. "news": false,
  58. "votes": "10",
  59. "related": []
  60. },
  61. {
  62. "title": "This is the title3",
  63. "kwic": "This is the content3",
  64. "content": "",
  65. "url": "http://this.is.the.url3/",
  66. "iurl": "http://upload.wikimedia.org/optimized.jpg",
  67. "domain": "css3test.com",
  68. "author": "Jim Dalrymple",
  69. "news": false,
  70. "votes": "10",
  71. "related": []
  72. }
  73. ],
  74. "query": "test",
  75. "suggestions": [],
  76. "count": 100,
  77. "start": 1,
  78. "length": 10,
  79. "time": "15"
  80. }
  81. """
  82. response = mock.Mock(text=json)
  83. results = faroo.response(response)
  84. self.assertEqual(type(results), list)
  85. self.assertEqual(len(results), 3)
  86. self.assertEqual(results[0]['title'], 'This is the title')
  87. self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
  88. self.assertEqual(results[0]['content'], 'This is the content')
  89. self.assertEqual(results[1]['title'], 'This is the title2')
  90. self.assertEqual(results[1]['url'], 'http://this.is.the.url2/')
  91. self.assertEqual(results[1]['content'], 'This is the content2')
  92. self.assertEqual(results[2]['thumbnail'], 'http://upload.wikimedia.org/optimized.jpg')
  93. json = """
  94. {}
  95. """
  96. response = mock.Mock(text=json)
  97. results = faroo.response(response)
  98. self.assertEqual(type(results), list)
  99. self.assertEqual(len(results), 0)