test_blekko_images.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import blekko_images
  4. from searx.testing import SearxTestCase
  5. class TestBlekkoImagesEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['safesearch'] = 1
  11. params = blekko_images.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn(query, params['url'])
  14. self.assertIn('blekko.com', params['url'])
  15. self.assertIn('page', params['url'])
  16. dicto['pageno'] = 1
  17. params = blekko_images.request(query, dicto)
  18. self.assertNotIn('page', params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, blekko_images.response, None)
  21. self.assertRaises(AttributeError, blekko_images.response, [])
  22. self.assertRaises(AttributeError, blekko_images.response, '')
  23. self.assertRaises(AttributeError, blekko_images.response, '[]')
  24. response = mock.Mock(text='[]')
  25. self.assertEqual(blekko_images.response(response), [])
  26. json = """
  27. [
  28. {
  29. "c": 1,
  30. "page_url": "http://result_url.html",
  31. "title": "Photo title",
  32. "tn_url": "http://ts1.mm.bing.net/th?id=HN.608050619474382748&pid=15.1",
  33. "url": "http://result_image.jpg"
  34. },
  35. {
  36. "c": 2,
  37. "page_url": "http://companyorange.simpsite.nl/OSM",
  38. "title": "OSM",
  39. "tn_url": "http://ts2.mm.bing.net/th?id=HN.608048068264919461&pid=15.1",
  40. "url": "http://simpsite.nl/userdata2/58985/Home/OSM.bmp"
  41. },
  42. {
  43. "c": 3,
  44. "page_url": "http://invincible.webklik.nl/page/osm",
  45. "title": "OSM",
  46. "tn_url": "http://ts1.mm.bing.net/th?id=HN.608024514657649476&pid=15.1",
  47. "url": "http://www.webklik.nl/user_files/2009_09/65324/osm.gif"
  48. },
  49. {
  50. "c": 4,
  51. "page_url": "http://www.offshorenorway.no/event/companyDetail/id/12492",
  52. "title": "Go to OSM Offshore AS homepage",
  53. "tn_url": "http://ts2.mm.bing.net/th?id=HN.608054265899847285&pid=15.1",
  54. "url": "http://www.offshorenorway.no/firmalogo/OSM-logo.png"
  55. }
  56. ]
  57. """
  58. response = mock.Mock(text=json)
  59. results = blekko_images.response(response)
  60. self.assertEqual(type(results), list)
  61. self.assertEqual(len(results), 4)
  62. self.assertEqual(results[0]['title'], 'Photo title')
  63. self.assertEqual(results[0]['url'], 'http://result_url.html')
  64. self.assertEqual(results[0]['img_src'], 'http://result_image.jpg')