123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from collections import defaultdict
  2. import mock
  3. from searx.engines import searchcode_code
  4. from searx.testing import SearxTestCase
  5. class TestSearchcodeCodeEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. dicto['pageno'] = 0
  10. params = searchcode_code.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_code.response, None)
  16. self.assertRaises(AttributeError, searchcode_code.response, [])
  17. self.assertRaises(AttributeError, searchcode_code.response, '')
  18. self.assertRaises(AttributeError, searchcode_code.response, '[]')
  19. response = mock.Mock(text='{}')
  20. self.assertEqual(searchcode_code.response(response), [])
  21. response = mock.Mock(text='{"data": []}')
  22. self.assertEqual(searchcode_code.response(response), [])
  23. json = """
  24. {
  25. "matchterm": "test",
  26. "previouspage": null,
  27. "searchterm": "test",
  28. "query": "test",
  29. "total": 1000,
  30. "page": 0,
  31. "nextpage": 1,
  32. "results": [
  33. {
  34. "repo": "https://repo",
  35. "linescount": 1044,
  36. "location": "/tests",
  37. "name": "Name",
  38. "url": "https://url",
  39. "md5hash": "ecac6e479edd2b9406c9e08603cec655",
  40. "lines": {
  41. "1": "// Test 011",
  42. "2": "// Source: "
  43. },
  44. "id": 51223527,
  45. "filename": "File.CPP"
  46. }
  47. ]
  48. }
  49. """
  50. response = mock.Mock(text=json)
  51. results = searchcode_code.response(response)
  52. self.assertEqual(type(results), list)
  53. self.assertEqual(len(results), 1)
  54. self.assertEqual(results[0]['title'], 'Name - File.CPP')
  55. self.assertEqual(results[0]['url'], 'https://url')
  56. self.assertEqual(results[0]['repository'], 'https://repo')
  57. self.assertEqual(results[0]['code_language'], 'cpp')
  58. json = r"""
  59. {"toto":[
  60. {"id":200,"name":"Artist Name",
  61. "link":"http:\/\/www.searchcode_code.com\/artist\/1217","type":"artist"}
  62. ]}
  63. """
  64. response = mock.Mock(text=json)
  65. results = searchcode_code.response(response)
  66. self.assertEqual(type(results), list)
  67. self.assertEqual(len(results), 0)