test_torrentz.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import mock
  2. from collections import defaultdict
  3. from searx.engines import torrentz
  4. from searx.testing import SearxTestCase
  5. from datetime import datetime
  6. class TestTorrentzEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dic = defaultdict(dict)
  10. dic['pageno'] = 1
  11. params = torrentz.request(query, dic)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('torrentz.eu' in params['url'])
  15. def test_response(self):
  16. resp = mock.Mock(text='<html></html>')
  17. self.assertEqual(torrentz.response(resp), [])
  18. html = """
  19. <div class="results">
  20. <dl>
  21. <dt>
  22. <a href="/4362e08b1d80e1820fb2550b752f9f3126fe76d6">
  23. Completely valid info
  24. </a>
  25. books ebooks
  26. </dt>
  27. <dd>
  28. <span class="v">1</span>
  29. <span class="a">
  30. <span title="Sun, 22 Nov 2015 03:01:42">4 months</span>
  31. </span>
  32. <span class="s">30 MB</span>
  33. <span class="u">14</span>
  34. <span class="d">1</span>
  35. </dd>
  36. </dl>
  37. <dl>
  38. <dt>
  39. <a href="/poaskdpokaspod">
  40. Invalid hash and date and filesize
  41. </a>
  42. books ebooks
  43. </dt>
  44. <dd>
  45. <span class="v">1</span>
  46. <span class="a">
  47. <span title="Sun, 2124091j0j190gm42">4 months</span>
  48. </span>
  49. <span class="s">30MB</span>
  50. <span class="u">5,555</span>
  51. <span class="d">1,234,567</span>
  52. </dd>
  53. </dl>
  54. </div>
  55. """
  56. resp = mock.Mock(text=html)
  57. results = torrentz.response(resp)
  58. self.assertEqual(type(results), list)
  59. self.assertEqual(len(results), 2)
  60. # testing against the first result
  61. r = results[0]
  62. self.assertEqual(r['url'], 'https://torrentz.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
  63. self.assertEqual(r['title'], 'Completely valid info books ebooks')
  64. # 22 Nov 2015 03:01:42
  65. self.assertEqual(r['publishedDate'], datetime(2015, 11, 22, 3, 1, 42))
  66. self.assertEqual(r['seed'], 14)
  67. self.assertEqual(r['leech'], 1)
  68. self.assertEqual(r['filesize'], 30 * 1024 * 1024)
  69. self.assertEqual(r['magnetlink'], 'magnet:?xt=urn:btih:4362e08b1d80e1820fb2550b752f9f3126fe76d6')
  70. # testing against the second result
  71. r = results[1]
  72. self.assertEqual(r['url'], 'https://torrentz.eu/poaskdpokaspod')
  73. self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
  74. self.assertEqual(r['seed'], 5555)
  75. self.assertEqual(r['leech'], 1234567)
  76. # in the second result we have invalid hash, creation date & torrent size,
  77. # so these tests should fail
  78. self.assertFalse('magnetlink' in r)
  79. self.assertFalse('filesize' in r)
  80. self.assertFalse('publishedDate' in r)