Exemple #1
0
    def test_response(self):
        self.assertRaises(AttributeError, wolframalpha_api.response, None)
        self.assertRaises(AttributeError, wolframalpha_api.response, [])
        self.assertRaises(AttributeError, wolframalpha_api.response, '')
        self.assertRaises(AttributeError, wolframalpha_api.response, '[]')

        referer_url = 'referer_url'
        request = Request(headers={'Referer': referer_url})

        # test failure
        xml = '''<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='false' error='false' />
        '''
        response = mock.Mock(text=xml.encode('utf-8'))
        self.assertEqual(wolframalpha_api.response(response), [])

        # test basic case
        xml = b"""<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='3'
            datatypes='Math'
            id='queryresult_id'
            host='http://www4c.wolframalpha.com'
            related='related_url'
            version='2.6'>
            <pod title='Input'
                 scanner='Identity'
                 id='Input'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='input_img_src.gif'
                           alt='input_img_alt'
                           title='input_img_title' />
                       <plaintext>input_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Result'
                 scanner='Simplification'
                 id='Result'
                 numsubpods='1'
                 primary='true'>
                  <subpod title=''>
                       <img src='result_img_src.gif'
                           alt='result_img_alt'
                           title='result_img_title' />
                       <plaintext>result_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Manipulatives illustration'
                 scanner='Arithmetic'
                 id='Illustration'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='illustration_img_src.gif'
                           alt='illustration_img_alt' />
                       <plaintext>illustration_plaintext</plaintext>
                  </subpod>
             </pod>
        </queryresult>
        """
        response = mock.Mock(text=xml, request=request)
        results = wolframalpha_api.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
        xml = b"""<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='2'
            datatypes=''
            parsetimedout='false'
            id='queryresult_id'
            host='http://www5b.wolframalpha.com'
            related='related_url'
            version='2.6' >
            <pod title='Indefinite integral'
                scanner='Integral'
                id='IndefiniteIntegral'
                error='false'
                numsubpods='1'
                primary='true'>
                <subpod title=''>
                    <img src='integral_image.gif'
                        alt='integral_img_alt'
                        title='integral_img_title' />
                    <plaintext>integral_plaintext</plaintext>
                </subpod>
            </pod>
            <pod title='Plot of the integral'
                scanner='Integral'
                id='Plot'
                error='false'
                numsubpods='1'>
                <subpod title=''>
                    <img src='plot.gif'
                        alt='plot_alt'
                        title='' />
                    <plaintext></plaintext>
                </subpod>
            </pod>
        </queryresult>
        """
        response = mock.Mock(text=xml, request=request)
        results = wolframalpha_api.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'])
