# -*- 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='')
self.assertEqual(btdigg.response(response), [])
html = u"""
1 |
[magnet]
|
[cloud]
|
Taille:
8 B
|
Fichiers:
710
|
Téléchargements:
5
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
"""
response = mock.Mock(text=html.encode('utf-8'))
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 = """
"""
response = mock.Mock(text=html.encode('utf-8'))
results = btdigg.response(response)
self.assertEqual(type(results), list)
self.assertEqual(len(results), 0)
html = u"""
1 |
[magnet]
|
[cloud]
|
Taille:
1 KB
|
Fichiers:
710
|
Téléchargements:
5
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
1 |
[magnet]
|
[cloud]
|
Taille:
1 MB
|
Fichiers:
a
|
Téléchargements:
4
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
1 |
[magnet]
|
[cloud]
|
Taille:
1 GB
|
Fichiers:
710
|
Téléchargements:
3
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
1 |
[magnet]
|
[cloud]
|
Taille:
1 TB
|
Fichiers:
710
|
Téléchargements:
2
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
1 |
[magnet]
|
[cloud]
|
Taille:
a TB
|
Fichiers:
710
|
Téléchargements:
z
|
Temps:
417.8 jours
|
Dernière mise à jour:
5.3 jours
|
Faux:
Aucun
|
Content
|
"""
response = mock.Mock(text=html.encode('utf-8'))
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)