1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import digbt
  4. from searx.testing import SearxTestCase
  5. class TestDigBTEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = digbt.request(query, dicto)
  11. self.assertIn('url', params)
  12. self.assertIn(query, params['url'])
  13. self.assertIn('digbt.org', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, digbt.response, None)
  16. self.assertRaises(AttributeError, digbt.response, [])
  17. self.assertRaises(AttributeError, digbt.response, '')
  18. self.assertRaises(AttributeError, digbt.response, '[]')
  19. response = mock.Mock(content='<html></html>')
  20. self.assertEqual(digbt.response(response), [])
  21. html = """
  22. <table class="table">
  23. <tr><td class="x-item">
  24. <div>
  25. <a title="The Big Bang Theory" class="title" href="/The-Big-Bang-Theory-d2.html">The Big Bang Theory</a>
  26. <span class="ctime"><span style="color:red;">4 hours ago</span></span>
  27. </div>
  28. <div class="files">
  29. <ul>
  30. <li>The Big Bang Theory 2.9 GB</li>
  31. <li>....</li>
  32. </ul>
  33. </div>
  34. <div class="tail">
  35. Files: 1 Size: 2.9 GB Downloads: 1 Updated: <span style="color:red;">4 hours ago</span>
  36. &nbsp; &nbsp;
  37. <a class="title" href="magnet:?xt=urn:btih:a&amp;dn=The+Big+Bang+Theory">
  38. <span class="glyphicon glyphicon-magnet"></span> magnet-link
  39. </a>
  40. &nbsp; &nbsp;
  41. </div>
  42. </td></tr>
  43. </table>
  44. """
  45. response = mock.Mock(content=html)
  46. results = digbt.response(response)
  47. self.assertEqual(type(results), list)
  48. self.assertEqual(len(results), 1)
  49. self.assertEqual(results[0]['title'], 'The Big Bang Theory')
  50. self.assertEqual(results[0]['url'], 'https://digbt.org/The-Big-Bang-Theory-d2.html')
  51. self.assertEqual(results[0]['content'], 'The Big Bang Theory 2.9 GB ....')
  52. self.assertEqual(results[0]['filesize'], 3113851289)
  53. self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:a&dn=The+Big+Bang+Theory')