test_gigablast.py 4.3KB

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