BTDigg's unit test

This commit is contained in:
Cqoicebordel 2015-01-30 19:52:44 +01:00
parent 5761d6f0ab
commit a3d444ab85
3 changed files with 388 additions and 8 deletions

View File

@ -23,11 +23,6 @@ paging = True
url = 'https://btdigg.org'
search_url = url + '/search?q={search_term}&p={pageno}'
# specific xpath variables
magnet_xpath = './/a[@title="Torrent magnet link"]'
torrent_xpath = './/a[@title="Download torrent file"]'
content_xpath = './/span[@class="font11px lightgrey block"]'
# do search-request
def request(query, params):
@ -52,8 +47,8 @@ def response(resp):
# parse results
for result in search_res:
link = result.xpath('.//td[@class="torrent_name"]//a')[0]
href = urljoin(url, link.attrib['href'])
title = escape(extract_text(link.xpath('.//text()')))
href = urljoin(url, link.attrib.get('href'))
title = escape(extract_text(link))
content = escape(extract_text(result.xpath('.//pre[@class="snippet"]')[0]))
content = "<br />".join(content.split("\n"))
@ -81,7 +76,7 @@ def response(resp):
filesize = int(filesize * 1024 * 1024 * 1024)
elif filesize_multiplier == 'MB':
filesize = int(filesize * 1024 * 1024)
elif filesize_multiplier == 'kb':
elif filesize_multiplier == 'KB':
filesize = int(filesize * 1024)
except:
filesize = None

View File

@ -0,0 +1,384 @@
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import btdigg
from searx.testing import SearxTestCase
class TestBtdiggEngine(SearxTestCase):
def test_request(self):
query = 'test_query'
dicto = defaultdict(dict)
dicto['pageno'] = 0
params = btdigg.request(query, dicto)
self.assertIn('url', params)
self.assertIn(query, params['url'])
self.assertIn('btdigg.org', params['url'])
def test_response(self):
self.assertRaises(AttributeError, btdigg.response, None)
self.assertRaises(AttributeError, btdigg.response, [])
self.assertRaises(AttributeError, btdigg.response, '')
self.assertRaises(AttributeError, btdigg.response, '[]')
response = mock.Mock(text='<html></html>')
self.assertEqual(btdigg.response(response), [])
html = """
<div id="search_res">
<table>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">8 B</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">710</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">5</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
</table>
</div>
"""
response = mock.Mock(text=html)
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['title'], 'Should be the title')
self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
self.assertEqual(results[0]['content'], 'Content')
self.assertEqual(results[0]['seed'], 5)
self.assertEqual(results[0]['leech'], 0)
self.assertEqual(results[0]['filesize'], 8)
self.assertEqual(results[0]['files'], 710)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')
html = """
<div id="search_res">
<table>
</table>
</div>
"""
response = mock.Mock(text=html)
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = """
<div id="search_res">
<table>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">1 KB</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">710</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">5</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">1 MB</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">a</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">4</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">1 GB</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">710</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">3</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">1 TB</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">710</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">2</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
<tr>
<td class="idx">1</td>
<td>
<table class="torrent_name_tbl">
<tr>
<td class="torrent_name">
<a href="/url">Should be the title</a>
</td>
</tr>
</table>
<table class="torrent_name_tbl">
<tr>
<td class="ttth">
<a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
title="Télécharger des liens Magnet">[magnet]</a>
</td>
<td class="ttth">
<a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
target="_blank" title="Ajouter à BTCloud">[cloud]</a>
</td>
<td>
<span class="attr_name">Taille:</span>
<span class="attr_val">a TB</span>
</td>
<td>
<span class="attr_name">Fichiers:</span>
<span class="attr_val">710</span>
</td>
<td>
<span class="attr_name">Téléchargements:</span>
<span class="attr_val">z</span>
</td>
<td>
<span class="attr_name">Temps:</span>
<span class="attr_val">417.8&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
<span class="attr_val">5.3&nbsp;jours</span>
</td>
<td>
<span class="attr_name">Faux:</span>
<span class="attr_val">Aucun</span>
</td>
</tr>
</table>
<pre class="snippet">
Content
</pre>
</td>
</tr>
</table>
</div>
"""
response = mock.Mock(text=html)
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 5)
self.assertEqual(results[0]['title'], 'Should be the title')
self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
self.assertEqual(results[0]['content'], 'Content')
self.assertEqual(results[0]['seed'], 5)
self.assertEqual(results[0]['leech'], 0)
self.assertEqual(results[0]['files'], 710)
self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')
self.assertEqual(results[0]['filesize'], 1024)
self.assertEqual(results[1]['filesize'], 1048576)
self.assertEqual(results[2]['filesize'], 1073741824)
self.assertEqual(results[3]['filesize'], 1099511627776)

View File

@ -1,6 +1,7 @@
from searx.tests.engines.test_bing import * # noqa
from searx.tests.engines.test_bing_images import * # noqa
from searx.tests.engines.test_bing_news import * # noqa
from searx.tests.engines.test_btdigg import * # noqa
from searx.tests.engines.test_dailymotion import * # noqa
from searx.tests.engines.test_deezer import * # noqa
from searx.tests.engines.test_deviantart import * # noqa