# -*- 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 dicto['time_range'] = '' 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='') self.assertEqual(youtube_noapi.response(response), []) html = """
  1. Title - Durée : 11:35.

    • il y a 20 heures
    • 8 424 vues
    Description
    • Nouveauté
    • HD
""" 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 = """
  1. - Durée : 11:35.

    • il y a 20 heures
    • 8 424 vues
    • Nouveauté
    • HD
""" response = mock.Mock(text=html) results = youtube_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 1) html = """
""" response = mock.Mock(text=html) results = youtube_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0)