test_dailymotion.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import dailymotion
  5. from searx.testing import SearxTestCase
  6. class TestDailymotionEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 0
  11. dicto['language'] = 'fr_FR'
  12. params = dailymotion.request(query, dicto)
  13. self.assertTrue('url' in params)
  14. self.assertTrue(query in params['url'])
  15. self.assertTrue('dailymotion.com' in params['url'])
  16. self.assertTrue('fr' in params['url'])
  17. def test_response(self):
  18. self.assertRaises(AttributeError, dailymotion.response, None)
  19. self.assertRaises(AttributeError, dailymotion.response, [])
  20. self.assertRaises(AttributeError, dailymotion.response, '')
  21. self.assertRaises(AttributeError, dailymotion.response, '[]')
  22. response = mock.Mock(text='{}')
  23. self.assertEqual(dailymotion.response(response), [])
  24. response = mock.Mock(text='{"data": []}')
  25. self.assertEqual(dailymotion.response(response), [])
  26. json = """
  27. {
  28. "page": 1,
  29. "limit": 5,
  30. "explicit": false,
  31. "total": 289487,
  32. "has_more": true,
  33. "list": [
  34. {
  35. "created_time": 1422173451,
  36. "title": "Title",
  37. "description": "Description",
  38. "duration": 81,
  39. "url": "http://www.url",
  40. "thumbnail_360_url": "http://thumbnail",
  41. "id": "x2fit7q"
  42. }
  43. ]
  44. }
  45. """
  46. response = mock.Mock(text=json)
  47. results = dailymotion.response(response)
  48. self.assertEqual(type(results), list)
  49. self.assertEqual(len(results), 1)
  50. self.assertEqual(results[0]['title'], 'Title')
  51. self.assertEqual(results[0]['url'], 'http://www.url')
  52. self.assertEqual(results[0]['content'], 'Description')
  53. self.assertIn('x2fit7q', results[0]['embedded'])
  54. json = r"""
  55. {"toto":[
  56. {"id":200,"name":"Artist Name",
  57. "link":"http:\/\/www.dailymotion.com\/artist\/1217","type":"artist"}
  58. ]}
  59. """
  60. response = mock.Mock(text=json)
  61. results = dailymotion.response(response)
  62. self.assertEqual(type(results), list)
  63. self.assertEqual(len(results), 0)
  64. def test_fetch_supported_languages(self):
  65. json = r"""
  66. {"list":[{"code":"af","name":"Afrikaans","native_name":"Afrikaans",
  67. "localized_name":"Afrikaans","display_name":"Afrikaans"},
  68. {"code":"ar","name":"Arabic","native_name":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629",
  69. "localized_name":"Arabic","display_name":"Arabic"},
  70. {"code":"la","name":"Latin","native_name":null,
  71. "localized_name":"Latin","display_name":"Latin"}
  72. ]}
  73. """
  74. response = mock.Mock(text=json)
  75. languages = dailymotion._fetch_supported_languages(response)
  76. self.assertEqual(type(languages), dict)
  77. self.assertEqual(len(languages), 3)
  78. self.assertIn('af', languages)
  79. self.assertIn('ar', languages)
  80. self.assertIn('la', languages)
  81. self.assertEqual(type(languages['af']), dict)
  82. self.assertEqual(type(languages['ar']), dict)
  83. self.assertEqual(type(languages['la']), dict)
  84. self.assertIn('name', languages['af'])
  85. self.assertIn('name', languages['ar'])
  86. self.assertNotIn('name', languages['la'])
  87. self.assertIn('english_name', languages['af'])
  88. self.assertIn('english_name', languages['ar'])
  89. self.assertIn('english_name', languages['la'])
  90. self.assertEqual(languages['af']['name'], 'Afrikaans')
  91. self.assertEqual(languages['af']['english_name'], 'Afrikaans')
  92. self.assertEqual(languages['ar']['name'], u'العربية')
  93. self.assertEqual(languages['ar']['english_name'], 'Arabic')
  94. self.assertEqual(languages['la']['english_name'], 'Latin')