logo

searx

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

test_base.py (2837B)


  1. # -*- coding: utf-8 -*-
  2. from collections import defaultdict
  3. import mock
  4. from searx.engines import base
  5. from searx.testing import SearxTestCase
  6. class TestBaseEngine(SearxTestCase):
  7. def test_request(self):
  8. query = 'test_query'
  9. dicto = defaultdict(dict)
  10. dicto['pageno'] = 1
  11. params = base.request(query, dicto)
  12. self.assertIn('url', params)
  13. self.assertIn('base-search.net', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, base.response, None)
  16. self.assertRaises(AttributeError, base.response, [])
  17. self.assertRaises(AttributeError, base.response, '')
  18. self.assertRaises(AttributeError, base.response, '[]')
  19. response = mock.Mock(content=b'<response></response>')
  20. self.assertEqual(base.response(response), [])
  21. xml_mock = b"""<?xml version="1.0"?>
  22. <response>
  23. <lst name="responseHeader">
  24. <int name="status">0</int>
  25. <int name="QTime">1</int>
  26. </lst>
  27. <result name="response" numFound="1" start="0">
  28. <doc>
  29. <date name="dchdate">2000-01-01T01:01:01Z</date>
  30. <str name="dcdocid">1</str>
  31. <str name="dccontinent">cna</str>
  32. <str name="dccountry">us</str>
  33. <str name="dccollection">ftciteseerx</str>
  34. <str name="dcprovider">CiteSeerX</str>
  35. <str name="dctitle">Science and more</str>
  36. <arr name="dccreator">
  37. <str>Someone</str>
  38. </arr>
  39. <arr name="dcperson">
  40. <str>Someone</str>
  41. </arr>
  42. <arr name="dcsubject">
  43. <str>Science and more</str>
  44. </arr>
  45. <str name="dcdescription">Science, and even more.</str>
  46. <arr name="dccontributor">
  47. <str>The neighbour</str>
  48. </arr>
  49. <str name="dcdate">2001</str>
  50. <int name="dcyear">2001</int>
  51. <arr name="dctype">
  52. <str>text</str>
  53. </arr>
  54. <arr name="dctypenorm">
  55. <str>1</str>
  56. </arr>
  57. <arr name="dcformat">
  58. <str>application/pdf</str>
  59. </arr>
  60. <arr name="dccontenttype">
  61. <str>application/pdf</str>
  62. </arr>
  63. <arr name="dcidentifier">
  64. <str>http://example.org/</str>
  65. </arr>
  66. <str name="dclink">http://example.org</str>
  67. <str name="dcsource">http://example.org</str>
  68. <arr name="dclanguage">
  69. <str>en</str>
  70. </arr>
  71. <str name="dcrights">Under the example.org licence</str>
  72. <int name="dcoa">1</int>
  73. <arr name="dclang">
  74. <str>eng</str>
  75. </arr>
  76. </doc>
  77. </result>
  78. </response>"""
  79. response = mock.Mock(content=xml_mock)
  80. results = base.response(response)
  81. self.assertEqual(type(results), list)
  82. self.assertEqual(len(results), 1)
  83. self.assertEqual(results[0]['title'], 'Science and more')
  84. self.assertEqual(results[0]['content'], 'Science, and even more.')