# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import piratebay
from searx.testing import SearxTestCase
class TestPiratebayEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
dicto['category'] = 'Toto'
params = piratebay.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('piratebay.se', params['url'])
self.assertIn('0', params['url'])
dicto['category'] = 'music'
params = piratebay.request(query, dicto)
self.assertIn('100', params['url'])
def test_response(self):
self.assertRaises(AttributeError, piratebay.response, None)
self.assertRaises(AttributeError, piratebay.response, [])
self.assertRaises(AttributeError, piratebay.response, '')
self.assertRaises(AttributeError, piratebay.response, '[]')
response = mock.Mock(text='')
self.assertEqual(piratebay.response(response), [])
html = """
Anime
(Anime)
|
This is the content and should be OK
|
13 |
334 |
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'https://thepiratebay.se/this.is.the.link')
self.assertEqual(results[0]['content'], 'This is the content and should be OK')
self.assertEqual(results[0]['seed'], 13)
self.assertEqual(results[0]['leech'], 334)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
html = """
Anime
(Anime)
|
This is the content and should be OK
|
s |
d |
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This is the title')
self.assertEqual(results[0]['url'], 'https://thepiratebay.se/this.is.the.link')
self.assertEqual(results[0]['content'], 'This is the content and should be OK')
self.assertEqual(results[0]['seed'], 0)
self.assertEqual(results[0]['leech'], 0)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETLINK')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/TORRENTFILE.torrent')
html = """
"""
response = mock.Mock(text=html)
results = piratebay.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)