logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://anongit.hacktivis.me/git/searx.git/

test_torrentz.py (2906B)


  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('torrentz2.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>1</span>
  29. <span title="1503595924">5 hours</span>
  30. <span>30 MB</span>
  31. <span>14</span>
  32. <span>1</span>
  33. </dd>
  34. </dl>
  35. <dl>
  36. <dt>
  37. <a href="/poaskdpokaspod">
  38. Invalid hash and date and filesize
  39. </a>
  40. books ebooks
  41. </dt>
  42. <dd>
  43. <span>1</span>
  44. <span title="1503595924 aaa">5 hours</span>
  45. <span>30MB</span>
  46. <span>5,555</span>
  47. <span>1,234,567</span>
  48. </dd>
  49. </dl>
  50. </div>
  51. """
  52. resp = mock.Mock(text=html)
  53. results = torrentz.response(resp)
  54. self.assertEqual(type(results), list)
  55. self.assertEqual(len(results), 2)
  56. # testing against the first result
  57. r = results[0]
  58. self.assertEqual(r['url'], 'https://torrentz2.eu/4362e08b1d80e1820fb2550b752f9f3126fe76d6')
  59. self.assertEqual(r['title'], 'Completely valid info books ebooks')
  60. # 22 Nov 2015 03:01:42
  61. self.assertEqual(r['publishedDate'], datetime.fromtimestamp(1503595924))
  62. self.assertEqual(r['seed'], 14)
  63. self.assertEqual(r['leech'], 1)
  64. self.assertEqual(r['filesize'], 30 * 1024 * 1024)
  65. self.assertEqual(r['magnetlink'], 'magnet:?xt=urn:btih:4362e08b1d80e1820fb2550b752f9f3126fe76d6')
  66. # testing against the second result
  67. r = results[1]
  68. self.assertEqual(r['url'], 'https://torrentz2.eu/poaskdpokaspod')
  69. self.assertEqual(r['title'], 'Invalid hash and date and filesize books ebooks')
  70. self.assertEqual(r['seed'], 5555)
  71. self.assertEqual(r['leech'], 1234567)
  72. # in the second result we have invalid hash, creation date & torrent size,
  73. # so these tests should fail
  74. self.assertFalse('magnetlink' in r)
  75. self.assertFalse('filesize' in r)
  76. self.assertFalse('publishedDate' in r)