logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_gigablast.py (4184B)


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