Browse Source

add tests for unicode strings in wolframalpha

a01200356 9 years ago
parent
commit
d997265e55

+ 2
- 2
searx/engines/wolframalpha_noapi.py View File

@@ -73,11 +73,11 @@ def response(resp):
73 73
         results.append({'answer': answer})
74 74
 
75 75
     # user input is in first part of title
76
-    title = dom.xpath(title_xpath)[0].text
76
+    title = dom.xpath(title_xpath)[0].text.encode('utf-8')
77 77
     result_url = request(title[:-16], {})['url']
78 78
 
79 79
     # append result
80 80
     results.append({'url': result_url,
81
-                    'title': title})
81
+                    'title': title.decode('utf-8')})
82 82
 
83 83
     return results

+ 104
- 34
searx/tests/engines/test_wolframalpha_api.py View File

@@ -30,32 +30,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
30 30
         xml = '''<?xml version='1.0' encoding='UTF-8'?>
31 31
         <queryresult success='false' error='false' />
32 32
         '''
33
-
34
-        response = mock.Mock(content=xml)
35
-        self.assertEqual(wolframalpha_api.response(response), [])
36
-
37
-        xml = """<?xml version='1.0' encoding='UTF-8'?>
38
-        <queryresult success='false'
39
-            error='false'
40
-            numpods='0'
41
-            datatypes=''
42
-            timedout=''
43
-            timedoutpods=''
44
-            timing='0.241'
45
-            parsetiming='0.074'
46
-            parsetimedout='false'
47
-            recalculate=''
48
-            id=''
49
-            host='http://www5a.wolframalpha.com'
50
-            server='56'
51
-            related=''
52
-            version='2.6'>
53
-         <tips count='1'>
54
-          <tip text='Check your spelling, and use English' />
55
-         </tips>
56
-        </queryresult>
57
-        """
58
-
33
+        # test failure
59 34
         response = mock.Mock(content=xml)
60 35
         self.assertEqual(wolframalpha_api.response(response), [])
61 36
 
@@ -145,14 +120,12 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
145 120
          </pod>
146 121
         </queryresult>
147 122
         """
123
+        # test private user area char in response
148 124
         response = mock.Mock(content=xml)
149 125
         results = wolframalpha_api.response(response)
150 126
         self.assertEqual(type(results), list)
151
-        # self.assertEqual(len(results), 2)
152 127
         self.assertEqual(len(results), 1)
153
-        self.assertIn("i", results[0]['answer'])
154
-        # self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
155
-        # self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
128
+        self.assertIn('i', results[0]['answer'])
156 129
 
157 130
         xml = """<?xml version='1.0' encoding='UTF-8'?>
158 131
             <queryresult success='true'
@@ -246,11 +219,108 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
246 219
              </assumptions>
247 220
             </queryresult>
