test_gigablast.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import gigablast
  4. from searx.testing import SearxTestCase
  5. class TestGigablastEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. dicto['language'] = 'all'
  11. params = gigablast.request(query, dicto)
  12. self.assertTrue('url' in params)
  13. self.assertTrue(query in params['url'])
  14. self.assertTrue('gigablast.com' in params['url'])
  15. self.assertTrue('xx' in params['url'])
  16. dicto['language'] = 'en-US'
  17. params = gigablast.request(query, dicto)
  18. self.assertTrue('en' in params['url'])
  19. self.assertFalse('en-US' in params['url'])
  20. def test_response(self):
  21. self.assertRaises(AttributeError, gigablast.response, None)
  22. self.assertRaises(AttributeError, gigablast.response, [])
  23. self.assertRaises(AttributeError, gigablast.response, '')
  24. self.assertRaises(AttributeError, gigablast.response, '[]')
  25. response = mock.Mock(text='{"results": []}')
  26. self.assertEqual(gigablast.response(response), [])
  27. json = """{"results": [
  28. {
  29. "title":"South by Southwest 2016",
  30. "dmozEntry":{
  31. "dmozCatId":1041152,
  32. "directCatId":1,
  33. "dmozCatStr":"Top: Regional: North America: United States",
  34. "dmozTitle":"South by Southwest (SXSW)",
  35. "dmozSum":"Annual music, film, and interactive conference.",
  36. "dmozAnchor":""
  37. },
  38. "dmozEntry":{
  39. "dmozCatId":763945,
  40. "directCatId":1,
  41. "dmozCatStr":"Top: Regional: North America: United States",
  42. "dmozTitle":"South by Southwest (SXSW)",
  43. "dmozSum":"",
  44. "dmozAnchor":"www.sxsw.com"
  45. },
  46. "dmozEntry":{
  47. "dmozCatId":761446,
  48. "directCatId":1,
  49. "dmozCatStr":"Top: Regional: North America: United States",
  50. "dmozTitle":"South by Southwest (SXSW)",
  51. "dmozSum":"Music, film, and interactive conference and festival.",
  52. "dmozAnchor":""
  53. },
  54. "indirectDmozCatId":1041152,
  55. "indirectDmozCatId":763945,
  56. "indirectDmozCatId":761446,
  57. "contentType":"html",
  58. "sum":"This should be the content.",
  59. "url":"www.sxsw.com",
  60. "hopCount":0,
  61. "size":" 102k",
  62. "sizeInBytes":104306,
  63. "bytesUsedToComputeSummary":70000,
  64. "docId":269411794364,
  65. "docScore":586571136.000000,
  66. "summaryGenTimeMS":12,
  67. "summaryTagdbLookupTimeMS":0,
  68. "summaryTitleRecLoadTimeMS":1,
  69. "site":"www.sxsw.com",
  70. "spidered":1452203608,
  71. "firstIndexedDateUTC":1444167123,
  72. "contentHash32":2170650347,
  73. "language":"English",
  74. "langAbbr":"en"
  75. }
  76. ]}
  77. """
  78. response = mock.Mock(text=json)
  79. results = gigablast.response(response)
  80. self.assertEqual(type(results), list)
  81. self.assertEqual(len(results), 1)
  82. self.assertEqual(results[0]['title'], 'South by Southwest 2016')
  83. self.assertEqual(results[0]['url'], 'www.sxsw.com')
  84. self.assertEqual(results[0]['content'], 'This should be the content.')
  85. def test_fetch_supported_languages(self):
  86. html = """<html></html>"""
  87. response = mock.Mock(text=html)
  88. results = gigablast._fetch_supported_languages(response)
  89. self.assertEqual(type(results), list)
  90. self.assertEqual(len(results), 0)
  91. html = """
  92. <html>
  93. <body>
  94. <span id="menu2">
  95. <a href="/search?&rxikd=1&qlang=xx"></a>
  96. <a href="/search?&rxikd=1&qlang=en"></a>
  97. <a href="/search?&rxikd=1&prepend=gblang%3Aen"></a>
  98. <a href="/search?&rxikd=1&qlang=zh_"></a>
  99. <a href="/search?&rxikd=1&prepend=gblang%3Azh_tw"></a>
  100. </span>
  101. </body>
  102. </html>
  103. """
  104. response = mock.Mock(text=html)
  105. languages = gigablast._fetch_supported_languages(response)
  106. self.assertEqual(type(languages), list)
  107. self.assertEqual(len(languages), 2)
  108. self.assertIn('en', languages)
  109. self.assertIn('zh-TW', languages)