on eb182df help sign in
searx/tests/unit/engines/test_searchcode_code.py
75 lines 2.5 kB history raw
1from collections import defaultdict
2import mock
3from searx.engines import searchcode_code
4from searx.testing import SearxTestCase
7class TestSearchcodeCodeEngine(SearxTestCase):
9 def test_request(self):
10 query = 'test_query'
11 dicto = defaultdict(dict)
12 dicto['pageno'] = 0
13 params = searchcode_code.request(query, dicto)
14 self.assertIn('url', params)
15 self.assertIn(query, params['url'])
16 self.assertIn('searchcode.com', params['url'])
18 def test_response(self):
19 self.assertRaises(AttributeError, searchcode_code.response, None)
20 self.assertRaises(AttributeError, searchcode_code.response, [])
21 self.assertRaises(AttributeError, searchcode_code.response, '')
22 self.assertRaises(AttributeError, searchcode_code.response, '[]')
24 response = mock.Mock(text='{}')
25 self.assertEqual(searchcode_code.response(response), [])
27 response = mock.Mock(text='{"data": []}')
28 self.assertEqual(searchcode_code.response(response), [])
30 json = """
31 {
32 "matchterm": "test",
33 "previouspage": null,
34 "searchterm": "test",
35 "query": "test",
36 "total": 1000,
37 "page": 0,
38 "nextpage": 1,
39 "results": [
40 {
41 "repo": "https://repo",
42 "linescount": 1044,
43 "location": "/tests",
44 "name": "Name",
45 "url": "https://url",
46 "md5hash": "ecac6e479edd2b9406c9e08603cec655",
47 "lines": {
48 "1": "// Test 011",
49 "2": "// Source: "
50 },
51 "id": 51223527,
52 "filename": "File.CPP"
53 }
54 ]
55 }
56 """
57 response = mock.Mock(text=json)
58 results = searchcode_code.response(response)
59 self.assertEqual(type(results), list)
60 self.assertEqual(len(results), 1)
61 self.assertEqual(results[0]['title'], 'Name - File.CPP')
62 self.assertEqual(results[0]['url'], 'https://url')
63 self.assertEqual(results[0]['repository'], 'https://repo')
64 self.assertEqual(results[0]['code_language'], 'cpp')
66 json = r"""
67 {"toto":[
68 {"id":200,"name":"Artist Name",
69 "link":"http:\/\/www.searchcode_code.com\/artist\/1217","type":"artist"}
70 ]}
71 """
72 response = mock.Mock(text=json)
73 results = searchcode_code.response(response)
74 self.assertEqual(type(results), list)
75 self.assertEqual(len(results), 0)