test_deviantart.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 class="tt-a tt-fh tt-boxed" collect_rid="1:149167425"
  25. usericon="http://a.deviantart.net/avatars/t/e/test-0.gif" userid="233301"
  26. username="test-0" symbol="~" category="digitalart/animation">
  27. <span class="tt-w" style="width: auto; max-width: 277px;">
  28. <span class="tt-fh-tc" style="width: 202px;">
  29. <span class="tt-bb" style="width: 202px;">
  30. </span>
  31. <span class="shadow">
  32. <a class="thumb" href="http://url.of.result/2nd.part.of.url"
  33. title="Behoimi BE Animation Test by test-0, Jan 4,
  34. 2010 in Digital Art &gt; Animation"> <i></i>
  35. <img width="200" height="200" alt="Test"
  36. src="http://url.of.thumbnail" data-src="http://th08.deviantart.net/test.jpg">
  37. </a>
  38. </span>
  39. <!-- ^TTT -->
  40. </span>
  41. <span class="details">
  42. <a href="http://test-0.deviantart.com/art/Test" class="t"
  43. title="Behoimi BE Animation Test by test-0, Jan 4, 2010">
  44. <span class="tt-fh-oe">Title of image</span> </a>
  45. <small>
  46. <span class="category">
  47. <span class="age">
  48. 5 years ago
  49. </span>
  50. in <a title="Behoimi BE Animation Test by test-0, Jan 4, 2010"
  51. href="http://www.deviantart.com/browse/all/digitalart/animation/">Animation</a>
  52. </span>
  53. <div class="commentcount">
  54. <a href="http://test-0.deviantart.com/art/Test#comments">
  55. <span class="iconcommentsstats"></span>9 Comments</a>
  56. </div>
  57. <a class="mlt-link" href="http://www.deviantart.com/morelikethis/149167425">
  58. <span class="mlt-icon"></span> <span class="mlt-text">More Like This</span> </a>
  59. </span>
  60. </small> <!-- TTT$ -->
  61. </span>
  62. </div>
  63. """
  64. response = mock.Mock(text=html)
  65. results = deviantart.response(response)
  66. self.assertEqual(type(results), list)
  67. self.assertEqual(len(results), 1)
  68. self.assertEqual(results[0]['title'], 'Title of image')
  69. self.assertEqual(results[0]['url'], 'http://url.of.result/2nd.part.of.url')
  70. self.assertNotIn('content', results[0])
  71. self.assertEqual(results[0]['thumbnail_src'], 'https://url.of.thumbnail')
  72. html = """
  73. <span class="tt-fh-tc" style="width: 202px;">
  74. <span class="tt-bb" style="width: 202px;">
  75. </span>
  76. <span class="shadow">
  77. <a class="thumb" href="http://url.of.result/2nd.part.of.url"
  78. title="Behoimi BE Animation Test by test-0, Jan 4,
  79. 2010 in Digital Art &gt; Animation"> <i></i>
  80. <img width="200" height="200" alt="Test"
  81. src="http://url.of.thumbnail" data-src="http://th08.deviantart.net/test.jpg">
  82. </a>
  83. </span>
  84. <!-- ^TTT -->
  85. </span>
  86. <span class="details">
  87. <a href="http://test-0.deviantart.com/art/Test" class="t"
  88. title="Behoimi BE Animation Test by test-0, Jan 4, 2010">
  89. <span class="tt-fh-oe">Title of image</span> </a>
  90. <small>
  91. <span class="category">
  92. <span class="age">
  93. 5 years ago
  94. </span>
  95. in <a title="Behoimi BE Animation Test by test-0, Jan 4, 2010"
  96. href="http://www.deviantart.com/browse/all/digitalart/animation/">Animation</a>
  97. </span>
  98. <div class="commentcount">
  99. <a href="http://test-0.deviantart.com/art/Test#comments">
  100. <span class="iconcommentsstats"></span>9 Comments</a>
  101. </div>
  102. <a class="mlt-link" href="http://www.deviantart.com/morelikethis/149167425">
  103. <span class="mlt-icon"></span> <span class="mlt-text">More Like This</span> </a>
  104. </span>
  105. </small> <!-- TTT$ -->
  106. """
  107. response = mock.Mock(text=html)
  108. results = deviantart.response(response)
  109. self.assertEqual(type(results), list)
  110. self.assertEqual(len(results), 0)