Exemple #2
0
    def test_response(self):
        self.assertRaises(AttributeError, wolframalpha_api.response, None)
        self.assertRaises(AttributeError, wolframalpha_api.response, [])
        self.assertRaises(AttributeError, wolframalpha_api.response, '')
        self.assertRaises(AttributeError, wolframalpha_api.response, '[]')

        referer_url = 'referer_url'
        request = Request(headers={'Referer': referer_url})

        # test failure
        xml = '''<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='false' error='false' />
        '''
        response = mock.Mock(text=xml.encode('utf-8'))
        self.assertEqual(wolframalpha_api.response(response), [])

        # test basic case
        xml = b"""<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='3'
            datatypes='Math'
            id='queryresult_id'
            host='http://www4c.wolframalpha.com'
            related='related_url'
            version='2.6'>
            <pod title='Input'
                 scanner='Identity'
                 id='Input'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='input_img_src.gif'
                           alt='input_img_alt'
                           title='input_img_title' />
                       <plaintext>input_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Result'
                 scanner='Simplification'
                 id='Result'
                 numsubpods='1'
                 primary='true'>
                  <subpod title=''>
                       <img src='result_img_src.gif'
                           alt='result_img_alt'
                           title='result_img_title' />
                       <plaintext>result_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Manipulatives illustration'
                 scanner='Arithmetic'
                 id='Illustration'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='illustration_img_src.gif'
                           alt='illustration_img_alt' />
                       <plaintext>illustration_plaintext</plaintext>
                  </subpod>
             </pod>
        </queryresult>
        """
        response = mock.Mock(text=xml, request=request)
        results = wolframalpha_api.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
        xml = b"""<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='2'
            datatypes=''
            parsetimedout='false'
            id='queryresult_id'
            host='http://www5b.wolframalpha.com'
            related='related_url'
            version='2.6' >
            <pod title='Indefinite integral'
                scanner='Integral'
                id='IndefiniteIntegral'
                error='false'
                numsubpods='1'
                primary='true'>
                <subpod title=''>
                    <img src='integral_image.gif'
                        alt='integral_img_alt'
                        title='integral_img_title' />
                    <plaintext>integral_plaintext</plaintext>
                </subpod>
            </pod>
            <pod title='Plot of the integral'
                scanner='Integral'
                id='Plot'
                error='false'
                numsubpods='1'>
                <subpod title=''>
                    <img src='plot.gif'
                        alt='plot_alt'
                        title='' />
                    <plaintext></plaintext>
                </subpod>
            </pod>
        </queryresult>
        """
        response = mock.Mock(text=xml, request=request)
        results = wolframalpha_api.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_api.response, None)
        self.assertRaises(AttributeError, wolframalpha_api.response, [])
        self.assertRaises(AttributeError, wolframalpha_api.response, '')
        self.assertRaises(AttributeError, wolframalpha_api.response, '[]')

        xml = '''<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='false' error='false' />
        '''
        # test failure
        response = mock.Mock(content=xml)
        self.assertEqual(wolframalpha_api.response(response), [])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='6'
            datatypes=''
            timedout=''
            timedoutpods=''
            timing='0.684'
            parsetiming='0.138'
            parsetimedout='false'
            recalculate=''
            id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
            host='http://www3.wolframalpha.com'
            server='6'
            related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
            version='2.6'>
         <pod title='Input'
             scanner='Identity'
             id='Input'
             position='100'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>sqrt(-1)</plaintext>
          </subpod>
         </pod>
         <pod title='Result'
             scanner='Simplification'
             id='Result'
             position='200'
             error='false'
             numsubpods='1'
             primary='true'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
          <states count='1'>
           <state name='Step-by-step solution'
               input='Result__Step-by-step solution' />
          </states>
         </pod>
         <pod title='Polar coordinates'
             scanner='Numeric'
             id='PolarCoordinates'
             position='300'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>r1 (radius), θ90° (angle)</plaintext>
          </subpod>
         </pod>
         <pod title='Position in the complex plane'
             scanner='Numeric'
             id='PositionInTheComplexPlane'
             position='400'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
         <pod title='All 2nd roots of -1'
             scanner='RootsOfUnity'
             id=''
             position='500'
             error='false'
             numsubpods='2'>
          <subpod title=''>
           <plaintext>  (principal root)</plaintext>
          </subpod>
          <subpod title=''>
           <plaintext>-</plaintext>
          </subpod>
         </pod>
         <pod title='Plot of all roots in the complex plane'
             scanner='RootsOfUnity'
             id='PlotOfAllRootsInTheComplexPlane'
             position='600'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
        </queryresult>
        """
        # 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.assertIn('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'])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
            <queryresult success='true'
                error='false'
                numpods='2'
                datatypes=''
                timedout=''
                timedoutpods=''
                timing='1.286'
                parsetiming='0.255'
                parsetimedout='false'
                recalculate=''
                id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
                host='http://www3.wolframalpha.com'
                server='20'
                related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
                version='2.6'>
             <pod title='Indefinite integral'
                 scanner='Integral'
                 id='IndefiniteIntegral'
                 position='100'
                 error='false'
                 numsubpods='1'
                 primary='true'>
              <subpod title=''>
               <plaintext>∫1/xxlog(x)+constant</plaintext>
              </subpod>
              <states count='1'>
               <state name='Step-by-step solution'
                   input='IndefiniteIntegral__Step-by-step solution' />
              </states>
              <infos count='1'>
               <info text='log(x) is the natural logarithm'>
                <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
                    text='Documentation'
                    title='Mathematica' />
                <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
                    text='Properties'
                    title='Wolfram Functions Site' />
                <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
                    text='Definition'
                    title='MathWorld' />
               </info>
              </infos>
             </pod>
             <pod title='Plots of the integral'
                 scanner='Integral'
                 id='Plot'
                 position='200'
                 error='false'
                 numsubpods='2'>
              <subpod title=''>
               <plaintext></plaintext>
               <states count='1'>
                <statelist count='2'
                    value='Complex-valued plot'
                    delimiters=''>
                 <state name='Complex-valued plot'
                     input='Plot__1_Complex-valued plot' />
                 <state name='Real-valued plot'
                     input='Plot__1_Real-valued plot' />
                </statelist>
               </states>
              </subpod>
              <subpod title=''>
               <plaintext></plaintext>
               <states count='1'>
                <statelist count='2'
                    value='Complex-valued plot'
                    delimiters=''>
                 <state name='Complex-valued plot'
                     input='Plot__2_Complex-valued plot' />
                 <state name='Real-valued plot'
                     input='Plot__2_Real-valued plot' />
                </statelist>
               </states>
              </subpod>
             </pod>
             <assumptions count='1'>
              <assumption type='Clash'
                  word='integral'
                  template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
                  count='2'>
               <value name='IntegralsWord'
                   desc='an integral'
                   input='*C.integral-_*IntegralsWord-' />
               <value name='MathematicalFunctionIdentityPropertyClass'
                   desc='a function property'
                   input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
              </assumption>
             </assumptions>
            </queryresult>
        """
        # test integral
        response = mock.Mock(content=xml)
        results = wolframalpha_api.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'])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='4'
            datatypes='Solve'
            timedout=''
            timedoutpods=''
            timing='0.79'
            parsetiming='0.338'
            parsetimedout='false'
            recalculate=''
            id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc'
            host='http://www5b.wolframalpha.com'
            server='23'
            related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
            version='2.6'>
         <pod title='Input interpretation'
             scanner='Identity'
             id='Input'
             position='100'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>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=''>
           <plaintext>x-1</plaintext>
          </subpod>
          <subpod title=''>
           <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=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
         <pod title='Number line'
             scanner='Solve'
             id='NumberLine'
             position='400'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <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), 3)
        self.assertIn('x=-1', results[0]['answer'])
        self.assertIn('x=0', results[1]['answer'])
        self.assertIn('solve x^2+x0 - Wolfram|Alpha'.decode('utf-8'), results[2]['title'])
        self.assertEquals('http://www.wolframalpha.com/input/?i=solve+x%5E2%2Bx%EF%9F%990', results[2]['url'])
    def test_response(self):
        self.assertRaises(AttributeError, wolframalpha_api.response, None)
        self.assertRaises(AttributeError, wolframalpha_api.response, [])
        self.assertRaises(AttributeError, wolframalpha_api.response, '')
        self.assertRaises(AttributeError, wolframalpha_api.response, '[]')

        xml = '''<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='false' error='false' />
        '''
        # test failure
        response = mock.Mock(content=xml)
        self.assertEqual(wolframalpha_api.response(response), [])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='6'
            datatypes=''
            timedout=''
            timedoutpods=''
            timing='0.684'
            parsetiming='0.138'
            parsetimedout='false'
            recalculate=''
            id='MSPa416020a7966dachc463600000f9c66cc21444cfg'
            host='http://www3.wolframalpha.com'
            server='6'
            related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...'
            version='2.6'>
         <pod title='Input'
             scanner='Identity'
             id='Input'
             position='100'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>sqrt(-1)</plaintext>
          </subpod>
         </pod>
         <pod title='Result'
             scanner='Simplification'
             id='Result'
             position='200'
             error='false'
             numsubpods='1'
             primary='true'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
          <states count='1'>
           <state name='Step-by-step solution'
               input='Result__Step-by-step solution' />
          </states>
         </pod>
         <pod title='Polar coordinates'
             scanner='Numeric'
             id='PolarCoordinates'
             position='300'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>r1 (radius), θ90° (angle)</plaintext>
          </subpod>
         </pod>
         <pod title='Position in the complex plane'
             scanner='Numeric'
             id='PositionInTheComplexPlane'
             position='400'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
         <pod title='All 2nd roots of -1'
             scanner='RootsOfUnity'
             id=''
             position='500'
             error='false'
             numsubpods='2'>
          <subpod title=''>
           <plaintext>  (principal root)</plaintext>
          </subpod>
          <subpod title=''>
           <plaintext>-</plaintext>
          </subpod>
         </pod>
         <pod title='Plot of all roots in the complex plane'
             scanner='RootsOfUnity'
             id='PlotOfAllRootsInTheComplexPlane'
             position='600'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
        </queryresult>
        """
        # 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.assertIn('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'])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
            <queryresult success='true'
                error='false'
                numpods='2'
                datatypes=''
                timedout=''
                timedoutpods=''
                timing='1.286'
                parsetiming='0.255'
                parsetimedout='false'
                recalculate=''
                id='MSPa195222ad740ede5214h30000480ca61h003d3gd6'
                host='http://www3.wolframalpha.com'
                server='20'
                related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
                version='2.6'>
             <pod title='Indefinite integral'
                 scanner='Integral'
                 id='IndefiniteIntegral'
                 position='100'
                 error='false'
                 numsubpods='1'
                 primary='true'>
              <subpod title=''>
               <plaintext>∫1/xxlog(x)+constant</plaintext>
              </subpod>
              <states count='1'>
               <state name='Step-by-step solution'
                   input='IndefiniteIntegral__Step-by-step solution' />
              </states>
              <infos count='1'>
               <info text='log(x) is the natural logarithm'>
                <link url='http://reference.wolfram.com/mathematica/ref/Log.html'
                    text='Documentation'
                    title='Mathematica' />
                <link url='http://functions.wolfram.com/ElementaryFunctions/Log'
                    text='Properties'
                    title='Wolfram Functions Site' />
                <link url='http://mathworld.wolfram.com/NaturalLogarithm.html'
                    text='Definition'
                    title='MathWorld' />
               </info>
              </infos>
             </pod>
             <pod title='Plots of the integral'
                 scanner='Integral'
                 id='Plot'
                 position='200'
                 error='false'
                 numsubpods='2'>
              <subpod title=''>
               <plaintext></plaintext>
               <states count='1'>
                <statelist count='2'
                    value='Complex-valued plot'
                    delimiters=''>
                 <state name='Complex-valued plot'
                     input='Plot__1_Complex-valued plot' />
                 <state name='Real-valued plot'
                     input='Plot__1_Real-valued plot' />
                </statelist>
               </states>
              </subpod>
              <subpod title=''>
               <plaintext></plaintext>
               <states count='1'>
                <statelist count='2'
                    value='Complex-valued plot'
                    delimiters=''>
                 <state name='Complex-valued plot'
                     input='Plot__2_Complex-valued plot' />
                 <state name='Real-valued plot'
                     input='Plot__2_Real-valued plot' />
                </statelist>
               </states>
              </subpod>
             </pod>
             <assumptions count='1'>
              <assumption type='Clash'
                  word='integral'
                  template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead'
                  count='2'>
               <value name='IntegralsWord'
                   desc='an integral'
                   input='*C.integral-_*IntegralsWord-' />
               <value name='MathematicalFunctionIdentityPropertyClass'
                   desc='a function property'
                   input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' />
              </assumption>
             </assumptions>
            </queryresult>
        """
        # test integral
        response = mock.Mock(content=xml)
        results = wolframalpha_api.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'])

        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='4'
            datatypes='Solve'
            timedout=''
            timedoutpods=''
            timing='0.79'
            parsetiming='0.338'
            parsetimedout='false'
            recalculate=''
            id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc'
            host='http://www5b.wolframalpha.com'
            server='23'
            related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
            version='2.6'>
         <pod title='Input interpretation'
             scanner='Identity'
             id='Input'
             position='100'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <plaintext>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=''>
           <plaintext>x-1</plaintext>
          </subpod>
          <subpod title=''>
           <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=''>
           <plaintext></plaintext>
          </subpod>
         </pod>
         <pod title='Number line'
             scanner='Solve'
             id='NumberLine'
             position='400'
             error='false'
             numsubpods='1'>
          <subpod title=''>
           <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), 3)
        self.assertIn('x=-1', results[0]['answer'])
        self.assertIn('x=0', results[1]['answer'])
        self.assertIn('solve x^2+x0 - Wolfram|Alpha'.decode('utf-8'),
                      results[2]['title'])
        self.assertEquals(
            'http://www.wolframalpha.com/input/?i=solve+x%5E2%2Bx%EF%9F%990',
            results[2]['url'])
