From d997265e5599333b4316561ca18a8f4131e3e2d9 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sun, 3 Jan 2016 19:57:37 -0600 Subject: [PATCH] add tests for unicode strings in wolframalpha --- searx/engines/wolframalpha_noapi.py | 4 +- searx/tests/engines/test_wolframalpha_api.py | 138 +++++++--- .../tests/engines/test_wolframalpha_noapi.py | 256 ++++++------------ 3 files changed, 186 insertions(+), 212 deletions(-) diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index 0f0315630..71ad3b281 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -73,11 +73,11 @@ def response(resp): results.append({'answer': answer}) # user input is in first part of title - title = dom.xpath(title_xpath)[0].text + title = dom.xpath(title_xpath)[0].text.encode('utf-8') result_url = request(title[:-16], {})['url'] # append result results.append({'url': result_url, - 'title': title}) + 'title': title.decode('utf-8')}) return results diff --git a/searx/tests/engines/test_wolframalpha_api.py b/searx/tests/engines/test_wolframalpha_api.py index d9e23182f..98c53f769 100644 --- a/searx/tests/engines/test_wolframalpha_api.py +++ b/searx/tests/engines/test_wolframalpha_api.py @@ -30,32 +30,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase): xml = ''' ''' - - response = mock.Mock(content=xml) - self.assertEqual(wolframalpha_api.response(response), []) - - xml = """ - - - - - - """ - + # test failure response = mock.Mock(content=xml) self.assertEqual(wolframalpha_api.response(response), []) @@ -145,14 +120,12 @@ class TestWolframAlphaAPIEngine(SearxTestCase): """ + # test private user area char in response response = mock.Mock(content=xml) results = wolframalpha_api.response(response) self.assertEqual(type(results), list) - # self.assertEqual(len(results), 2) self.assertEqual(len(results), 1) - self.assertIn("i", results[0]['answer']) - # self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title']) - # self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url']) + self.assertIn('i', results[0]['answer']) xml = """ """ + # test integral response = mock.Mock(content=xml) results = wolframalpha_api.response(response) self.assertEqual(type(results), list) - # self.assertEqual(len(results), 2) self.assertEqual(len(results), 1) - self.assertIn("log(x)+c", results[0]['answer']) - # self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title']) - # self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url']) + self.assertIn('log(x)+c', results[0]['answer']) + + xml = """ + + + + solve x^2+x = 0 + solve x^2+x = 0</plaintext> + </subpod> + </pod> + <pod title='Results' + scanner='Solve' + id='Result' + position='200' + error='false' + numsubpods='2' + primary='true'> + <subpod title=''> + <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP350225h1ea85fgfbgb400005b1ebcefaha3ac97?...' + alt='x = -1' + title='x = -1' + width='47' + height='18' /> + <plaintext>x = -1</plaintext> + </subpod> + <subpod title=''> + <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP351225h1ea85fgfbgb4000032fic0ig981hc936?...' + alt='x = 0' + title='x = 0' + width='36' + height='18' /> + <plaintext>x = 0</plaintext> + </subpod> + <states count='1'> + <state name='Step-by-step solution' + input='Result__Step-by-step solution' /> + </states> + </pod> + <pod title='Root plot' + scanner='Solve' + id='RootPlot' + position='300' + error='false' + numsubpods='1'> + <subpod title=''> + <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP352225h1ea85fgfbgb40000464054c665hc5dee?...' + alt='' + title='' + width='300' + height='181' /> + <plaintext></plaintext> + </subpod> + </pod> + <pod title='Number line' + scanner='Solve' + id='NumberLine' + position='400' + error='false' + numsubpods='1'> + <subpod title=''> + <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP353225h1ea85fgfbgb400005ab1c8aai366fe46?...' + alt='' + title='' + width='310' + height='36' /> + <plaintext></plaintext> + </subpod> + </pod> + </queryresult> + """ + # test ecuation with multiple answers + response = mock.Mock(content=xml) + results = wolframalpha_api.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertIn('x = -1', results[0]['answer']) + self.assertIn('x = 0', results[1]['answer']) diff --git a/searx/tests/engines/test_wolframalpha_noapi.py b/searx/tests/engines/test_wolframalpha_noapi.py index 237f578db..3b6314672 100644 --- a/searx/tests/engines/test_wolframalpha_noapi.py +++ b/searx/tests/engines/test_wolframalpha_noapi.py @@ -40,7 +40,7 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): </body> </html> """ - + # test failed query response = mock.Mock(text=html) self.assertEqual(wolframalpha_noapi.response(response), []) @@ -51,113 +51,30 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): <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.$; + if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) { + context.jsonArray.popups.pod_0100 = []; } - } - catch(e){ context = window;} + context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": ""}); + } catch(e) { } - 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){} + try { + if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) { + context.jsonArray.popups.pod_0200 = []; + } + context.jsonArray.popups.pod_0200.push( {"stringified": "i","mInput": "","mOutput": ""}); + } catch(e) { } </script> </body> </html> """ + # test plaintext response = mock.Mock(text=html) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) - self.assertIn("i", results[0]['answer']) - self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title']) - self.assertIn("http://www.wolframalpha.com/input/?i=+sqrt%28-1%29", results[1]['url']) + self.assertEquals('i', results[0]['answer']) + self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title']) + self.assertEquals('http://www.wolframalpha.com/input/?i=+sqrt%28-1%29', results[1]['url']) html = """ <!DOCTYPE html> @@ -165,91 +82,78 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): <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.$; - } + if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) { + context.jsonArray.popups.pod_0100 = []; } - 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){} + context.jsonArray.popups.pod_0100.push( {"stringified": "integral 1\/x dx = log(x)+constant"}); + } catch(e) { } </script> </body> </html> """ + # test integral response = mock.Mock(text=html) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) - self.assertIn("log(x)+c", results[0]['answer']) - self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title']) - self.assertIn("http://www.wolframalpha.com/input/?i=+integral+1%2Fx", results[1]['url']) + self.assertIn('log(x)+c', results[0]['answer']) + self.assertIn('integral 1/x - Wolfram|Alpha', results[1]['title']) + self.assertEquals('http://www.wolframalpha.com/input/?i=+integral+1%2Fx', results[1]['url']) + + html = """ + <!DOCTYPE html> + <title> &int;1&#x2f;x &#xf74c;x - Wolfram|Alpha</title> + <meta charset="utf-8" /> + <body> + <script type="text/javascript"> + 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) { } + </script> + </body> + </html> + """ + # test input in mathematical notation + response = mock.Mock(text=html) + results = wolframalpha_noapi.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertIn('log(x)+c', results[0]['answer']) + self.assertIn('∫1/x x - Wolfram|Alpha'.decode('utf-8'), results[1]['title']) + self.assertEquals('http://www.wolframalpha.com/input/?i=+%E2%88%AB1%2Fx+%EF%9D%8Cx', results[1]['url']) + + html = """ + <!DOCTYPE html> + <title> 1 euro to yen - Wolfram|Alpha</title> + <meta charset="utf-8" /> + <body> + <script type="text/javascript"> + try { + if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) { + context.jsonArray.popups.pod_0100 = []; + } + context.jsonArray.popups.pod_0100.push( {"stringified": "convert euro1 (euro) to Japanese yen"}); + } catch(e) { } + + try { + if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) { + context.jsonArray.popups.pod_0200 = []; + } + context.jsonArray.popups.pod_0200.push( {"stringified": "&yen;130.5 (Japanese yen)"}); + } catch(e) { } + </script> + </body> + </html> + """ + # test output in htmlentity + response = mock.Mock(text=html) + results = wolframalpha_noapi.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertIn("¥".decode('utf-8'), results[0]['answer']) + self.assertIn('1 euro to yen - Wolfram|Alpha', results[1]['title']) + self.assertEquals('http://www.wolframalpha.com/input/?i=+1+euro+to+yen', results[1]['url'])