Browse Source

[fix] test in wolframalpha_noapi

a01200356 9 years ago
parent
commit
2a15944b58

+ 6
- 3
searx/engines/wolframalpha_noapi.py View File

@@ -41,8 +41,8 @@ def response(resp):
41 41
 
42 42
     # the answer is inside a js function
43 43
     # answer can be located in different 'pods', although by default it should be in pod_0200
44
-    possible_locations = ['pod_0200\.push(.*)\n',
45
-                          'pod_0100\.push(.*)\n']
44
+    possible_locations = ['pod_0200\.push\((.*)',
45
+                          'pod_0100\.push\((.*)']
46 46
 
47 47
     # failed result
48 48
     if dom.xpath(failure_xpath):
@@ -62,7 +62,10 @@ def response(resp):
62 62
     if line:
63 63
         # extract answer from json
64 64
         answer = line[line.find('{'):line.rfind('}')+1]
65
-        answer = loads(answer.encode('unicode-escape'))
65
+        try:
66
+            answer = loads(answer)
67
+        except Exception:
68
+            answer = loads(answer.encode('unicode-escape'))
66 69
         answer = answer['stringified']
67 70
 
68 71
         # clean plaintext answer

+ 36
- 2
searx/tests/engines/test_wolframalpha_noapi.py View File

@@ -149,11 +149,45 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
149 149
             </body>
150 150
         </html>
151 151
         """
152
-        # test output in htmlentity
152
+        # test output with htmlentity
153 153
         response = mock.Mock(text=html)
154 154
         results = wolframalpha_noapi.response(response)
155 155
         self.assertEqual(type(results), list)
156 156
         self.assertEqual(len(results), 2)
157
-        self.assertIn("¥".decode('utf-8'), results[0]['answer'])
157
+        self.assertIn('¥'.decode('utf-8'), results[0]['answer'])
158 158
         self.assertIn('1 euro to yen - Wolfram|Alpha', results[1]['title'])
159 159
         self.assertEquals('http://www.wolframalpha.com/input/?i=+1+euro+to+yen', results[1]['url'])
160
+
161
+        html = """
162
+        <!DOCTYPE html>
163
+            <title> distance from nairobi to kyoto in inches - Wolfram|Alpha</title>
164
+            <meta charset="utf-8" />
165
+            <body>
166
+                <script type="text/javascript">
167
+                  try {
168
+                    if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) {
169
+                      context.jsonArray.popups.pod_0100 = [];
170
+                    }
171
+[...].pod_0100.push( {"stringified": "convert distance | from | Nairobi, Kenya\nto | Kyoto, Japan to inches"});
172
+                  } catch(e) { }
173
+
174
+                  try {
175
+                    if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) {
176
+                      context.jsonArray.popups.pod_0200 = [];
177
+                    }
178
+pod_0200.push({"stringified": "4.295&times;10^8 inches","mOutput": "Quantity[4.295×10^8,&amp;quot;Inches&amp;quot;]"});
179
+
180
+                  } catch(e) { }
181
+                </script>
182
+            </body>
183
+        </html>
184
+        """
185
+        # test output with utf-8 character
186
+        response = mock.Mock(text=html)
187
+        results = wolframalpha_noapi.response(response)
188
+        self.assertEqual(type(results), list)
189
+        self.assertEqual(len(results), 2)
190
+        self.assertIn('4.295×10^8 inches'.decode('utf-8'), results[0]['answer'])
191
+        self.assertIn('distance from nairobi to kyoto in inches - Wolfram|Alpha', results[1]['title'])
192
+        self.assertEquals('http://www.wolframalpha.com/input/?i=+distance+from+nairobi+to+kyoto+in+inches',
193
+                          results[1]['url'])