def test_response(self): self.assertRaises(AttributeError, google_news.response, None) self.assertRaises(AttributeError, google_news.response, []) self.assertRaises(AttributeError, google_news.response, '') self.assertRaises(AttributeError, google_news.response, '[]') response = mock.Mock(text='{}') self.assertEqual(google_news.response(response), []) response = mock.Mock(text='{"data": []}') self.assertEqual(google_news.response(response), []) html = u""" <div class="g"> <div class="ts _V6c _Zmc _XO _knc _d7c"><a class="top _vQb _mnc" href="http://this.is.the.url" onmousedown="return rwt(this,'','','','5','AFQjCNGixEtJGC3qTB9pYFLXlRj8XXwdiA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQvIgBCCwwBA','','',event)"><img class="th _lub" id="news-thumbnail-image-52779299683347" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGQAZAMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAFBgADBAIBB//EADsQAAIBAwIEAwUGBAUFAAAAAAECAwAEEQUhBhIxURNBYRQiMnGBFSORobHBB1Ji4UKS0fDxFiQzNYL/xAAYAQEBAQEBAAAAAAAAAAAAAAACAwEEAP/EACERAAMBAAICAgMBAAAAAAAAAAABAhEhMQMSE2EEQVEi/9oADAMBAAIRAxEAPwDSkKr8JFWIzwnMTlSepFdiBgMiuxEdid6B7QbrEr8qE4L4wGNCIT7u/WifE0eIosHBz0oWnugCjQ4BfEipJHbpK/JG0gDP/KO9XT6Lw7BGDHrofIBIJQ4NXalAs8IDJnFLepS21pmOOFXl679FoY66ZTcNwWxtGzaaqwOf8JFcXupSSr4cupSOp6gnGaDQahJESQwU/wBIxXbX0l0jl+VgduY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7UoPsS6G7+HX3Wm6jdLvM0ioWPYLn9SaQFka91NHuTzme4Bkz58zb/AK17Uq1dImg5rcrXPGcUU2GSIxqq+QGA36ml+bMkaXEjM0kw8R89yd6lSpiCfCcEV7fXdpcxq8TWUjjujAggr2NXcJ3k08F9pcpDWslpJNyEfA6gEFe29SpQZqAgbmAY9TvXAY5YZOCRUqV79ifRthucRhXgglxsGkTmIHbNSpUrAn//2Q==" alt="A(z) south témájának képe a következőből: CBC.ca" data-deferred="1" onload="google.aft&&google.aft(this)"></a><div class="_cnc"><h3 class="r _U6c"><a class="l _HId" href="http://this.is.the.url" onmousedown="return rwt(this,'','','','5','AFQjCNGixEtJGC3qTB9pYFLXlRj8XXwdiA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQqQIILSgAMAQ','','',event)">Meet Thuli Madonsela — <em>South</em> Africa's conscience</a></h3><div class="slp"><span class="_tQb _IId">CBC.ca</span><span class="_v5">-</span><span class="f nsa _uQb">9 órával ezelőtt</span></div><div class="st"><em>South</em> African Public Protector</div></div><div class="_Xmc card-section"><a class="_sQb" href="http://www.news24.com/Columnists/Mpumelelo_Mkhabela/who-really-governs-south-africa-20161209" onmousedown="return rwt(this,'','','','5','AFQjCNHhc2MnYSZ5T4COqInzvgoju5k5bA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQuogBCC4oATAE','','',event)">Who really governs <em>South</em> Africa?</a><br><span class="_Wmc _GId">Vélemény</span><span class="_v5">-</span><span class="_tQb _IId">News24</span><span class="_v5">-</span><span class="f nsa _uQb">2016. dec. 8.</span></div><div class="_Vmc"></div></div> </div> """ # noqa response = mock.Mock(text=html) results = google_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 1) self.assertEqual( results[0]['title'], u'Meet Thuli Madonsela \u2014 South Africa\'s conscience') self.assertEqual(results[0]['url'], 'http://this.is.the.url') self.assertEqual(results[0]['content'], 'South African Public Protector')
def test_response(self): self.assertRaises(AttributeError, google_news.response, None) self.assertRaises(AttributeError, google_news.response, []) self.assertRaises(AttributeError, google_news.response, '') self.assertRaises(AttributeError, google_news.response, '[]') response = mock.Mock(text='{}') self.assertEqual(google_news.response(response), []) response = mock.Mock(text='{"data": []}') self.assertEqual(google_news.response(response), []) html = u""" <div class="g"> <div class="ts _V6c _Zmc _XO _knc _d7c"><a class="top _vQb _mnc" href="http://this.is.the.url" onmousedown="return rwt(this,'','','','5','AFQjCNGixEtJGC3qTB9pYFLXlRj8XXwdiA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQvIgBCCwwBA','','',event)"><img class="th _lub" id="news-thumbnail-image-52779299683347" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGQAZAMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAFBgADBAIBB//EADsQAAIBAwIEAwUGBAUFAAAAAAECAwAEEQUhBhIxURNBYRQiMnGBFSORobHBB1Ji4UKS0fDxFiQzNYL/xAAYAQEBAQEBAAAAAAAAAAAAAAACAwEEAP/EACERAAMBAAICAgMBAAAAAAAAAAABAhEhMQMSE2EEQVEi/9oADAMBAAIRAxEAPwDSkKr8JFWIzwnMTlSepFdiBgMiuxEdid6B7QbrEr8qE4L4wGNCIT7u/WifE0eIosHBz0oWnugCjQ4BfEipJHbpK/JG0gDP/KO9XT6Lw7BGDHrofIBIJQ4NXalAs8IDJnFLepS21pmOOFXl679FoY66ZTcNwWxtGzaaqwOf8JFcXupSSr4cupSOp6gnGaDQahJESQwU/wBIxXbX0l0jl+VgduYjJrfj/ofc9MluueV1z3xV2jlX1AlDkY61TY3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QFka91NHuTzme4Bkz58zb/AK17Uq1dImg5rcrXPGcUU2GSIxqq+QGA36ml+bMkaXEjM0kw8R89yd6lSpiCfCcEV7fXdpcxq8TWUjjujAggr2NXcJ3k08F9pcpDWslpJNyEfA6gEFe29SpQZqAgbmAY9TvXAY5YZOCRUqV79ifRthucRhXgglxsGkTmIHbNSpUrAn//2Q==" alt="A(z) south témájának képe a következőből: CBC.ca" data-deferred="1" onload="google.aft&&google.aft(this)"></a><div class="_cnc"><h3 class="r _U6c"><a class="l _HId" href="http://this.is.the.url" onmousedown="return rwt(this,'','','','5','AFQjCNGixEtJGC3qTB9pYFLXlRj8XXwdiA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQqQIILSgAMAQ','','',event)">Meet Thuli Madonsela — <em>South</em> Africa's conscience</a></h3><div class="slp"><span class="_tQb _IId">CBC.ca</span><span class="_v5">-</span><span class="f nsa _uQb">9 órával ezelőtt</span></div><div class="st"><em>South</em> African Public Protector</div></div><div class="_Xmc card-section"><a class="_sQb" href="http://www.news24.com/Columnists/Mpumelelo_Mkhabela/who-really-governs-south-africa-20161209" onmousedown="return rwt(this,'','','','5','AFQjCNHhc2MnYSZ5T4COqInzvgoju5k5bA','','0ahUKEwiG7O_M5-rQAhWDtRoKHd0RD5QQuogBCC4oATAE','','',event)">Who really governs <em>South</em> Africa?</a><br><span class="_Wmc _GId">Vélemény</span><span class="_v5">-</span><span class="_tQb _IId">News24</span><span class="_v5">-</span><span class="f nsa _uQb">2016. dec. 8.</span></div><div class="_Vmc"></div></div> </div> """ # noqa response = mock.Mock(text=html) results = google_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 1) self.assertEqual(results[0]['title'], u'Meet Thuli Madonsela \u2014 South Africa\'s conscience') self.assertEqual(results[0]['url'], 'http://this.is.the.url') self.assertEqual(results[0]['content'], 'South African Public Protector')
def test_response(self): self.assertRaises(AttributeError, google_news.response, None) self.assertRaises(AttributeError, google_news.response, []) self.assertRaises(AttributeError, google_news.response, '') self.assertRaises(AttributeError, google_news.response, '[]') response = mock.Mock(text='{}') self.assertEqual(google_news.response(response), []) response = mock.Mock(text='{"data": []}') self.assertEqual(google_news.response(response), []) html = u""" <h2 class="hd">Search Results</h2> <div data-async-context="query:searx" id="ires"> <div eid="oC2oWcGXCafR6ASkwoCwDA" id="rso"> <div class="_NId"> <!--m--> <div class="g _cy"> <div class="ts _JGs _JHs _tJs _KGs _jHs"> <div class="_hJs"> <h3 class="r _gJs"> <a class="l _PMs" href="https://example.com/" onmousedown="return rwt(this,'','','','11','AFQjCNEyehpzD5cJK1KUfXBx9RmsbqqG9g','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAggiKAAwAA','','',event)">Example title</a> </h3> <div class="slp"> <span class="_OHs _PHs"> Mac & i</span> <span class="_QGs"> -</span> <span class="f nsa _QHs"> Mar 21, 2016</span> </div> <div class="st">Example description</div> </div> </div> </div> <div class="g _cy"> <div class="ts _JGs _JHs _oGs _KGs _jHs"> <a class="top _xGs _SHs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChC8iAEIJDAB','','',event)"> <img class="th _RGs" src="https://example2.com/image.jpg" alt="Story image for searx from Golem.de" onload="typeof google==='object'&&google.aft&&google.aft(this)"> </a> <div class="_hJs"> <h3 class="r _gJs"> <a class="l _PMs" href="https://example2.com/" onmousedown="return rwt(this,'','','','12','AFQjCNHObfH7sYmLWI1SC-YhWXKZFRzRjw','','0ahUKEwjB58OR54HWAhWnKJoKHSQhAMY4ChCpAgglKAAwAQ','','',event)">Example title 2</a> </h3> <div class="slp"> <span class="_OHs _PHs"> Golem.de</span> <span class="_QGs"> -</span> <span class="f nsa _QHs"> Oct 4, 2016</span> </div> <div class="st">Example description 2</div> </div> </div> </div> </div> </div> </div> """ # noqa response = mock.Mock(text=html) results = google_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) self.assertEqual(results[0]['title'], u'Example title') self.assertEqual(results[0]['url'], 'https://example.com/') self.assertEqual(results[0]['content'], 'Example description') self.assertEqual(results[1]['title'], u'Example title 2') self.assertEqual(results[1]['url'], 'https://example2.com/') self.assertEqual(results[1]['content'], 'Example description 2') self.assertEqual(results[1]['img_src'], 'https://example2.com/image.jpg')
def test_response(self): self.assertRaises(AttributeError, google_news.response, None) self.assertRaises(AttributeError, google_news.response, []) self.assertRaises(AttributeError, google_news.response, '') self.assertRaises(AttributeError, google_news.response, '[]') response = mock.Mock(text='{}') self.assertEqual(google_news.response(response), []) response = mock.Mock(text='{"data": []}') self.assertEqual(google_news.response(response), []) json = """ { "responseData": { "results": [ { "GsearchResultClass": "GnewsSearch", "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr", "content": "This is the content", "unescapedUrl": "http://this.is.the.url", "url": "http://this.is.the.url", "title": "This is the title", "titleNoFormatting": "This is the title", "location": "", "publisher": "Jeux Actu", "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800", "signedRedirectUrl": "http://news.google.com/", "language": "fr", "image": { "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg", "tbUrl": "http://t1.gstatic.com/images?q=tbn:ANd9GcSF4yYrs9Ycw23DGiOSAZ-5SEPXYwG3LNs", "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm", "publisher": "Jeux Actu", "tbWidth": 80, "tbHeight": 30 }, "relatedStories": [ { "unescapedUrl": "http://www.jeuxvideo.com/test/415823/dying-light.htm", "url": "http%3A%2F%2Fwww.jeuxvideo.com%2Ftest%2F415823%2Fdying-light.htm", "title": "<b>Test</b> du jeu Dying Light - jeuxvideo.com", "titleNoFormatting": "Test du jeu Dying Light - jeuxvideo.com", "location": "", "publisher": "JeuxVideo.com", "publishedDate": "Fri, 30 Jan 2015 08:52:30 -0800", "signedRedirectUrl": "http://news.google.com/news/url?sa=T&", "language": "fr" } ] } ] }, "responseDetails": null, "responseStatus": 200 } """ response = mock.Mock(text=json) results = google_news.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'], 'http://this.is.the.url') self.assertEqual(results[0]['content'], 'This is the content') json = """ { "responseData": { "results": [ { "GsearchResultClass": "GnewsSearch", "clusterUrl": "http://news.google.com/news/story?ncl=d2d3t1LMDpNIj2MPPhdTT0ycN4sWM&hl=fr&ned=fr", "content": "This is the content", "unescapedUrl": "http://this.is.the.url", "title": "This is the title", "titleNoFormatting": "This is the title", "location": "", "publisher": "Jeux Actu", "publishedDate": "Fri, 30 Jan 2015 11:00:25 -0800", "signedRedirectUrl": "http://news.google.com/news/", "language": "fr", "image": { "url": "http://i.jeuxactus.com/datas/jeux/d/y/dying-light/vu/dying-light-54cc080b568fb.jpg", "tbUrl": "http://t1.gstatic.com/images?q=tbn:b_6f-OSAZ-5SEPXYwG3LNs", "originalContextUrl": "http://www.jeuxactu.com/test-dying-light-sur-ps4-97208.htm", "publisher": "Jeux Actu", "tbWidth": 80, "tbHeight": 30 } } ] }, "responseDetails": null, "responseStatus": 200 } """ response = mock.Mock(text=json) results = google_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0) json = """ { "responseData": {}, "responseDetails": null, "responseStatus": 200 } """ response = mock.Mock(text=json) results = google_news.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 0)