test_deviantart.py 3.8KB

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