summaryrefslogtreecommitdiff
path: root/searx/tests
diff options
context:
space:
mode:
authorCqoicebordel <Cqoicebordel@users.noreply.github.com>2015-05-31 00:25:59 +0200
committerCqoicebordel <Cqoicebordel@users.noreply.github.com>2015-05-31 00:25:59 +0200
commitf965c978222cf48e8dd4b7dd6c9a28ccca9bc62f (patch)
treec2bd2e7e17cc3090404390694f7127a580b589a8 /searx/tests
parentaac8d3a7bfdd77a5369e52a4ece99b20669a4625 (diff)
downloadsearxng-f965c978222cf48e8dd4b7dd6c9a28ccca9bc62f.tar.gz
searxng-f965c978222cf48e8dd4b7dd6c9a28ccca9bc62f.zip
Adds two engines : Youtube with or without API
The API needs an API_KEY The NOAPI doesn't have the published dates.
Diffstat (limited to 'searx/tests')
-rw-r--r--searx/tests/engines/test_youtube_api.py111
-rw-r--r--searx/tests/engines/test_youtube_noapi.py103
-rw-r--r--searx/tests/test_engines.py2
3 files changed, 216 insertions, 0 deletions
diff --git a/searx/tests/engines/test_youtube_api.py b/searx/tests/engines/test_youtube_api.py
new file mode 100644
index 000000000..0d4d478c3
--- /dev/null
+++ b/searx/tests/engines/test_youtube_api.py
@@ -0,0 +1,111 @@
+from collections import defaultdict
+import mock
+from searx.engines import youtube_api
+from searx.testing import SearxTestCase
+
+
+class TestYoutubeAPIEngine(SearxTestCase):
+
+ def test_request(self):
+ query = 'test_query'
+ dicto = defaultdict(dict)
+ dicto['pageno'] = 0
+ dicto['language'] = 'fr_FR'
+ params = youtube_api.request(query, dicto)
+ self.assertTrue('url' in params)
+ self.assertTrue(query in params['url'])
+ self.assertIn('googleapis.com', params['url'])
+ self.assertIn('youtube', params['url'])
+ self.assertIn('fr', params['url'])
+
+ dicto['language'] = 'all'
+ params = youtube_api.request(query, dicto)
+ self.assertFalse('fr' in params['url'])
+
+ def test_response(self):
+ self.assertRaises(AttributeError, youtube_api.response, None)
+ self.assertRaises(AttributeError, youtube_api.response, [])
+ self.assertRaises(AttributeError, youtube_api.response, '')
+ self.assertRaises(AttributeError, youtube_api.response, '[]')
+
+ response = mock.Mock(text='{}')
+ self.assertEqual(youtube_api.response(response), [])
+
+ response = mock.Mock(text='{"data": []}')
+ self.assertEqual(youtube_api.response(response), [])
+
+ json = """
+ {
+ "kind": "youtube#searchListResponse",
+ "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/YJQDcTBCDcaBvl-sRZJoXdvy1ME",
+ "nextPageToken": "CAUQAA",
+ "pageInfo": {
+ "totalResults": 1000000,
+ "resultsPerPage": 20
+ },
+ "items": [
+ {
+ "kind": "youtube#searchResult",
+ "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/IbLO64BMhbHIgWLwLw7MDYe7Hs4",
+ "id": {
+ "kind": "youtube#video",
+ "videoId": "DIVZCPfAOeM"
+ },
+ "snippet": {
+ "publishedAt": "2015-05-29T22:41:04.000Z",
+ "channelId": "UCNodmx1ERIjKqvcJLtdzH5Q",
+ "title": "Title",
+ "description": "Description",
+ "thumbnails": {
+ "default": {
+ "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/default.jpg"
+ },
+ "medium": {
+ "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/mqdefault.jpg"
+ },
+ "high": {
+ "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg"
+ }
+ },
+ "channelTitle": "MinecraftUniverse",
+ "liveBroadcastContent": "none"
+ }
+ }
+ ]
+ }
+ """
+ response = mock.Mock(text=json)
+ results = youtube_api.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 1)
+ self.assertEqual(results[0]['title'], 'Title')
+ self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
+ self.assertEqual(results[0]['content'], 'Description')
+ self.assertEqual(results[0]['thumbnail'], 'https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg')
+ self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
+
+ json = """
+ {
+ "kind": "youtube#searchListResponse",
+ "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/YJQDcTBCDcaBvl-sRZJoXdvy1ME",
+ "nextPageToken": "CAUQAA",
+ "pageInfo": {
+ "totalResults": 1000000,
+ "resultsPerPage": 20
+ }
+ }
+ """
+ response = mock.Mock(text=json)
+ results = youtube_api.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
+
+ json = """
+ {"toto":{"entry":[]
+ }
+ }
+ """
+ response = mock.Mock(text=json)
+ results = youtube_api.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
diff --git a/searx/tests/engines/test_youtube_noapi.py b/searx/tests/engines/test_youtube_noapi.py
new file mode 100644
index 000000000..b715ed2f1
--- /dev/null
+++ b/searx/tests/engines/test_youtube_noapi.py
@@ -0,0 +1,103 @@
+# -*- coding: utf-8 -*-
+from collections import defaultdict
+import mock
+from searx.engines import youtube_noapi
+from searx.testing import SearxTestCase
+
+
+class TestYoutubeNoAPIEngine(SearxTestCase):
+
+ def test_request(self):
+ query = 'test_query'
+ dicto = defaultdict(dict)
+ dicto['pageno'] = 0
+ params = youtube_noapi.request(query, dicto)
+ self.assertIn('url', params)
+ self.assertIn(query, params['url'])
+ self.assertIn('youtube.com', params['url'])
+
+ def test_response(self):
+ self.assertRaises(AttributeError, youtube_noapi.response, None)
+ self.assertRaises(AttributeError, youtube_noapi.response, [])
+ self.assertRaises(AttributeError, youtube_noapi.response, '')
+ self.assertRaises(AttributeError, youtube_noapi.response, '[]')
+
+ response = mock.Mock(text='<html></html>')
+ self.assertEqual(youtube_noapi.response(response), [])
+
+ html = """
+ <ol id="item-section-063864" class="item-section">
+ <li>
+ <div class="yt-lockup yt-lockup-tile yt-lockup-video vve-check clearfix yt-uix-tile"
+ data-context-item-id="DIVZCPfAOeM"
+ data-visibility-tracking="CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JECx_-GK5uqMpcIB">
+ <div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto">
+ <a aria-hidden="true" href="/watch?v=DIVZCPfAOeM" class=" yt-uix-sessionlink pf-link"
+ data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JFIEdGVzdA">
+ <div class="yt-thumb video-thumb"><img src="//i.ytimg.com/vi/DIVZCPfAOeM/mqdefault.jpg"
+ width="196" height="110"/></div><span class="video-time" aria-hidden="true">11:35</span></a>
+ <span class="thumb-menu dark-overflow-action-menu video-actions">
+ </span>
+ </div>
+ <div class="yt-lockup-content">
+ <h3 class="yt-lockup-title">
+ <a href="/watch?v=DIVZCPfAOeM"
+ class="yt-uix-tile-link yt-ui-ellipsis yt-ui-ellipsis-2 yt-uix-sessionlink spf-link"
+ data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JFIEdGVzdA"
+ title="Top Speed Test Kawasaki Ninja H2 (Thailand) By. MEHAY SUPERBIKE"
+ aria-describedby="description-id-259079" rel="spf-prefetch" dir="ltr">
+ Title
+ </a>
+ <span class="accessible-description" id="description-id-259079"> - Durée : 11:35.</span>
+ </h3>
+ <div class="yt-lockup-byline">de
+ <a href="/user/mheejapan" class=" yt-uix-sessionlink spf-link g-hovercard"
+ data-sessionlink="itct=CBgQ3DAYACITCPGXnYau6sUCFZEIHAod-VQASCj0JA" data-ytid="UCzEesu54Hjs0uRKmpy66qeA"
+ data-name="">MEHAY SUPERBIKE</a></div><div class="yt-lockup-meta">
+ <ul class="yt-lockup-meta-info">
+ <li>il y a 20 heures</li>
+ <li>8 424 vues</li>
+ </ul>
+ </div>
+ <div class="yt-lockup-description yt-ui-ellipsis yt-ui-ellipsis-2" dir="ltr">
+ Description
+ </div>
+ <div class="yt-lockup-badges">
+ <ul class="yt-badge-list ">
+ <li class="yt-badge-item" >
+ <span class="yt-badge">Nouveauté</span>
+ </li>
+ <li class="yt-badge-item" ><span class="yt-badge " >HD</span></li>
+ </ul>
+ </div>
+ <div class="yt-lockup-action-menu yt-uix-menu-container">
+ <div class="yt-uix-menu yt-uix-videoactionmenu hide-until-delayloaded"
+ data-video-id="DIVZCPfAOeM" data-menu-content-id="yt-uix-videoactionmenu-menu">
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ </li>
+ </ol>
+ """
+ response = mock.Mock(text=html)
+ results = youtube_noapi.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 1)
+ self.assertEqual(results[0]['title'], 'Title')
+ self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
+ self.assertEqual(results[0]['content'], 'Description')
+ self.assertEqual(results[0]['thumbnail'], 'https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg')
+ self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
+
+ html = """
+ <ol id="item-section-063864" class="item-section">
+ <li>
+ </li>
+ </ol>
+ """
+ response = mock.Mock(text=html)
+ results = youtube_noapi.response(response)
+ self.assertEqual(type(results), list)
+ self.assertEqual(len(results), 0)
diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py
index 5770458f3..d0a4de4b8 100644
--- a/searx/tests/test_engines.py
+++ b/searx/tests/test_engines.py
@@ -39,4 +39,6 @@ from searx.tests.engines.test_www500px import * # noqa
from searx.tests.engines.test_yacy import * # noqa
from searx.tests.engines.test_yahoo import * # noqa
from searx.tests.engines.test_youtube import * # noqa
+from searx.tests.engines.test_youtube_api import * # noqa
+from searx.tests.engines.test_youtube_noapi import * # noqa
from searx.tests.engines.test_yahoo_news import * # noqa