on 00a7041 help sign in
searx/tests/unit/engines/test_searchcode_doc.py
70 lines 2.3 kB history raw
1from collections import defaultdict
2import mock
3from searx.engines import searchcode_doc
4from searx.testing import SearxTestCase
7class TestSearchcodeDocEngine(SearxTestCase):
9 def test_request(self):
10 query = 'test_query'
11 dicto = defaultdict(dict)
12 dicto['pageno'] = 0
13 params = searchcode_doc.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_doc.response, None)
20 self.assertRaises(AttributeError, searchcode_doc.response, [])
21 self.assertRaises(AttributeError, searchcode_doc.response, '')
22 self.assertRaises(AttributeError, searchcode_doc.response, '[]')
24 response = mock.Mock(text='{}')
25 self.assertEqual(searchcode_doc.response(response), [])
27 response = mock.Mock(text='{"data": []}')
28 self.assertEqual(searchcode_doc.response(response), [])
30 json = """
31 {
32 "matchterm": "test",
33 "previouspage": null,
34 "searchterm": "test",
35 "query": "test",
36 "total": 60,
37 "page": 0,
38 "nextpage": 1,
39 "results": [
40 {
41 "synopsis": "Synopsis",
42 "displayname": null,
43 "name": "test",
44 "url": "http://url",
45 "type": "Type",
46 "icon": null,
47 "namespace": "Namespace",
48 "description": "Description"
49 }
50 ]
51 }
52 """
53 response = mock.Mock(text=json)
54 results = searchcode_doc.response(response)
55 self.assertEqual(type(results), list)
56 self.assertEqual(len(results), 1)
57 self.assertEqual(results[0]['title'], '[Type] Namespace test')
58 self.assertEqual(results[0]['url'], 'http://url')
59 self.assertIn('Description', results[0]['content'])
61 json = r"""
62 {"toto":[
63 {"id":200,"name":"Artist Name",
64 "link":"http:\/\/www.searchcode_doc.com\/artist\/1217","type":"artist"}
65 ]}
66 """
67 response = mock.Mock(text=json)
68 results = searchcode_doc.response(response)
69 self.assertEqual(type(results), list)
70 self.assertEqual(len(results), 0)