# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import kickass
from searx.testing import SearxTestCase
class TestKickassEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 1
params = kickass.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('kickass.cd', params['url'])
self.assertFalse(params['verify'])
def test_response(self):
self.assertRaises(AttributeError, kickass.response, None)
self.assertRaises(AttributeError, kickass.response, [])
self.assertRaises(AttributeError, kickass.response, '')
self.assertRaises(AttributeError, kickass.response, '[]')
response = mock.Mock(text='')
self.assertEqual(kickass.response(response), [])
html = """
"""
response = mock.Mock(text=html)
results = kickass.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'This should be the title')
self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
self.assertEqual(results[0]['seed'], 10)
self.assertEqual(results[0]['leech'], 1)
self.assertEqual(results[0]['filesize'], 449)
self.assertEqual(results[0]['files'], 4)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
html = """
"""
response = mock.Mock(text=html)
results = kickass.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """
torrent name |
size
|
files
|
age
|
seed
|
leech
|
|
1 KiB |
4 |
2 years |
10 |
1 |
|
1 MiB |
4 |
2 years |
9 |
1 |
|
1 GiB |
4 |
2 years |
8 |
1 |
|
1 TiB |
4 |
2 years |
7 |
1 |
|
z bytes |
r |
2 years |
a |
t |
"""
response = mock.Mock(text=html)
results = kickass.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 5)
self.assertEqual(results[0]['title'], 'This should be the title')
self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
self.assertEqual(results[0]['seed'], 10)
self.assertEqual(results[0]['leech'], 1)
self.assertEqual(results[0]['files'], 4)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
self.assertEqual(results[0]['filesize'], 1000)
self.assertEqual(results[1]['filesize'], 1000000)
self.assertEqual(results[2]['filesize'], 1000000000)
self.assertEqual(results[3]['filesize'], 1000000000000)
self.assertEqual(results[4]['seed'], 0)
self.assertEqual(results[4]['leech'], 0)
self.assertEqual(results[4]['files'], None)
self.assertEqual(results[4]['filesize'], None)