test_searchcode_doc.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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('Description', results[0]['content'])
  53. json = r"""
  54. {"toto":[
  55. {"id":200,"name":"Artist Name",
  56. "link":"http:\/\/www.searchcode_doc.com\/artist\/1217","type":"artist"}
  57. ]}
  58. """
  59. response = mock.Mock(text=json)
  60. results = searchcode_doc.response(response)
  61. self.assertEqual(type(results), list)
  62. self.assertEqual(len(results), 0)