summaryrefslogtreecommitdiff
path: root/tests/unit/test_plugin_calculator.py
blob: caca0dfe2821848f48e6a2914104a0f7e20be97b (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
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring

import flask
from parameterized.parameterized import parameterized
from searx import plugins
from searx import preferences
from tests import SearxTestCase
from .test_utils import random_string

from .test_plugins import get_search_mock


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

    def setUp(self):
        from searx import webapp  # pylint: disable=import-outside-toplevel

        self.webapp = webapp
        self.store = plugins.PluginStore()
        plugin = plugins.load_and_initialize_plugin('searx.plugins.calculator', False, (None, {}))
        self.store.register(plugin)
        self.preferences = preferences.Preferences(["simple"], ["general"], {}, self.store)
        self.preferences.parse_dict({"locale": "en"})

    def test_plugin_store_init(self):
        self.assertEqual(1, len(self.store.plugins))

    def test_single_page_number_true(self):
        with self.webapp.app.test_request_context():
            flask.request.preferences = self.preferences
            search = get_search_mock(query=random_string(10), pageno=2)
            result = self.store.call(self.store.plugins, 'post_search', flask.request, search)

        self.assertTrue(result)
        self.assertNotIn('calculate', search.result_container.answers)

    def test_long_query_true(self):
        with self.webapp.app.test_request_context():
            flask.request.preferences = self.preferences
            search = get_search_mock(query=random_string(101), pageno=1)
            result = self.store.call(self.store.plugins, 'post_search', flask.request, search)

        self.assertTrue(result)
        self.assertNotIn('calculate', search.result_container.answers)

    def test_alpha_true(self):
        with self.webapp.app.test_request_context():
            flask.request.preferences = self.preferences
            search = get_search_mock(query=random_string(10), pageno=1)
            result = self.store.call(self.store.plugins, 'post_search', flask.request, search)

        self.assertTrue(result)
        self.assertNotIn('calculate', search.result_container.answers)

    @parameterized.expand(
        [
            ("1+1", "2", "en"),
            ("1-1", "0", "en"),
            ("1*1", "1", "en"),
            ("1/1", "1", "en"),
            ("1**1", "1", "en"),
            ("1^1", "1", "en"),
            ("1,000.0+1,000.0", "2,000", "en"),
            ("1.0+1.0", "2", "en"),
            ("1.0-1.0", "0", "en"),
            ("1.0*1.0", "1", "en"),
            ("1.0/1.0", "1", "en"),
            ("1.0**1.0", "1", "en"),
            ("1.0^1.0", "1", "en"),
            ("1.000,0+1.000,0", "2.000", "de"),
            ("1,0+1,0", "2", "de"),
            ("1,0-1,0", "0", "de"),
            ("1,0*1,0", "1", "de"),
            ("1,0/1,0", "1", "de"),
            ("1,0**1,0", "1", "de"),
            ("1,0^1,0", "1", "de"),
        ]
    )
    def test_localized_query(self, operation: str, contains_result: str, lang: str):
        with self.webapp.app.test_request_context():
            self.preferences.parse_dict({"locale": lang})
            flask.request.preferences = self.preferences
            search = get_search_mock(query=operation, lang=lang, pageno=1)
            result = self.store.call(self.store.plugins, 'post_search', flask.request, search)

        self.assertTrue(result)
        self.assertIn('calculate', search.result_container.answers)
        self.assertIn(contains_result, search.result_container.answers['calculate']['answer'])

    @parameterized.expand(
        [
            "1/0",
        ]
    )
    def test_invalid_operations(self, operation):
        with self.webapp.app.test_request_context():
            flask.request.preferences = self.preferences
            search = get_search_mock(query=operation, pageno=1)
            result = self.store.call(self.store.plugins, 'post_search', flask.request, search)

        self.assertTrue(result)
        self.assertNotIn('calculate', search.result_container.answers)