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, '[]') html = """ <!DOCTYPE html> <title> Parangaricutirimícuaro - Wolfram|Alpha</title> <meta charset="utf-8" /> <body> <div id="closest"> <p class="pfail">Wolfram|Alpha doesn't know how to interpret your input.</p> <div id="dtips"> <div class="tip"> <span class="tip-title">Tip: </span> Check your spelling, and use English <span class="tip-extra"></span> </div> </div> </div> </body> </html> """ # test failed query response = mock.Mock(text=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 { if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) { context.jsonArray.popups.pod_0100 = []; } context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": ""}); } 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.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> <title> integral 1/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 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.assertEquals('http://www.wolframalpha.com/input/?i=+integral+1%2Fx', results[1]['url']) html = """ <!DOCTYPE html> <title> ∫1/x 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": "¥130.5 (Japanese yen)"}); } catch(e) { } </script> </body> </html> """ # test output with 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']) html = """ <!DOCTYPE html> <title> distance from nairobi to kyoto in inches - 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 = []; } [...].pod_0100.push( {"stringified": "convert distance | from | Nairobi, Kenya\nto | Kyoto, Japan to inches"}); } catch(e) { } try { if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) { context.jsonArray.popups.pod_0200 = []; } pod_0200.push({"stringified": "4.295×10^8 inches","mOutput": "Quantity[4.295×10^8,&quot;Inches&quot;]"}); } catch(e) { } </script> </body> </html> """ # test output with utf-8 character response = mock.Mock(text=html) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertIn('4.295×10^8 inches'.decode('utf-8'), results[0]['answer']) self.assertIn('distance from nairobi to kyoto in inches - Wolfram|Alpha', results[1]['title']) self.assertEquals('http://www.wolframalpha.com/input/?i=+distance+from+nairobi+to+kyoto+in+inches', results[1]['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, '[]') referer_url = 'referer_url' request = Request(headers={'Referer': referer_url}) # test failure json = r''' {"queryresult" : { "success" : false, "error" : false, "numpods" : 0, "id" : "", "host" : "https:\/\/www5a.wolframalpha.com", "didyoumeans" : {} }} ''' response = mock.Mock(text=json, request=request) self.assertEqual(wolframalpha_noapi.response(response), []) # test basic case json = r''' {"queryresult" : { "success" : true, "error" : false, "numpods" : 6, "datatypes" : "Math", "id" : "queryresult_id", "host" : "https:\/\/www5b.wolframalpha.com", "related" : "related_url", "version" : "2.6", "pods" : [ { "title" : "Input", "scanners" : [ "Identity" ], "id" : "Input", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "img" : { "src" : "input_img_src.gif", "alt" : "input_img_alt", "title" : "input_img_title" }, "plaintext" : "input_plaintext", "minput" : "input_minput" } ] }, { "title" : "Result", "scanners" : [ "Simplification" ], "id" : "Result", "error" : false, "numsubpods" : 1, "primary" : true, "subpods" : [ { "title" : "", "img" : { "src" : "result_img_src.gif", "alt" : "result_img_alt", "title" : "result_img_title" }, "plaintext" : "result_plaintext", "moutput" : "result_moutput" } ] }, { "title" : "Manipulatives illustration", "scanners" : [ "Arithmetic" ], "id" : "Illustration", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "CDFcontent" : "Resizeable", "img" : { "src" : "illustration_img_src.gif", "alt" : "illustration_img_alt", "title" : "illustration_img_title" }, "plaintext" : "illustration_img_plaintext" } ] } ] }} ''' response = mock.Mock(text=json, request=request) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual('input_plaintext', results[0]['infobox']) self.assertEqual(len(results[0]['attributes']), 3) self.assertEqual('Input', results[0]['attributes'][0]['label']) self.assertEqual('input_plaintext', results[0]['attributes'][0]['value']) self.assertEqual('Result', results[0]['attributes'][1]['label']) self.assertEqual('result_plaintext', results[0]['attributes'][1]['value']) self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label']) self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) self.assertEqual(len(results[0]['urls']), 1) self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) self.assertEqual('Wolfram|Alpha (input_plaintext)', results[1]['title']) self.assertIn('result_plaintext', results[1]['content']) # test calc json = r""" {"queryresult" : { "success" : true, "error" : false, "numpods" : 2, "datatypes" : "", "id" : "queryresult_id", "host" : "https:\/\/www4b.wolframalpha.com", "related" : "related_url", "version" : "2.6", "pods" : [ { "title" : "Indefinite integral", "scanners" : [ "Integral" ], "id" : "IndefiniteIntegral", "error" : false, "numsubpods" : 1, "primary" : true, "subpods" : [ { "title" : "", "img" : { "src" : "integral_img_src.gif", "alt" : "integral_img_alt", "title" : "integral_img_title" }, "plaintext" : "integral_plaintext", "minput" : "integral_minput", "moutput" : "integral_moutput" } ] }, { "title" : "Plot of the integral", "scanners" : [ "Integral" ], "id" : "Plot", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "img" : { "src" : "plot.gif", "alt" : "plot_alt", "title" : "plot_title" }, "plaintext" : "", "minput" : "plot_minput" } ] } ] }} """ response = mock.Mock(text=json, request=request) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual('integral_plaintext', results[0]['infobox']) self.assertEqual(len(results[0]['attributes']), 2) self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label']) self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value']) self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label']) self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src']) self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt']) self.assertEqual(len(results[0]['urls']), 1) self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title']) self.assertIn('integral_plaintext', results[1]['content'])
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, '[]') html = """ <!DOCTYPE html> <title> Parangaricutirimícuaro - Wolfram|Alpha</title> <meta charset="utf-8" /> <body> <div id="closest"> <p class="pfail">Wolfram|Alpha doesn't know how to interpret your input.</p> <div id="dtips"> <div class="tip"> <span class="tip-title">Tip: </span> Check your spelling, and use English <span class="tip-extra"></span> </div> </div> </div> </body> </html> """ # test failed query response = mock.Mock(text=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 { if (typeof context.jsonArray.popups.pod_0100 == "undefined" ) { context.jsonArray.popups.pod_0100 = []; } context.jsonArray.popups.pod_0100.push( {"stringified": "sqrt(-1)","mInput": "","mOutput": ""}); } 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.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> <title> integral 1/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 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.assertEquals( 'http://www.wolframalpha.com/input/?i=+integral+1%2Fx', results[1]['url']) html = """ <!DOCTYPE html> <title> ∫1/x 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": "¥130.5 (Japanese yen)"}); } catch(e) { } </script> </body> </html> """ # test output with 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']) html = """ <!DOCTYPE html> <title> distance from nairobi to kyoto in inches - 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 = []; } [...].pod_0100.push( {"stringified": "convert distance | from | Nairobi, Kenya\nto | Kyoto, Japan to inches"}); } catch(e) { } try { if (typeof context.jsonArray.popups.pod_0200 == "undefined" ) { context.jsonArray.popups.pod_0200 = []; } pod_0200.push({"stringified": "4.295×10^8 inches","mOutput": "Quantity[4.295×10^8,&quot;Inches&quot;]"}); } catch(e) { } </script> </body> </html> """ # test output with utf-8 character response = mock.Mock(text=html) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertIn('4.295×10^8 inches'.decode('utf-8'), results[0]['answer']) self.assertIn( 'distance from nairobi to kyoto in inches - Wolfram|Alpha', results[1]['title']) self.assertEquals( 'http://www.wolframalpha.com/input/?i=+distance+from+nairobi+to+kyoto+in+inches', results[1]['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, '[]') referer_url = 'referer_url' request = Request(headers={'Referer': referer_url}) # test failure json = r''' {"queryresult" : { "success" : false, "error" : false, "numpods" : 0, "id" : "", "host" : "https:\/\/www5a.wolframalpha.com", "didyoumeans" : {} }} ''' response = mock.Mock(text=json, request=request) self.assertEqual(wolframalpha_noapi.response(response), []) # test basic case json = r''' {"queryresult" : { "success" : true, "error" : false, "numpods" : 6, "datatypes" : "Math", "id" : "queryresult_id", "host" : "https:\/\/www5b.wolframalpha.com", "related" : "related_url", "version" : "2.6", "pods" : [ { "title" : "Input", "scanners" : [ "Identity" ], "id" : "Input", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "img" : { "src" : "input_img_src.gif", "alt" : "input_img_alt", "title" : "input_img_title" }, "plaintext" : "input_plaintext", "minput" : "input_minput" } ] }, { "title" : "Result", "scanners" : [ "Simplification" ], "id" : "Result", "error" : false, "numsubpods" : 1, "primary" : true, "subpods" : [ { "title" : "", "img" : { "src" : "result_img_src.gif", "alt" : "result_img_alt", "title" : "result_img_title" }, "plaintext" : "result_plaintext", "moutput" : "result_moutput" } ] }, { "title" : "Manipulatives illustration", "scanners" : [ "Arithmetic" ], "id" : "Illustration", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "CDFcontent" : "Resizeable", "img" : { "src" : "illustration_img_src.gif", "alt" : "illustration_img_alt", "title" : "illustration_img_title" }, "plaintext" : "illustration_img_plaintext" } ] } ] }} ''' response = mock.Mock(text=json, request=request) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual('input_plaintext', results[0]['infobox']) self.assertEqual(len(results[0]['attributes']), 3) self.assertEqual('Input', results[0]['attributes'][0]['label']) self.assertEqual('input_plaintext', results[0]['attributes'][0]['value']) self.assertEqual('Result', results[0]['attributes'][1]['label']) self.assertEqual('result_plaintext', results[0]['attributes'][1]['value']) self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label']) self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) self.assertEqual(len(results[0]['urls']), 1) self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) self.assertEqual('Wolfram|Alpha (input_plaintext)', results[1]['title']) self.assertIn('result_plaintext', results[1]['content']) # test calc json = r""" {"queryresult" : { "success" : true, "error" : false, "numpods" : 2, "datatypes" : "", "id" : "queryresult_id", "host" : "https:\/\/www4b.wolframalpha.com", "related" : "related_url", "version" : "2.6", "pods" : [ { "title" : "Indefinite integral", "scanners" : [ "Integral" ], "id" : "IndefiniteIntegral", "error" : false, "numsubpods" : 1, "primary" : true, "subpods" : [ { "title" : "", "img" : { "src" : "integral_img_src.gif", "alt" : "integral_img_alt", "title" : "integral_img_title" }, "plaintext" : "integral_plaintext", "minput" : "integral_minput", "moutput" : "integral_moutput" } ] }, { "title" : "Plot of the integral", "scanners" : [ "Integral" ], "id" : "Plot", "error" : false, "numsubpods" : 1, "subpods" : [ { "title" : "", "img" : { "src" : "plot.gif", "alt" : "plot_alt", "title" : "plot_title" }, "plaintext" : "", "minput" : "plot_minput" } ] } ] }} """ response = mock.Mock(text=json, request=request) results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual('integral_plaintext', results[0]['infobox']) self.assertEqual(len(results[0]['attributes']), 2) self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label']) self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value']) self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label']) self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src']) self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt']) self.assertEqual(len(results[0]['urls']), 1) self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title']) self.assertIn('integral_plaintext', results[1]['content'])