|
@@ -0,0 +1,61 @@
|
|
1
|
+from collections import defaultdict
|
|
2
|
+import mock
|
|
3
|
+from searx.engines import github
|
|
4
|
+from searx.testing import SearxTestCase
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+class TestGitHubEngine(SearxTestCase):
|
|
8
|
+
|
|
9
|
+ def test_request(self):
|
|
10
|
+ query = 'test_query'
|
|
11
|
+ params = github.request(query, defaultdict(dict))
|
|
12
|
+ self.assertTrue('url' in params)
|
|
13
|
+ self.assertTrue(query in params['url'])
|
|
14
|
+ self.assertTrue('github.com' in params['url'])
|
|
15
|
+ self.assertEqual(params['headers']['Accept'], github.accept_header)
|
|
16
|
+
|
|
17
|
+ def test_response(self):
|
|
18
|
+ self.assertRaises(AttributeError, github.response, None)
|
|
19
|
+ self.assertRaises(AttributeError, github.response, [])
|
|
20
|
+ self.assertRaises(AttributeError, github.response, '')
|
|
21
|
+ self.assertRaises(AttributeError, github.response, '[]')
|
|
22
|
+
|
|
23
|
+ response = mock.Mock(text='{}')
|
|
24
|
+ self.assertEqual(github.response(response), [])
|
|
25
|
+
|
|
26
|
+ response = mock.Mock(text='{"items": []}')
|
|
27
|
+ self.assertEqual(github.response(response), [])
|
|
28
|
+
|
|
29
|
+ json = """
|
|
30
|
+ {
|
|
31
|
+ "items": [
|
|
32
|
+ {
|
|
33
|
+ "name": "title",
|
|
34
|
+ "html_url": "url",
|
|
35
|
+ "description": ""
|
|
36
|
+ }
|
|
37
|
+ ]
|
|
38
|
+ }
|
|
39
|
+ """
|
|
40
|
+ response = mock.Mock(text=json)
|
|
41
|
+ results = github.response(response)
|
|
42
|
+ self.assertEqual(type(results), list)
|
|
43
|
+ self.assertEqual(len(results), 1)
|
|
44
|
+ self.assertEqual(results[0]['title'], 'title')
|
|
45
|
+ self.assertEqual(results[0]['url'], 'url')
|
|
46
|
+ self.assertEqual(results[0]['content'], '')
|
|
47
|
+
|
|
48
|
+ json = """
|
|
49
|
+ {
|
|
50
|
+ "items": [
|
|
51
|
+ {
|
|
52
|
+ "name": "title",
|
|
53
|
+ "html_url": "url",
|
|
54
|
+ "description": "desc"
|
|
55
|
+ }
|
|
56
|
+ ]
|
|
57
|
+ }
|
|
58
|
+ """
|
|
59
|
+ response = mock.Mock(text=json)
|
|
60
|
+ results = github.response(response)
|
|
61
|
+ self.assertEqual(results[0]['content'], "desc")
|