From ff2ad57a8781c72886848abf013ec84778807c97 Mon Sep 17 00:00:00 2001 From: Cqoicebordel Date: Wed, 4 Feb 2015 20:07:26 +0100 Subject: [PATCH] Yahoo News' unit test --- searx/tests/engines/test_yahoo_news.py | 143 +++++++++++++++++++++++++ searx/tests/test_engines.py | 1 + 2 files changed, 144 insertions(+) create mode 100644 searx/tests/engines/test_yahoo_news.py diff --git a/searx/tests/engines/test_yahoo_news.py b/searx/tests/engines/test_yahoo_news.py new file mode 100644 index 000000000..797dc11b7 --- /dev/null +++ b/searx/tests/engines/test_yahoo_news.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +from collections import defaultdict +from datetime import datetime +import mock +from searx.engines import yahoo_news +from searx.testing import SearxTestCase + + +class TestYahooNewsEngine(SearxTestCase): + + def test_request(self): + query = 'test_query' + dicto = defaultdict(dict) + dicto['pageno'] = 1 + dicto['language'] = 'fr_FR' + params = yahoo_news.request(query, dicto) + self.assertIn('url', params) + self.assertIn(query, params['url']) + self.assertIn('news.search.yahoo.com', params['url']) + self.assertIn('fr', params['url']) + self.assertIn('cookies', params) + self.assertIn('sB', params['cookies']) + self.assertIn('fr', params['cookies']['sB']) + + dicto['language'] = 'all' + params = yahoo_news.request(query, dicto) + self.assertIn('cookies', params) + self.assertIn('sB', params['cookies']) + self.assertIn('en', params['cookies']['sB']) + self.assertIn('en', params['url']) + + def test_response(self): + self.assertRaises(AttributeError, yahoo_news.response, None) + self.assertRaises(AttributeError, yahoo_news.response, []) + self.assertRaises(AttributeError, yahoo_news.response, '') + self.assertRaises(AttributeError, yahoo_news.response, '[]') + + response = mock.Mock(text='') + self.assertEqual(yahoo_news.response(response), []) + + html = """ +
+
+

+ + This is + the title... + +

+
+ Business via Yahoo! Finance   Feb 03 09:45am +
+ This is the content +
+
+ """ + response = mock.Mock(text=html) + results = yahoo_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') + + html = """ +
+
+

+ + This is + the title... + +

+
+ Business via Yahoo!   2 hours, 22 minutes ago +
+ This is the content +
+
+
+
+

+ + This is + the title... + +

+
+ Business via Yahoo!   22 minutes ago +
+ This is the content +
+
+
+
+

+ + This is + the title... + +

+
+ Business via Yahoo!   Feb 03 09:45am 1900 +
+ This is the content +
+
+ """ + response = mock.Mock(text=html) + results = yahoo_news.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 3) + 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') + self.assertEqual(results[2]['publishedDate'].year, datetime.now().year) + + html = """ +
  • +
    + +
    + + this.meta.com + + + + +
    +

    + This should be the content.

    +
    +
  • + """ + response = mock.Mock(text=html) + results = yahoo_news.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 0) diff --git a/searx/tests/test_engines.py b/searx/tests/test_engines.py index ccef28908..65f182e8f 100644 --- a/searx/tests/test_engines.py +++ b/searx/tests/test_engines.py @@ -27,3 +27,4 @@ from searx.tests.engines.test_twitter import * # noqa from searx.tests.engines.test_vimeo import * # noqa from searx.tests.engines.test_www500px import * # noqa from searx.tests.engines.test_youtube import * # noqa +from searx.tests.engines.test_yahoo_news import * # noqa