summaryrefslogtreecommitdiff
path: root/searx/tests/engines/test_wolframalpha_noapi.py
blob: d02dccd95a88af21d05dde9c65c639764888a45f (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# -*- coding: utf-8 -*-
from collections import defaultdict
import mock
from searx.engines import wolframalpha_noapi
from searx.testing import SearxTestCase


class TestWolframAlphaNoAPIEngine(SearxTestCase):

    def test_request(self):
        query = 'test_query'
        dicto = defaultdict(dict)
        dicto['pageno'] = 1
        params = wolframalpha_noapi.request(query, dicto)
        self.assertIn('url', params)
        self.assertIn(query, params['url'])
        self.assertIn('wolframalpha.com', params['url'])

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

        response = mock.Mock(text='<html></html>')
        self.assertEqual(wolframalpha_noapi.response(response), [])

        html = """
        <!DOCTYPE html>
            <title> sqrt(-1) - Wolfram|Alpha</title>
            <meta charset="utf-8" />
            <body>
                <script type="text/javascript">
                  try {
                    document.domain = "wolframalpha.com";
                    context = parent ? parent : document;
                } catch(e){}
                try {
                    if (typeof(context.$) == "undefined") {
                        context = window;
                    } else {
                        $=context.$;
                    }
                }
                catch(e){ context = window;}

            try {

              if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
                context.jsonArray.popups.pod_0100 = [];
              }

    context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": "", "popLinks": {} });

            } catch(e) { }

            try {

            $("#results #pod_0100:not(iframe #pod_0100)")
            .add("#showsteps #pod_0100:not(iframe #pod_0100)")
            .add(".results-pod #pod_0100:not(iframe #pod_0100)")
                .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
                .data("podIdentifier", '\x22Input\x22')
                .data("podShortIdentifier", '\x22Input\x22')
                .data("buttonStates", '\x22\x22')
                .data("scanner", '\x22\x22');
            $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
            .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
            .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
                    .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
                    .data("podIdentifier", '\x22Input\x22')
                    .data("podShortIdentifier", '\x22Input\x22')
                    .data("buttonStates", '\x22\x22')
                    .data("scanner", '\x22\x22');

              $("#results #subpod_0100_1")
              .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
              .add(".results-pod #subpod_0100_1")
                    .data("tempFileID", "MSP44511e0dda34g97a0c89000059490h319161eea3")
                    .data("cellDataTempFile", "MSP44521e0dda34g97a0c89000011378c50d38ede6h")
                    .data("tempFileServer", "")
                    .data("dataSources", "")
                    .data("sources", "")
                    .data("sharetype", "1")
                    .data("shareable", "false");

            } catch(e){}

            //false

            try {

              if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
                context.jsonArray.popups.pod_0200 = [];
              }

              context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": "", "popLinks": {} });

            } catch(e) { }

            try {

            $("#results #pod_0200:not(iframe #pod_0200)")
            .add("#showsteps #pod_0200:not(iframe #pod_0200)")
            .add(".results-pod #pod_0200:not(iframe #pod_0200)")
                .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
                .data("podIdentifier", '\x22Result\x22')
                .data("podShortIdentifier", '\x22Result\x22')
                .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None,\x20None,\x20None\x7D')
                .data("scanner", '\x22\x22');
            $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
            .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
            .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
                    .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
                    .data("podIdentifier", '\x22Result\x22')
                    .data("podShortIdentifier", '\x22Result\x22')
                    .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None\x7D')
                    .data("scanner", '\x22\x22');

              $("#results #subpod_0200_1")
              .add("#showsteps #subpod_0200_1:not(iframe #subpod_0200_1)")
              .add(".results-pod #subpod_0200_1")
                    .data("tempFileID", "MSP44551e0dda34g97a0c8900003gdgd37faa7272e0")
                    .data("cellDataTempFile", "MSP44561e0dda34g97a0c89000018ea1iae00104g13")
                    .data("tempFileServer", "")
                    .data("dataSources", "")
                    .data("sources", "")
                    .data("sharetype", "1")
                    .data("shareable", "false");
                    } catch(e){}
                </script>
            </body>
        </html>
        """
        response = mock.Mock(text=html)
        results = wolframalpha_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertIn("i", results[0]['answer'])

        html = """
        <!DOCTYPE html>
            <title> integral 1/x - Wolfram|Alpha</title>
            <meta charset="utf-8" />
            <body>
                <script type="text/javascript">
                //true
                  try {
                    document.domain = "wolframalpha.com";
                    context = parent ? parent : document;
                    } catch(e){}
                    try {
                        if (typeof(context.$) == "undefined") {
                            context = window;
                        } else {
                            $=context.$;
                        }
                    }
                    catch(e){ context = window;}

                try {

                  if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
                    context.jsonArray.popups.pod_0100 = [];
                  }

                context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});

                } catch(e) { }

                try {

                $("#results #pod_0100:not(iframe #pod_0100)")
                .add("#showsteps #pod_0100:not(iframe #pod_0100)")
                .add(".results-pod #pod_0100:not(iframe #pod_0100)")
                    .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
                    .data("podIdentifier", '\x22IndefiniteIntegral\x22')
                    .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
                    .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
                    .data("scanner", '\x22\x22');
                $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
                .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
                .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
                        .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
                        .data("podIdentifier", '\x22IndefiniteIntegral\x22')
                        .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
                        .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
                        .data("scanner", '\x22\x22');

                  $("#results #subpod_0100_1")
                  .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
                  .add(".results-pod #subpod_0100_1")
                        .data("tempFileID", "MSP2071if2202e8bg0757100004dg60f2a4ca8cf73")
                        .data("cellDataTempFile", "MSP2081if2202e8bg0757100001h18329f72fe90fg")
                        .data("tempFileServer", "")
                        .data("dataSources", "")
                        .data("sources", "")
                        .data("sharetype", "1")
                        .data("shareable", "false");

                } catch(e){}

                //false
                try {

                $("#results #pod_0200:not(iframe #pod_0200)")
                .add("#showsteps #pod_0200:not(iframe #pod_0200)")
                .add(".results-pod #pod_0200:not(iframe #pod_0200)")
                    .data("tempFileID", '')
                    .data("podIdentifier", '\x22Plot\x22')
                    .data("podShortIdentifier", '')
                    .data("buttonStates", '')
                    .data("scanner", '\x22\x22');
                $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
                .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
                .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
                        .data("tempFileID", '')
                        .data("podIdentifier", '\x22Plot\x22')
                        .data("podShortIdentifier", '')
                        .data("buttonStates", '')
                        .data("scanner", '\x22\x22');

                } catch(e){}
                </script>
            </body>
        </html>
        """
        response = mock.Mock(text=html)
        results = wolframalpha_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertIn("log(x)+c", results[0]['answer'])