test_google_images.py 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import google_images
  4. from searx.testing import SearxTestCase
  5. class TestGoogleImagesEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. dicto['safesearch'] = 1
  11. dicto['time_range'] = ''
  12. params = google_images.request(query, dicto)
  13. self.assertIn('url', params)
  14. self.assertIn(query, params['url'])
  15. self.assertIn('safe=active', params['url'])
  16. dicto['safesearch'] = 0
  17. params = google_images.request(query, dicto)
  18. self.assertNotIn('safe', params['url'])
  19. def test_response(self):
  20. self.assertRaises(AttributeError, google_images.response, None)
  21. self.assertRaises(AttributeError, google_images.response, [])
  22. self.assertRaises(AttributeError, google_images.response, '')
  23. self.assertRaises(AttributeError, google_images.response, '[]')
  24. response = mock.Mock(text='<div></div>')
  25. self.assertEqual(google_images.response(response), [])
  26. html = """
  27. <div style="display:none">
  28. <div eid="fWhnVq4Shqpp3pWo4AM" id="isr_scm_1" style="display:none"></div>
  29. <div data-cei="fWhnVq4Shqpp3pWo4AM" class="rg_add_chunk"><!--m-->
  30. <div class="rg_di rg_el ivg-i" data-ved="0ahUKEwjuxPWQts3JAhUGVRoKHd4KCjwQMwgDKAAwAA">
  31. <a href="/imgres?imgurl=http://www.clker.com/cliparts/H/X/l/b/0/0/south-arrow-hi.png&amp;imgrefurl=http://www.clker.com/clipart-south-arrow.html&amp;h=598&amp;w=504&amp;tbnid=bQWQ9wz9loJmjM:&amp;docid=vlONkeBtERfDuM&amp;ei=fWhnVq4Shqpp3pWo4AM&amp;tbm=isch" jsaction="fire.ivg_o;mouseover:str.hmov;mouseout:str.hmou" class="rg_l"><img data-src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRsxy3gKnEX0lrwwpRxdPWyLJ8iZ--PXZ-ThbBA2_xXDG_bdQutMQ" data-sz="f" name="bQWQ9wz9loJmjM:" class="rg_i" alt="Image result for south" jsaction="load:str.tbn" onload="google.aft&&google.aft(this)">
  32. <div class="_aOd rg_ilm">
  33. <div class="rg_ilmbg"><span class="rg_ilmn"> 504&nbsp;&#215;&nbsp;598 - clker.com </span>
  34. </div>
  35. </div>
  36. </a>
  37. <div class="rg_meta">
  38. {"id":"bQWQ9wz9loJmjM:","isu":"clker.com","ity":"png","md":"/search?tbs\u003dsbi:AMhZZit7u1mHyop9pQisu-5idR-8W_1Itvwc3afChmsjQYPx_1yYMzBvUZgtkcGoojqekKZ-6n_1rjX9ySH0OWA_1eO5OijFY6BBDw_1GApr6xxb1bXJcBcj-DiguMoXWW7cZSG7MRQbwnI5SoDZNXcv_1xGszy886I7NVb_1oRKSliTHtzqbXAxhvYreM","msu":"/search?q\u003dsouth\u0026biw\u003d1364\u0026bih\u003d235\u0026tbm\u003disch\u0026tbs\u003dsimg:CAQSEgltBZD3DP2WgiG-U42R4G0RFw","oh":598,"os":"13KB","ow":504,"pt":"South Arrow Clip Art at Clker.com - vector clip art online ...","rid":"vlONkeBtERfDuM","s":"Download this image as:","sc":1,"si":"/search?q\u003dsouth\u0026biw\u003d1364\u0026bih\u003d235\u0026tbm\u003disch\u0026tbs\u003dsimg:CAESEgltBZD3DP2WgiG-U42R4G0RFw","th":245,"tu":"https://thumbnail.url/","tw":206,"ru":"a","ou":"b"}
  39. </div>
  40. </div><!--n--><!--m-->
  41. </div>
  42. </div>
  43. """ # noqa
  44. response = mock.Mock(text=html)
  45. results = google_images.response(response)
  46. self.assertEqual(type(results), list)
  47. self.assertEqual(len(results), 1)
  48. self.assertEqual(results[0]['title'], u'South Arrow Clip Art at Clker.com - vector clip art online ...')
  49. self.assertEqual(results[0]['url'], 'a')
  50. self.assertEqual(results[0]['thumbnail_src'], 'https://thumbnail.url/')
  51. self.assertEqual(results[0]['img_src'], 'b')
  52. self.assertEqual(results[0]['content'], 'Download this image as:')