248 221
         """
222
+        # test integral
249 223
         response = mock.Mock(content=xml)
250 224
         results = wolframalpha_api.response(response)
251 225
         self.assertEqual(type(results), list)
252
-        # self.assertEqual(len(results), 2)
253 226
         self.assertEqual(len(results), 1)
254
-        self.assertIn("log(x)+c", results[0]['answer'])
255
-        # self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
256
-        # self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])
227
+        self.assertIn('log(x)+c', results[0]['answer'])
228
+
229
+        xml = """<?xml version='1.0' encoding='UTF-8'?>
230
+        <queryresult success='true'
231
+            error='false'
232
+            numpods='4'
233
+            datatypes='Solve'
234
+            timedout=''
235
+            timedoutpods=''
236
+            timing='0.883'
237
+            parsetiming='0.337'
238
+            parsetimedout='false'
239
+            recalculate=''
240
+            id='MSPa347225h1ea85fgfbgb4000064ff000d25g5df3f'
241
+            host='http://www5a.wolframalpha.com'
242
+            server='52'
243
+            related='http://www5a.wolframalpha.com/api/v2/relatedQueries.jsp?...'
244
+            version='2.6'>
245
+         <pod title='Input interpretation'
246
+             scanner='Identity'
247
+             id='Input'
248
+             position='100'
249
+             error='false'
250
+             numsubpods='1'>
251
+          <subpod title=''>
252
+           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP349225h1ea85fgfbgb400005dhd93b9eegg8f32?...'
253
+               alt='solve x^2+x = 0'
254
+               title='solve x^2+x = 0'
255
+               width='157'
256
+               height='35' />
257
+           <plaintext>solve x^2+x = 0</plaintext>
258
+          </subpod>
259
+         </pod>
260
+         <pod title='Results'
261
+             scanner='Solve'
262
+             id='Result'
263
+             position='200'
264
+             error='false'
265
+             numsubpods='2'
266
+             primary='true'>
267
+          <subpod title=''>
268
+           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP350225h1ea85fgfbgb400005b1ebcefaha3ac97?...'
269
+               alt='x = -1'
270
+               title='x = -1'
271
+               width='47'
272
+               height='18' />
273
+           <plaintext>x = -1</plaintext>
274
+          </subpod>
275
+          <subpod title=''>
276
+           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP351225h1ea85fgfbgb4000032fic0ig981hc936?...'
277
+               alt='x = 0'
278
+               title='x = 0'
279
+               width='36'
280
+               height='18' />
281
+           <plaintext>x = 0</plaintext>
282
+          </subpod>
283
+          <states count='1'>
284
+           <state name='Step-by-step solution'
285
+               input='Result__Step-by-step solution' />
286
+          </states>
287
+         </pod>
288
+         <pod title='Root plot'
289
+             scanner='Solve'
290
+             id='RootPlot'
291
+             position='300'
292
+             error='false'
293
+             numsubpods='1'>
294
+          <subpod title=''>
295
+           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP352225h1ea85fgfbgb40000464054c665hc5dee?...'
296
+               alt=''
297
+               title=''
298
+               width='300'
299
+               height='181' />
300
+           <plaintext></plaintext>
301
+          </subpod>
302
+         </pod>
303
+         <pod title='Number line'
304
+             scanner='Solve'
305
+             id='NumberLine'
306
+             position='400'
307
+             error='false'
308
+             numsubpods='1'>
309
+          <subpod title=''>
310
+           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP353225h1ea85fgfbgb400005ab1c8aai366fe46?...'
311
+               alt=''
312
+               title=''
313
+               width='310'
314
+               height='36' />
315
+           <plaintext></plaintext>
316
+          </subpod>
317
+         </pod>
318
+        </queryresult>
319
+        """
320
+        # test ecuation with multiple answers
321
+        response = mock.Mock(content=xml)
322
+        results = wolframalpha_api.response(response)
323
+        self.assertEqual(type(results), list)
324
+        self.assertEqual(len(results), 2)
325
+        self.assertIn('x = -1', results[0]['answer'])
326
+        self.assertIn('x = 0', results[1]['answer'])

+ 77
- 173
searx/tests/engines/test_wolframalpha_noapi.py View File

@@ -40,7 +40,7 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
40 40
             </body>
41 41
         </html>
42 42
         """
43
-
43
+        # test failed query
44 44
         response = mock.Mock(text=html)
45 45
         self.assertEqual(wolframalpha_noapi.response(response), [])
46 46
 
@@ -51,113 +51,30 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
51 51
             <body>
52 52
                 <script type="text/javascript">
