test_www500px.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import www500px
  5. from searx.testing import SearxTestCase
  6. class TestWww500pxImagesEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = www500px.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('500px.com' in params['url'])
  15. def test_response(self):
  16. self.assertRaises(AttributeError, www500px.response, None)
  17. self.assertRaises(AttributeError, www500px.response, [])
  18. self.assertRaises(AttributeError, www500px.response, '')
  19. self.assertRaises(AttributeError, www500px.response, '[]')
  20. response = mock.Mock(text='<html></html>')
  21. self.assertEqual(www500px.response(response), [])
  22. html = """
  23. <div class="photo">
  24. <a href="/this.should.be.the.url" data-ga-category="Photo Thumbnail" data-ga-action="Title">
  25. <img src="https://image.url/3.jpg?v=0" />
  26. </a>
  27. <div class="details">
  28. <div class="inside">
  29. <div class="title">
  30. <a href="/photo/64312705/branch-out-by-oliver-turpin?feature=">
  31. This is the title
  32. </a>
  33. </div>
  34. <div class="info">
  35. <a href="/ChronicleUK" data-ga-action="Image" data-ga-category="Photo Thumbnail">
  36. This is the content
  37. </a>
  38. </div>
  39. <div class="rating">44.8</div>
  40. </div>
  41. </div>
  42. </div>
  43. """
  44. response = mock.Mock(text=html)
  45. results = www500px.response(response)
  46. self.assertEqual(type(results), list)
  47. self.assertEqual(len(results), 1)
  48. self.assertEqual(results[0]['title'], 'This is the title')
  49. self.assertEqual(results[0]['url'], 'https://500px.com/this.should.be.the.url')
  50. self.assertEqual(results[0]['content'], 'This is the content')
  51. self.assertEqual(results[0]['thumbnail_src'], 'https://image.url/3.jpg?v=0')
  52. self.assertEqual(results[0]['img_src'], 'https://image.url/2048.jpg')
  53. html = """
  54. <a href="/this.should.be.the.url" data-ga-category="Photo Thumbnail" data-ga-action="Title">
  55. <img src="https://image.url/3.jpg?v=0" />
  56. </a>
  57. <div class="details">
  58. <div class="inside">
  59. <div class="title">
  60. <a href="/photo/64312705/branch-out-by-oliver-turpin?feature=">
  61. This is the title
  62. </a>
  63. </div>
  64. <div class="info">
  65. <a href="/ChronicleUK" data-ga-action="Image" data-ga-category="Photo Thumbnail">
  66. Oliver Turpin
  67. </a>
  68. </div>
  69. <div class="rating">44.8</div>
  70. </div>
  71. </div>
  72. """
  73. response = mock.Mock(text=html)
  74. results = www500px.response(response)
  75. self.assertEqual(type(results), list)
  76. self.assertEqual(len(results), 0)