summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_github.py
blob: 460be8c3df89f3da38e9491d42c440a111eafde9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from collections import defaultdict
import mock
from searx.engines import github
from searx.testing import SearxTestCase


class TestGitHubEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        params = github.request(query, defaultdict(dict))
        self.assertTrue('url' in params)
        self.assertTrue(query in params['url'])
        self.assertTrue('github.com' in params['url'])
        self.assertEqual(params['headers']['Accept'], github.accept_header)

    def test_response(self):
        self.assertRaises(AttributeError, github.response, None)
        self.assertRaises(AttributeError, github.response, [])
        self.assertRaises(AttributeError, github.response, '')
        self.assertRaises(AttributeError, github.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(github.response(response), [])

        response = mock.Mock(text='{"items": []}')
        self.assertEqual(github.response(response), [])

        json = """
        {
            "items": [
                {
                    "name": "title",
                    "html_url": "url",
                    "description": ""
                }
            ]
        }
        """
        response = mock.Mock(text=json)
        results = github.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'title')
        self.assertEqual(results[0]['url'], 'url')
        self.assertEqual(results[0]['content'], '')

        json = """
        {
            "items": [
                {
                    "name": "title",
                    "html_url": "url",
                    "description": "desc"
                }
            ]
        }
        """
        response = mock.Mock(text=json)
        results = github.response(response)
        self.assertEqual(results[0]['content'], "desc")