Esempio n. 1
0
    def test_response(self):
        self.assertRaises(AttributeError, yahoo.response, None)
        self.assertRaises(AttributeError, yahoo.response, [])
        self.assertRaises(AttributeError, yahoo.response, '')
        self.assertRaises(AttributeError, yahoo.response, '[]')

        response = mock.Mock(text='<html></html>')
        self.assertEqual(yahoo.response(response), [])

        html = """
<ol class="reg mb-15 searchCenterMiddle">
    <li class="first">
        <div class="dd algo fst Sr">
            <div class="compTitle">
                <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
                     _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
                     /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
                     target="_blank" data-bid="54e712e13671c">
                     <b><b>This is the title</b></b></a>
                </h3>
            </div>
            <div class="compText aAbs">
                <p class="lh-18"><b><b>This is the </b>content</b>
                </p>
            </div>
        </div>
    </li>
    <li>
        <div class="dd algo lst Sr">
            <div class="compTitle">
            </div>
            <div class="compText aAbs">
                <p class="lh-18">This is the second content</p>
            </div>
        </div>
    </li>
</ol>
<div class="dd assist fst lst AlsoTry" data-bid="54e712e138d04">
    <div class="compTitle mb-4 h-17">
        <h3 class="title">Also Try</h3> </div>
    <table class="compTable m-0 ac-1st td-u fz-ms">
        <tbody>
            <tr>
                <td class="w-50p pr-28"><a href="https://search.yahoo.com/"><B>This is the </B>suggestion<B></B></a>
                </td>
            </tr>
    </table>
</div>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
        self.assertEqual(results[0]['content'], 'This is the content')
        self.assertEqual(results[1]['suggestion'], 'This is the suggestion')

        html = """
<ol class="reg mb-15 searchCenterMiddle">
    <li class="first">
        <div class="dd algo fst Sr">
            <div class="compTitle">
                <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
                     _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
                     /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
                     target="_blank" data-bid="54e712e13671c">
                  <b><b>This is the title</b></b></a>
                </h3>
            </div>
            <div class="compText aAbs">
                <p class="lh-18"><b><b>This is the </b>content</b>
                </p>
            </div>
        </div>
    </li>
</ol>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
        self.assertEqual(results[0]['content'], 'This is the content')

        html = """
        <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
        </li>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)
Esempio n. 2
0
    def test_response(self):
        self.assertRaises(AttributeError, yahoo.response, None)
        self.assertRaises(AttributeError, yahoo.response, [])
        self.assertRaises(AttributeError, yahoo.response, '')
        self.assertRaises(AttributeError, yahoo.response, '[]')

        response = mock.Mock(text='<html></html>')
        self.assertEqual(yahoo.response(response), [])

        html = """
<ol class="reg mb-15 searchCenterMiddle">
    <li class="first">
        <div class="dd algo fst Sr">
            <div class="compTitle">
                <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
                     _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
                     /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
                     target="_blank" data-bid="54e712e13671c">
                     <b><b>This is the title</b></b></a>
                </h3>
            </div>
            <div class="compText aAbs">
                <p class="lh-18"><b><b>This is the </b>content</b>
                </p>
            </div>
        </div>
    </li>
    <li>
        <div class="dd algo lst Sr">
            <div class="compTitle">
            </div>
            <div class="compText aAbs">
                <p class="lh-18">This is the second content</p>
            </div>
        </div>
    </li>
</ol>
<div class="dd assist fst lst AlsoTry" data-bid="54e712e138d04">
    <div class="compTitle mb-4 h-17">
        <h3 class="title">Also Try</h3> </div>
    <table class="compTable m-0 ac-1st td-u fz-ms">
        <tbody>
            <tr>
                <td class="w-50p pr-28"><a href="https://search.yahoo.com/"><B>This is the </B>suggestion<B></B></a>
                </td>
            </tr>
    </table>
</div>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
        self.assertEqual(results[0]['content'], 'This is the content')
        self.assertEqual(results[1]['suggestion'], 'This is the suggestion')

        html = """
<ol class="reg mb-15 searchCenterMiddle">
    <li class="first">
        <div class="dd algo fst Sr">
            <div class="compTitle">
                <h3 class="title"><a class=" td-u" href="http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;
                     _ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10
                     /RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=dtcJsfP4mEeBOjnVfUQ-"
                     target="_blank" data-bid="54e712e13671c">
                  <b><b>This is the title</b></b></a>
                </h3>
            </div>
            <div class="compText aAbs">
                <p class="lh-18"><b><b>This is the </b>content</b>
                </p>
            </div>
        </div>
    </li>
</ol>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://this.is.the.url/')
        self.assertEqual(results[0]['content'], 'This is the content')

        html = """
        <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
        </li>
        """
        response = mock.Mock(text=html)
        results = yahoo.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)