test_faroo.py 3.9 KB

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