53 53
                   try {
54
-                    document.domain = "wolframalpha.com";
55
-                    context = parent ? parent : document;
56
-                } catch(e){}
57
-                try {
58
-                    if (typeof(context.$) == "undefined") {
59
-                        context = window;
60
-                    } else {
61
-                        $=context.$;
54
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
55
+                      context.jsonArray.popups.pod_0100 = [];
62 56
                     }
63
-                }
64
-                catch(e){ context = window;}
65
-
66
-            try {
67
-
68
-              if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
69
-                context.jsonArray.popups.pod_0100 = [];
70
-              }
71
-
72
-    context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": "", "popLinks": {} });
73
-
74
-            } catch(e) { }
75
-
76
-            try {
77
-
78
-            $("#results #pod_0100:not(iframe #pod_0100)")
79
-            .add("#showsteps #pod_0100:not(iframe #pod_0100)")
80
-            .add(".results-pod #pod_0100:not(iframe #pod_0100)")
81
-                .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
82
-                .data("podIdentifier", '\x22Input\x22')
83
-                .data("podShortIdentifier", '\x22Input\x22')
84
-                .data("buttonStates", '\x22\x22')
85
-                .data("scanner", '\x22\x22');
86
-            $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
87
-            .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
88
-            .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
89
-                    .data("tempFileID", 'MSP44501e0dda34g97a0c8900003i71207d6491ab22')
90
-                    .data("podIdentifier", '\x22Input\x22')
91
-                    .data("podShortIdentifier", '\x22Input\x22')
92
-                    .data("buttonStates", '\x22\x22')
93
-                    .data("scanner", '\x22\x22');
94
-
95
-              $("#results #subpod_0100_1")
96
-              .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
97
-              .add(".results-pod #subpod_0100_1")
98
-                    .data("tempFileID", "MSP44511e0dda34g97a0c89000059490h319161eea3")
99
-                    .data("cellDataTempFile", "MSP44521e0dda34g97a0c89000011378c50d38ede6h")
100
-                    .data("tempFileServer", "")
101
-                    .data("dataSources", "")
102
-                    .data("sources", "")
103
-                    .data("sharetype", "1")
104
-                    .data("shareable", "false");
105
-
106
-            } catch(e){}
107
-
108
-            //false
109
-
110
-            try {
111
-
112
-              if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
113
-                context.jsonArray.popups.pod_0200 = [];
114
-              }
115
-
116
-              context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": "", "popLinks": {} });
117
-
118
-            } catch(e) { }
119
-
120
-            try {
57
+                    context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": ""});
58
+                  } catch(e) { }
121 59
 
122
-            $("#results #pod_0200:not(iframe #pod_0200)")
123
-            .add("#showsteps #pod_0200:not(iframe #pod_0200)")
124
-            .add(".results-pod #pod_0200:not(iframe #pod_0200)")
125
-                .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
126
-                .data("podIdentifier", '\x22Result\x22')
127
-                .data("podShortIdentifier", '\x22Result\x22')
128
-                .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None,\x20None,\x20None\x7D')
129
-                .data("scanner", '\x22\x22');
130
-            $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
131
-            .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
132
-            .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
133
-                    .data("tempFileID", 'MSP44541e0dda34g97a0c8900004f449i50fa482fd8')
134
-                    .data("podIdentifier", '\x22Result\x22')
135
-                    .data("podShortIdentifier", '\x22Result\x22')
136
-                    .data("buttonStates", '\x22Result\x22\x20\x2D\x3E\x20\x7BAll,\x20None,\x20None\x7D')
137
-                    .data("scanner", '\x22\x22');
138
-
139
-              $("#results #subpod_0200_1")
140
-              .add("#showsteps #subpod_0200_1:not(iframe #subpod_0200_1)")
141
-              .add(".results-pod #subpod_0200_1")
142
-                    .data("tempFileID", "MSP44551e0dda34g97a0c8900003gdgd37faa7272e0")
143
-                    .data("cellDataTempFile", "MSP44561e0dda34g97a0c89000018ea1iae00104g13")
144
-                    .data("tempFileServer", "")
145
-                    .data("dataSources", "")
146
-                    .data("sources", "")
147
-                    .data("sharetype", "1")
148
-                    .data("shareable", "false");
149
-                    } catch(e){}
60
+                  try {
61
+                    if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
62
+                      context.jsonArray.popups.pod_0200 = [];
63
+                    }
64
+                    context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": ""});
65
+                  } catch(e) { }
150 66
                 </script>
