@@ -0,0 +1,232 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
3+import mock
4+from searx.engines import wolframalpha_noapi
5+from searx.testing import SearxTestCase
6+
7+
8+class TestWolframAlphaNoAPIEngine(SearxTestCase):
9+
10+ def test_request(self):
11+ query = 'test_query'
12+ dicto = defaultdict(dict)
13+ dicto['pageno'] = 1
14+ params = wolframalpha_noapi.request(query, dicto)
15+ self.assertIn('url', params)
16+ self.assertIn(query, params['url'])
17+ self.assertIn('wolframalpha.com', params['url'])
18+
19+ def test_response(self):
20+ self.assertRaises(AttributeError, wolframalpha_noapi.response, None)
21+ self.assertRaises(AttributeError, wolframalpha_noapi.response, [])
22+ self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
23+ self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')
24+
25+ response = mock.Mock(text='<html></html>')
26+ self.assertEqual(wolframalpha_noapi.response(response), [])
27+
28+ html = """
29+ <!DOCTYPE html>
30+ <title> sqrt(-1) - Wolfram|Alpha</title>
31+ <meta charset="utf-8" />
32+ <body>
33+ <script type="text/javascript">
34+ try {
35+ document.domain = "wolframalpha.com";
36+ context = parent ? parent : document;
37+ } catch(e){}
38+ try {
39+ if (typeof(context.$) == "undefined") {
40+ context = window;
41+ } else {
42+ $=context.$;
43+ }
44+ }
45+ catch(e){ context = window;}
46+
47+ try {
48+
49+ if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
50+ context.jsonArray.popups.pod_0100 = [];
51+ }
52+
53+ context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": "", "popLinks": {} });
54+
55+ } catch(e) { }
56+
57+ try {
58+
59+ $("#results #pod_0100:not(iframe #pod_0100)")
60+ .add("#showsteps #pod_0100:not(iframe #pod_0100)")
61+ .add(".results-pod #pod_0100:not(iframe #pod_0100)")
62+ .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
63+ .data("podIdentifier", '\x22Input\x22')
64+ .data("podShortIdentifier", '\x22Input\x22')
65+ .data("buttonStates", '\x22\x22')
66+ .data("scanner", '\x22\x22');
67+ $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
68+ .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
69+ .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
70+ .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
71+ .data("podIdentifier", '\x22Input\x22')
72+ .data("podShortIdentifier", '\x22Input\x22')
73+ .data("buttonStates", '\x22\x22')
74+ .data("scanner", '\x22\x22');
75+
76+ $("#results #subpod_0100_1")
77+ .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
78+ .add(".results-pod #subpod_0100_1")
79+ .data("tempFileID", "MSP44511e0dda34g97a0c89000059490h319161eea3")
80+ .data("cellDataTempFile", "MSP44521e0dda34g97a0c89000011378c50d38ede6h")
81+ .data("tempFileServer", "")
82+ .data("dataSources", "")
83+ .data("sources", "")
84+ .data("sharetype", "1")
85+ .data("shareable", "false");
86+
87+ } catch(e){}
88+
89+ //false
90+
91+ try {
92+
93+ if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
94+ context.jsonArray.popups.pod_0200 = [];
95+ }
96+
97+ context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": "", "popLinks": {} });
98+
99+ } catch(e) { }
100+
101+ try {
102+
103+ $("#results #pod_0200:not(iframe #pod_0200)")
104+ .add("#showsteps #pod_0200:not(iframe #pod_0200)")
105+ .add(".results-pod #pod_0200:not(iframe #pod_0200)")
106+ .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
107+ .data("podIdentifier", '\x22Result\x22')
108+ .data("podShortIdentifier", '\x22Result\x22')
109+ .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None,\x20None,\x20None\x7D')
110+ .data("scanner", '\x22\x22');
111+ $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
112+ .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
113+ .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
114+ .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
115+ .data("podIdentifier", '\x22Result\x22')
116+ .data("podShortIdentifier", '\x22Result\x22')
117+ .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None\x7D')
118+ .data("scanner", '\x22\x22');
119+
120+ $("#results #subpod_0200_1")
121+ .add("#showsteps #subpod_0200_1:not(iframe #subpod_0200_1)")
122+ .add(".results-pod #subpod_0200_1")
123+ .data("tempFileID", "MSP44551e0dda34g97a0c8900003gdgd37faa7272e0")
124+ .data("cellDataTempFile", "MSP44561e0dda34g97a0c89000018ea1iae00104g13")
125+ .data("tempFileServer", "")
126+ .data("dataSources", "")
127+ .data("sources", "")
128+ .data("sharetype", "1")
129+ .data("shareable", "false");
130+ } catch(e){}
131+ </script>
132+ </body>
133+ </html>
134+ """
135+ response = mock.Mock(text=html)
136+ results = wolframalpha_noapi.response(response)
137+ self.assertEqual(type(results), list)
138+ self.assertEqual(len(results), 1)
139+ self.assertIn("i", results[0]['answer'])
140+
141+ html = """
142+ <!DOCTYPE html>
143+ <title> integral 1/x - Wolfram|Alpha</title>
144+ <meta charset="utf-8" />
145+ <body>
146+ <script type="text/javascript">
147+ //true
148+ try {
149+ document.domain = "wolframalpha.com";
150+ context = parent ? parent : document;
151+ } catch(e){}
152+ try {
153+ if (typeof(context.$) == "undefined") {
154+ context = window;
155+ } else {
156+ $=context.$;
157+ }
158+ }
159+ catch(e){ context = window;}
160+
161+ try {
162+
163+ if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
164+ context.jsonArray.popups.pod_0100 = [];
165+ }
166+
167+ context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
168+
169+ } catch(e) { }
170+
171+ try {
172+
173+ $("#results #pod_0100:not(iframe #pod_0100)")
174+ .add("#showsteps #pod_0100:not(iframe #pod_0100)")
175+ .add(".results-pod #pod_0100:not(iframe #pod_0100)")
176+ .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
177+ .data("podIdentifier", '\x22IndefiniteIntegral\x22')
178+ .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
179+ .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
180+ .data("scanner", '\x22\x22');
181+ $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
182+ .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
183+ .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
184+ .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
185+ .data("podIdentifier", '\x22IndefiniteIntegral\x22')
186+ .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
187+ .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
188+ .data("scanner", '\x22\x22');
189+
190+ $("#results #subpod_0100_1")
191+ .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
192+ .add(".results-pod #subpod_0100_1")
193+ .data("tempFileID", "MSP2071if2202e8bg0757100004dg60f2a4ca8cf73")
194+ .data("cellDataTempFile", "MSP2081if2202e8bg0757100001h18329f72fe90fg")
195+ .data("tempFileServer", "")
196+ .data("dataSources", "")
197+ .data("sources", "")
198+ .data("sharetype", "1")
199+ .data("shareable", "false");
200+
201+ } catch(e){}
202+
203+ //false
204+ try {
205+
206+ $("#results #pod_0200:not(iframe #pod_0200)")
207+ .add("#showsteps #pod_0200:not(iframe #pod_0200)")
208+ .add(".results-pod #pod_0200:not(iframe #pod_0200)")
209+ .data("tempFileID", '')
210+ .data("podIdentifier", '\x22Plot\x22')
211+ .data("podShortIdentifier", '')
212+ .data("buttonStates", '')
213+ .data("scanner", '\x22\x22');
214+ $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
215+ .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
216+ .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
217+ .data("tempFileID", '')
218+ .data("podIdentifier", '\x22Plot\x22')
219+ .data("podShortIdentifier", '')
220+ .data("buttonStates", '')
221+ .data("scanner", '\x22\x22');
222+
223+ } catch(e){}
224+ </script>
225+ </body>
226+ </html>
227+ """
228+ response = mock.Mock(text=html)
229+ results = wolframalpha_noapi.response(response)
230+ self.assertEqual(type(results), list)
231+ self.assertEqual(len(results), 1)
232+ self.assertIn("log(x)+c", results[0]['answer'])