test_youtube_api.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import youtube_api
  4. from searx.testing import SearxTestCase
  5. class TestYoutubeAPIEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'fr_FR'
  11. params = youtube_api.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertIn('googleapis.com', params['url'])
  15. self.assertIn('youtube', params['url'])
  16. self.assertIn('fr', params['url'])
  17. dicto['language'] = 'all'
  18. params = youtube_api.request(query, dicto)
  19. self.assertFalse('fr' in params['url'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, youtube_api.response, None)
  22. self.assertRaises(AttributeError, youtube_api.response, [])
  23. self.assertRaises(AttributeError, youtube_api.response, '')
  24. self.assertRaises(AttributeError, youtube_api.response, '[]')
  25. response = mock.Mock(text='{}')
  26. self.assertEqual(youtube_api.response(response), [])
  27. response = mock.Mock(text='{"data": []}')
  28. self.assertEqual(youtube_api.response(response), [])
  29. json = """
  30. {
  31. "kind": "youtube#searchListResponse",
  32. "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/YJQDcTBCDcaBvl-sRZJoXdvy1ME",
  33. "nextPageToken": "CAUQAA",
  34. "pageInfo": {
  35. "totalResults": 1000000,
  36. "resultsPerPage": 20
  37. },
  38. "items": [
  39. {
  40. "kind": "youtube#searchResult",
  41. "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/IbLO64BMhbHIgWLwLw7MDYe7Hs4",
  42. "id": {
  43. "kind": "youtube#video",
  44. "videoId": "DIVZCPfAOeM"
  45. },
  46. "snippet": {
  47. "publishedAt": "2015-05-29T22:41:04.000Z",
  48. "channelId": "UCNodmx1ERIjKqvcJLtdzH5Q",
  49. "title": "Title",
  50. "description": "Description",
  51. "thumbnails": {
  52. "default": {
  53. "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/default.jpg"
  54. },
  55. "medium": {
  56. "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/mqdefault.jpg"
  57. },
  58. "high": {
  59. "url": "https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg"
  60. }
  61. },
  62. "channelTitle": "MinecraftUniverse",
  63. "liveBroadcastContent": "none"
  64. }
  65. }
  66. ]
  67. }
  68. """
  69. response = mock.Mock(text=json)
  70. results = youtube_api.response(response)
  71. self.assertEqual(type(results), list)
  72. self.assertEqual(len(results), 1)
  73. self.assertEqual(results[0]['title'], 'Title')
  74. self.assertEqual(results[0]['url'], 'https://www.youtube.com/watch?v=DIVZCPfAOeM')
  75. self.assertEqual(results[0]['content'], 'Description')
  76. self.assertEqual(results[0]['thumbnail'], 'https://i.ytimg.com/vi/DIVZCPfAOeM/hqdefault.jpg')
  77. self.assertTrue('DIVZCPfAOeM' in results[0]['embedded'])
  78. json = """
  79. {
  80. "kind": "youtube#searchListResponse",
  81. "etag": "xmg9xJZuZD438sF4hb-VcBBREXc/YJQDcTBCDcaBvl-sRZJoXdvy1ME",
  82. "nextPageToken": "CAUQAA",
  83. "pageInfo": {
  84. "totalResults": 1000000,
  85. "resultsPerPage": 20
  86. }
  87. }
  88. """
  89. response = mock.Mock(text=json)
  90. results = youtube_api.response(response)
  91. self.assertEqual(type(results), list)
  92. self.assertEqual(len(results), 0)
  93. json = """
  94. {"toto":{"entry":[]
  95. }
  96. }
  97. """
  98. response = mock.Mock(text=json)
  99. results = youtube_api.response(response)
  100. self.assertEqual(type(results), list)
  101. self.assertEqual(len(results), 0)