151 67
             </body>
152 68
         </html>
153 69
         """
70
+        # test plaintext
154 71
         response = mock.Mock(text=html)
155 72
         results = wolframalpha_noapi.response(response)
156 73
         self.assertEqual(type(results), list)
157 74
         self.assertEqual(len(results), 2)
158
-        self.assertIn("i", results[0]['answer'])
159
-        self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
160
-        self.assertIn("http://www.wolframalpha.com/input/?i=+sqrt%28-1%29", results[1]['url'])
75
+        self.assertEquals('i', results[0]['answer'])
76
+        self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title'])
77
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+sqrt%28-1%29', results[1]['url'])
161 78
 
162 79
         html = """
163 80
         <!DOCTYPE html>
@@ -165,91 +82,78 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
165 82
             <meta charset="utf-8" />
166 83
             <body>
167 84
                 <script type="text/javascript">
168
-                //true
169 85
                   try {
170
-                    document.domain = "wolframalpha.com";
171
-                    context = parent ? parent : document;
172
-                    } catch(e){}
173
-                    try {
174
-                        if (typeof(context.$) == "undefined") {
175
-                            context = window;
176
-                        } else {
177
-                            $=context.$;
178
-                        }
86
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
87
+                      context.jsonArray.popups.pod_0100 = [];
179 88
                     }
