test_deviantart.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import deviantart
  4. from searx.testing import SearxTestCase
  5. class TestDeviantartEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = deviantart.request(query, dicto)
  11. self.assertTrue('url' in params)
  12. self.assertTrue(query in params['url'])
  13. self.assertTrue('deviantart.com' in params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, deviantart.response, None)
  16. self.assertRaises(AttributeError, deviantart.response, [])
  17. self.assertRaises(AttributeError, deviantart.response, '')
  18. self.assertRaises(AttributeError, deviantart.response, '[]')
  19. response = mock.Mock(text='<html></html>')
  20. self.assertEqual(deviantart.response(response), [])
  21. response = mock.Mock(status_code=302)
  22. self.assertEqual(deviantart.response(response), [])
  23. html = """
  24. <div id="page-1-results" class="page-results results-page-thumb torpedo-container">
  25. <span class="thumb wide" href="http://amai911.deviantart.com/art/Horse-195212845"
  26. data-super-full-width="900" data-super-full-height="600">
  27. <a class="torpedo-thumb-link" href="https://url.of.image">
  28. <img data-sigil="torpedo-img" src="https://url.of.thumbnail" />
  29. </a>
  30. <span class="info"><span class="title-wrap"><span class="title">Title of image</span></span>
  31. </div>
  32. """
  33. response = mock.Mock(text=html)
  34. results = deviantart.response(response)
  35. self.assertEqual(type(results), list)
  36. self.assertEqual(len(results), 1)
  37. self.assertEqual(results[0]['title'], 'Title of image')
  38. self.assertEqual(results[0]['url'], 'https://url.of.image')
  39. self.assertNotIn('content', results[0])
  40. self.assertEqual(results[0]['thumbnail_src'], 'https://url.of.thumbnail')
  41. html = """
  42. <span class="tt-fh-tc" style="width: 202px;">
  43. <span class="tt-bb" style="width: 202px;">
  44. </span>
  45. <span class="shadow">
  46. <a class="thumb" href="http://url.of.result/2nd.part.of.url"
  47. title="Behoimi BE Animation Test by test-0, Jan 4,
  48. 2010 in Digital Art &gt; Animation"> <i></i>
  49. <img width="200" height="200" alt="Test"
  50. src="http://url.of.thumbnail" data-src="http://th08.deviantart.net/test.jpg">
  51. </a>
  52. </span>
  53. <!-- ^TTT -->
  54. </span>
  55. <span class="details">
  56. <a href="http://test-0.deviantart.com/art/Test" class="t"
  57. title="Behoimi BE Animation Test by test-0, Jan 4, 2010">
  58. <span class="tt-fh-oe">Title of image</span> </a>
  59. <small>
  60. <span class="category">
  61. <span class="age">
  62. 5 years ago
  63. </span>
  64. in <a title="Behoimi BE Animation Test by test-0, Jan 4, 2010"
  65. href="http://www.deviantart.com/browse/all/digitalart/animation/">Animation</a>
  66. </span>
  67. <div class="commentcount">
  68. <a href="http://test-0.deviantart.com/art/Test#comments">
  69. <span class="iconcommentsstats"></span>9 Comments</a>
  70. </div>
  71. <a class="mlt-link" href="http://www.deviantart.com/morelikethis/149167425">
  72. <span class="mlt-icon"></span> <span class="mlt-text">More Like This</span> </a>
  73. </span>
  74. </small> <!-- TTT$ -->
  75. """
  76. response = mock.Mock(text=html)
  77. results = deviantart.response(response)
  78. self.assertEqual(type(results), list)
  79. self.assertEqual(len(results), 0)