test_searchcode_doc.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import searchcode_doc
  4. from searx.testing import SearxTestCase
  5. class TestSearchcodeDocEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = searchcode_doc.request(query, dicto)
  11. self.assertIn('url', params)
  12. self.assertIn(query, params['url'])
  13. self.assertIn('searchcode.com', params['url'])
  14. def test_response(self):
  15. self.assertRaises(AttributeError, searchcode_doc.response, None)
  16. self.assertRaises(AttributeError, searchcode_doc.response, [])
  17. self.assertRaises(AttributeError, searchcode_doc.response, '')
  18. self.assertRaises(AttributeError, searchcode_doc.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(searchcode_doc.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(searchcode_doc.response(response), [])
  23. json = """
  24. {
  25. "matchterm": "test",
  26. "previouspage": null,
  27. "searchterm": "test",
  28. "query": "test",
  29. "total": 60,
  30. "page": 0,
  31. "nextpage": 1,
  32. "results": [
  33. {
  34. "synopsis": "Synopsis",
  35. "displayname": null,
  36. "name": "test",
  37. "url": "http://url",
  38. "type": "Type",
  39. "icon": null,
  40. "namespace": "Namespace",
  41. "description": "Description"
  42. }
  43. ]
  44. }
  45. """
  46. response = mock.Mock(text=json)
  47. results = searchcode_doc.response(response)
  48. self.assertEqual(type(results), list)
  49. self.assertEqual(len(results), 1)
  50. self.assertEqual(results[0]['title'], '[Type] Namespace test')
  51. self.assertEqual(results[0]['url'], 'http://url')
  52. self.assertIn('Synopsis', results[0]['content'])
  53. self.assertIn('Type', results[0]['content'])
  54. self.assertIn('test', results[0]['content'])
  55. self.assertIn('Description', results[0]['content'])
  56. json = r"""
  57. {"toto":[
  58. {"id":200,"name":"Artist Name",
  59. "link":"http:\/\/www.searchcode_doc.com\/artist\/1217","type":"artist"}
  60. ]}
  61. """
  62. response = mock.Mock(text=json)
  63. results = searchcode_doc.response(response)
  64. self.assertEqual(type(results), list)
  65. self.assertEqual(len(results), 0)