Exemple #5
0
    def test_response(self):
        self.assertRaises(AttributeError, wolframalpha_api.response, None)
        self.assertRaises(AttributeError, wolframalpha_api.response, [])
        self.assertRaises(AttributeError, wolframalpha_api.response, "")
        self.assertRaises(AttributeError, wolframalpha_api.response, "[]")

        referer_url = "referer_url"
        request = Request(headers={"Referer": referer_url})

        # test failure
        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='false' error='false' />
        """
        response = mock.Mock(content=xml)
        self.assertEqual(wolframalpha_api.response(response), [])

        # test basic case
        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='3'
            datatypes='Math'
            id='queryresult_id'
            host='http://www4c.wolframalpha.com'
            related='related_url'
            version='2.6'>
            <pod title='Input'
                 scanner='Identity'
                 id='Input'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='input_img_src.gif'
                           alt='input_img_alt'
                           title='input_img_title' />
                       <plaintext>input_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Result'
                 scanner='Simplification'
                 id='Result'
                 numsubpods='1'
                 primary='true'>
                  <subpod title=''>
                       <img src='result_img_src.gif'
                           alt='result_img_alt'
                           title='result_img_title' />
                       <plaintext>result_plaintext</plaintext>
                  </subpod>
             </pod>
             <pod title='Manipulatives illustration'
                 scanner='Arithmetic'
                 id='Illustration'
                 numsubpods='1'>
                  <subpod title=''>
                       <img src='illustration_img_src.gif'
                           alt='illustration_img_alt' />
                       <plaintext>illustration_plaintext</plaintext>
                  </subpod>
             </pod>
        </queryresult>
        """
        response = mock.Mock(content=xml, request=request)
        results = wolframalpha_api.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", results[1]["title"])

        # test calc
        xml = """<?xml version='1.0' encoding='UTF-8'?>
        <queryresult success='true'
            error='false'
            numpods='2'
            datatypes=''
            parsetimedout='false'
            id='queryresult_id'
            host='http://www5b.wolframalpha.com'
            related='related_url'
            version='2.6' >
            <pod title='Indefinite integral'
                scanner='Integral'
                id='IndefiniteIntegral'
                error='false'
                numsubpods='1'
                primary='true'>
                <subpod title=''>
                    <img src='integral_image.gif'
                        alt='integral_img_alt'
                        title='integral_img_title' />
                    <plaintext>integral_plaintext</plaintext>
                </subpod>
            </pod>
            <pod title='Plot of the integral'
                scanner='Integral'
                id='Plot'
                error='false'
                numsubpods='1'>
                <subpod title=''>
                    <img src='plot.gif'
                        alt='plot_alt'
                        title='' />
                    <plaintext></plaintext>
                </subpod>
            </pod>
        </queryresult>
        """
        response = mock.Mock(content=xml, request=request)
        results = wolframalpha_api.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", results[1]["title"])