180
-                    catch(e){ context = window;}
181
-
182
-                try {
183
-
184
-                  if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
185
-                    context.jsonArray.popups.pod_0100 = [];
186
-                  }
187
-
188
-                context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
189
-
190
-                } catch(e) { }
191
-
192
-                try {
193
-
194
-                $("#results #pod_0100:not(iframe #pod_0100)")
195
-                .add("#showsteps #pod_0100:not(iframe #pod_0100)")
196
-                .add(".results-pod #pod_0100:not(iframe #pod_0100)")
197
-                    .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
198
-                    .data("podIdentifier", '\x22IndefiniteIntegral\x22')
199
-                    .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
200
-                    .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
201
-                    .data("scanner", '\x22\x22');
202
-                $("#results #pod_0100-popup:not(iframe #pod_0100-popup)")
203
-                .add("#showsteps #pod_0100-popup:not(iframe #pod_0100-popup)")
204
-                .add(".results-pod #pod_0100-popup:not(iframe #pod_0100-popup)")
205
-                        .data("tempFileID", 'MSP2051if2202e8bg0757100000d119b05egf583d3')
206
-                        .data("podIdentifier", '\x22IndefiniteIntegral\x22')
207
-                        .data("podShortIdentifier", '\x22IndefiniteIntegral\x22')
208
-                        .data("buttonStates", '\x22Indefinite\x20integral\x22\x20\x2D\x3E\x20\x7B\x7D')
209
-                        .data("scanner", '\x22\x22');
210
-
211
-                  $("#results #subpod_0100_1")
212
-                  .add("#showsteps #subpod_0100_1:not(iframe #subpod_0100_1)")
213
-                  .add(".results-pod #subpod_0100_1")
214
-                        .data("tempFileID", "MSP2071if2202e8bg0757100004dg60f2a4ca8cf73")
215
-                        .data("cellDataTempFile", "MSP2081if2202e8bg0757100001h18329f72fe90fg")
216
-                        .data("tempFileServer", "")
217
-                        .data("dataSources", "")
218
-                        .data("sources", "")
219
-                        .data("sharetype", "1")
220
-                        .data("shareable", "false");
221
-
222
-                } catch(e){}
89
+                    context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
90
+                  } catch(e) { }
91
+                </script>
92
+            </body>
93
+        </html>
94
+        """
95
+        # test integral
96
+        response = mock.Mock(text=html)
97
+        results = wolframalpha_noapi.response(response)
98
+        self.assertEqual(type(results), list)
99
+        self.assertEqual(len(results), 2)
100
+        self.assertIn('log(x)+c', results[0]['answer'])
101
+        self.assertIn('integral 1/x - Wolfram|Alpha', results[1]['title'])
102
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+integral+1%2Fx', results[1]['url'])
223 103
 
224
-                //false
225
-                try {
104
+        html = """
105
+        <!DOCTYPE html>
106
+            <title> &int;1&#x2f;x &#xf74c;x - Wolfram|Alpha</title>
107
+            <meta charset="utf-8" />
108
+            <body>
109
+                <script type="text/javascript">
110
+                  try {
111
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
112
+                      context.jsonArray.popups.pod_0100 = [];
113
+                    }
114
+                    context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"});
115
+                  } catch(e) { }
116
+                </script>
117
+            </body>
118
+        </html>
119
+        """
120
+        # test input in mathematical notation
121
+        response = mock.Mock(text=html)
122
+        results = wolframalpha_noapi.response(response)
123
+        self.assertEqual(type(results), list)
124
+        self.assertEqual(len(results), 2)
125
+        self.assertIn('log(x)+c', results[0]['answer'])
126
+        self.assertIn('∫1/x x - Wolfram|Alpha'.decode('utf-8'), results[1]['title'])
127
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+%E2%88%AB1%2Fx+%EF%9D%8Cx', results[1]['url'])
226 128
 
227
-                $("#results #pod_0200:not(iframe #pod_0200)")
228
-                .add("#showsteps #pod_0200:not(iframe #pod_0200)")
229
-                .add(".results-pod #pod_0200:not(iframe #pod_0200)")
230
-                    .data("tempFileID", '')
231
-                    .data("podIdentifier", '\x22Plot\x22')
232
-                    .data("podShortIdentifier", '')
233
-                    .data("buttonStates", '')
234
-                    .data("scanner", '\x22\x22');
235
-                $("#results #pod_0200-popup:not(iframe #pod_0200-popup)")
236
-                .add("#showsteps #pod_0200-popup:not(iframe #pod_0200-popup)")
237
-                .add(".results-pod #pod_0200-popup:not(iframe #pod_0200-popup)")
238
-                        .data("tempFileID", '')
239
-                        .data("podIdentifier", '\x22Plot\x22')
240
-                        .data("podShortIdentifier", '')
241
-                        .data("buttonStates", '')
242
-                        .data("scanner", '\x22\x22');
129
+        html = """
130
+        <!DOCTYPE html>
131
+            <title> 1 euro to yen - Wolfram|Alpha</title>
132
+            <meta charset="utf-8" />
133
+            <body>
134
+                <script type="text/javascript">
135
+                  try {
136
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
137
+                      context.jsonArray.popups.pod_0100 = [];
138
+                    }
139
+                  context.jsonArray.popups.pod_0100.push( {"stringified": "convert euro1  (euro) to Japanese yen"});
140
+                  } catch(e) { }
243 141
 
244
-                } catch(e){}
142
+                  try {
143
+                    if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
144
+                      context.jsonArray.popups.pod_0200 = [];
145
+                    }
146
+                    context.jsonArray.popups.pod_0200.push( {"stringified": "&yen;130.5  (Japanese yen)"});
147
+                  } catch(e) { }
245 148
                 </script>
246 149
             </body>
247 150
         </html>
248 151
         """
152
+        # test output in htmlentity
249 153
         response = mock.Mock(text=html)
250 154
         results = wolframalpha_noapi.response(response)
251 155
         self.assertEqual(type(results), list)
252 156
         self.assertEqual(len(results), 2)
253
-        self.assertIn("log(x)+c", results[0]['answer'])
254
-        self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
255
-        self.assertIn("http://www.wolframalpha.com/input/?i=+integral+1%2Fx", results[1]['url'])
157
+        self.assertIn("¥".decode('utf-8'), results[0]['answer'])
158
+        self.assertIn('1 euro to yen - Wolfram|Alpha', results[1]['title'])
159
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+1+euro+to+yen', results[1]['url'])