summaryrefslogtreecommitdiff
path: root/tests/unit/engines/test_searchcode_code.py
blob: 955aea111e3df2bf2cd0f2222cab9c18d1d2206c (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from collections import defaultdict
import mock
from searx.engines import searchcode_code
from searx.testing import SearxTestCase


class TestSearchcodeCodeEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 0
        params = searchcode_code.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('searchcode.com', params['url'])

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

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

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

        json = """
        {
        "matchterm": "test",
        "previouspage": null,
        "searchterm": "test",
        "query": "test",
        "total": 1000,
        "page": 0,
        "nextpage": 1,
        "results": [
            {
            "repo": "https://repo",
            "linescount": 1044,
            "location": "/tests",
            "name": "Name",
            "url": "https://url",
            "md5hash": "ecac6e479edd2b9406c9e08603cec655",
            "lines": {
                "1": "// Test 011",
                "2": "// Source: "
            },
            "id": 51223527,
            "filename": "File.CPP"
            }
        ]
        }
        """
        response = mock.Mock(text=json)
        results = searchcode_code.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'Name - File.CPP')
        self.assertEqual(results[0]['url'], 'https://url')
        self.assertEqual(results[0]['repository'], 'https://repo')
        self.assertEqual(results[0]['code_language'], 'cpp')

        json = r"""
        {"toto":[
            {"id":200,"name":"Artist Name",
            "link":"http:\/\/www.searchcode_code.com\/artist\/1217","type":"artist"}
        ]}
        """
        response = mock.Mock(text=json)
        results = searchcode_code.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)