123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import digg
  4. from searx.testing import SearxTestCase
  5. class TestDiggEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 1
  10. params = digg.request(query, dicto)
  11. self.assertIn('url', params)
  12. self.assertIn(query, params['url'])
  13. self.assertIn('digg.com', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, digg.response, None)
  16. self.assertRaises(AttributeError, digg.response, [])
  17. self.assertRaises(AttributeError, digg.response, '')
  18. self.assertRaises(AttributeError, digg.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(digg.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(digg.response(response), [])
  23. json = """
  24. {
  25. "status": "ok",
  26. "num": 10,
  27. "next_position": 20,
  28. "html": "<article itemscope itemtype=\\"http://schema.org/Article\\"
  29. class=\\"story-container digg-story-el hentry entry story-1sRANah col-1\\"
  30. data-content-id=\\"1sRANah\\" data-contenturl=\\"http://url.of.link\\"
  31. data-position=\\"0\\" data-diggs=\\"24\\" data-tweets=\\"69\\"
  32. data-digg-score=\\"1190\\"> <div class=\\"story-image story-image-thumb\\">
  33. <a data-position=\\"0\\" data-content-id=\\"1sRANah\\"
  34. class=\\"story-link\\" href=\\"http://www.thedailybeast.com/\\"
  35. target=\\"_blank\\"><img class=\\"story-image-img\\"
  36. src=\\"http://url.of.image.jpeg\\" width=\\"312\\" height=\\"170\\"
  37. alt=\\"\\" /> </a> </div> <div class=\\"story-content\\"><header
  38. class=\\"story-header\\"> <div itemprop=\\"alternativeHeadline\\"
  39. class=\\"story-kicker\\" >Kicker</div> <h2 itemprop=\\"headline\\"
  40. class=\\"story-title entry-title\\"><a class=\\"story-title-link story-link\\"
  41. rel=\\"bookmark\\" itemprop=\\"url\\" href=\\"http://www.thedailybeast.com/\\"
  42. target=\\"_blank\\">Title of article</h2> <div class=\\"story-meta\\">
  43. <div class=\\"story-score \\">
  44. <div class=\\"story-score-diggscore diggscore-1sRANah\\">1190</div>
  45. <div class=\\"story-score-details\\"> <div class=\\"arrow\\"></div>
  46. <ul class=\\"story-score-details-list\\"> <li
  47. class=\\"story-score-detail story-score-diggs\\"><span
  48. class=\\"label\\">Diggs:</span> <span class=\\"count diggs-1sRANah\\">24</span>
  49. </li> <li class=\\"story-score-detail story-score-twitter\\"><span
  50. class=\\"label\\">Tweets:</span> <span class=\\"count tweets-1sRANah\\">69</span>
  51. </li> <li class=\\"story-score-detail story-score-facebook\\"><span
  52. class=\\"label\\">Facebook Shares:</span> <span
  53. class=\\"count fb_shares-1sRANah\\">1097</span></li> </ul> </div> </div>
  54. <span class=\\"story-meta-item story-source\\"> <a
  55. itemprop=\\"publisher copyrightHolder sourceOrganization provider\\"
  56. class=\\"story-meta-item-link story-source-link\\"
  57. href=\\"/source/thedailybeast.com\\">The Daily Beast </a> </span>
  58. <span class=\\"story-meta-item story-tag first-tag\\"> <a
  59. itemprop=\\"keywords\\" rel=\\"tag\\"
  60. class=\\"story-meta-item-link story-tag-link\\" href=\\"/tag/news\\">News</a>
  61. </span> <abbr class=\\"published story-meta-item story-timestamp\\"
  62. title=\\"2014-10-18 14:53:45\\"> <time datetime=\\"2014-10-18 14:53:45\\">18 Oct 2014</time>
  63. </abbr> </div> </header> </div> <ul class=\\"story-actions\\"> <li
  64. class=\\"story-action story-action-digg btn-story-action-container\\">
  65. <a class=\\"target digg-1sRANah\\" href=\\"#\\">Digg</a></li> <li
  66. class=\\"story-action story-action-save btn-story-action-container\\">
  67. <a class=\\"target save-1sRANah\\" href=\\"#\\">Save</a></li> <li
  68. class=\\"story-action story-action-share\\"><a
  69. class=\\"target share-facebook\\" href=\\"https://www.facebook.com/\\">Facebook</a></li>
  70. <li class=\\"story-action story-action-share\\"><a class=\\"target share-twitter\\"
  71. href=\\"https://twitter.com/\\">Twitter</a></li> </ul> </article>"
  72. }
  73. """
  74. json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
  75. response = mock.Mock(text=json)
  76. results = digg.response(response)
  77. self.assertEqual(type(results), list)
  78. self.assertEqual(len(results), 1)
  79. self.assertEqual(results[0]['title'], 'Title of article')
  80. self.assertEqual(results[0]['url'], 'http://url.of.link')
  81. self.assertEqual(results[0]['thumbnail'], 'http://url.of.image.jpeg')
  82. self.assertEqual(results[0]['content'], '')
  83. json = """
  84. {
  85. "status": "error",
  86. "num": 10,
  87. "next_position": 20
  88. }
  89. """
  90. response = mock.Mock(text=json)
  91. results = digg.response(response)
  92. self.assertEqual(type(results), list)
  93. self.assertEqual(len(results), 0)