summaryrefslogtreecommitdiff
path: root/tests/unit/test_external_bangs.py
blob: 15352159962f955563cbc9c6ef3062323d42cbb3 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring

from searx.external_bang import (
    get_node,
    resolve_bang_definition,
    get_bang_url,
    get_bang_definition_and_autocomplete,
    LEAF_KEY,
)
from searx.search import SearchQuery, EngineRef
from tests import SearxTestCase


TEST_DB = {
    'trie': {
        'exam': {
            'ple': '//example.com/' + chr(2) + chr(1) + '0',
            LEAF_KEY: '//wikipedia.org/wiki/' + chr(2) + chr(1) + '0',
        },
        'sea': {
            LEAF_KEY: 'sea' + chr(2) + chr(1) + '0',
            'rch': {
                LEAF_KEY: 'search' + chr(2) + chr(1) + '0',
                'ing': 'searching' + chr(2) + chr(1) + '0',
            },
            's': {
                'on': 'season' + chr(2) + chr(1) + '0',
                'capes': 'seascape' + chr(2) + chr(1) + '0',
            },
        },
        'error': ['error in external_bangs.json'],
    }
}


class TestGetNode(SearxTestCase):  # pylint: disable=missing-class-docstring

    DB = {  # pylint:disable=invalid-name
        'trie': {
            'exam': {
                'ple': 'test',
                LEAF_KEY: 'not used',
            }
        }
    }

    def test_found(self):
        node, before, after = get_node(TestGetNode.DB, 'example')

        self.assertEqual(node, 'test')
        self.assertEqual(before, 'example')
        self.assertEqual(after, '')

    def test_get_partial(self):
        node, before, after = get_node(TestGetNode.DB, 'examp')
        self.assertEqual(node, TestGetNode.DB['trie']['exam'])
        self.assertEqual(before, 'exam')
        self.assertEqual(after, 'p')

    def test_not_found(self):
        node, before, after = get_node(TestGetNode.DB, 'examples')
        self.assertEqual(node, 'test')
        self.assertEqual(before, 'example')
        self.assertEqual(after, 's')


class TestResolveBangDefinition(SearxTestCase):  # pylint:disable=missing-class-docstring
    def test_https(self):
        url, rank = resolve_bang_definition('//example.com/' + chr(2) + chr(1) + '42', 'query')
        self.assertEqual(url, 'https://example.com/query')
        self.assertEqual(rank, 42)

    def test_http(self):
        url, rank = resolve_bang_definition('http://example.com/' + chr(2) + chr(1) + '0', 'text')
        self.assertEqual(url, 'http://example.com/text')
        self.assertEqual(rank, 0)


class TestGetBangDefinitionAndAutocomplete(SearxTestCase):  # pylint:disable=missing-class-docstring
    def test_found(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('exam', external_bangs_db=TEST_DB)
        self.assertEqual(bang_definition, TEST_DB['trie']['exam'][LEAF_KEY])
        self.assertEqual(new_autocomplete, ['example'])

    def test_found_optimized(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('example', external_bangs_db=TEST_DB)
        self.assertEqual(bang_definition, TEST_DB['trie']['exam']['ple'])
        self.assertEqual(new_autocomplete, [])

    def test_partial(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('examp', external_bangs_db=TEST_DB)
        self.assertIsNone(bang_definition)
        self.assertEqual(new_autocomplete, ['example'])

    def test_partial2(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('sea', external_bangs_db=TEST_DB)
        self.assertEqual(bang_definition, TEST_DB['trie']['sea'][LEAF_KEY])
        self.assertEqual(new_autocomplete, ['search', 'searching', 'seascapes', 'season'])

    def test_error(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('error', external_bangs_db=TEST_DB)
        self.assertIsNone(bang_definition)
        self.assertEqual(new_autocomplete, [])

    def test_actual_data(self):
        bang_definition, new_autocomplete = get_bang_definition_and_autocomplete('duckduckgo')
        self.assertTrue(bang_definition.startswith('//duckduckgo.com/?q='))
        self.assertEqual(new_autocomplete, [])


class TestExternalBangJson(SearxTestCase):  # pylint:disable=missing-class-docstring
    def test_no_external_bang_query(self):
        result = get_bang_url(SearchQuery('test', engineref_list=[EngineRef('wikipedia', 'general')]))
        self.assertIsNone(result)

    def test_get_bang_url(self):
        url = get_bang_url(SearchQuery('test', engineref_list=[], external_bang='example'), external_bangs_db=TEST_DB)
        self.assertEqual(url, 'https://example.com/test')

    def test_actual_data(self):
        google_url = get_bang_url(SearchQuery('test', engineref_list=[], external_bang='g'))
        self.assertEqual(google_url, 'https://www.google.com/search?q=test')