@@ -0,0 +1,143 @@
1+# -*- coding: utf-8 -*-
2+from collections import defaultdict
3+from datetime import datetime
4+import mock
5+from searx.engines import yahoo_news
6+from searx.testing import SearxTestCase
7+
8+
9+class TestYahooNewsEngine(SearxTestCase):
10+
11+ def test_request(self):
12+ query = 'test_query'
13+ dicto = defaultdict(dict)
14+ dicto['pageno'] = 1
15+ dicto['language'] = 'fr_FR'
16+ params = yahoo_news.request(query, dicto)
17+ self.assertIn('url', params)
18+ self.assertIn(query, params['url'])
19+ self.assertIn('news.search.yahoo.com', params['url'])
20+ self.assertIn('fr', params['url'])
21+ self.assertIn('cookies', params)
22+ self.assertIn('sB', params['cookies'])
23+ self.assertIn('fr', params['cookies']['sB'])
24+
25+ dicto['language'] = 'all'
26+ params = yahoo_news.request(query, dicto)
27+ self.assertIn('cookies', params)
28+ self.assertIn('sB', params['cookies'])
29+ self.assertIn('en', params['cookies']['sB'])
30+ self.assertIn('en', params['url'])
31+
32+ def test_response(self):
33+ self.assertRaises(AttributeError, yahoo_news.response, None)
34+ self.assertRaises(AttributeError, yahoo_news.response, [])
35+ self.assertRaises(AttributeError, yahoo_news.response, '')
36+ self.assertRaises(AttributeError, yahoo_news.response, '[]')
37+
38+ response = mock.Mock(text='<html></html>')
39+ self.assertEqual(yahoo_news.response(response), [])
40+
41+ html = """
42+ <div class="res">
43+ <div>
44+ <h3>
45+ <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
46+ This is
47+ the <b>title</b>...
48+ </a>
49+ </h3>
50+ </div>
51+ <span class="url">Business via Yahoo! Finance</span> <span class="timestamp">Feb 03 09:45am</span>
52+ <div class="abstr">
53+ This is the content
54+ </div>
55+ </div>
56+ """
57+ response = mock.Mock(text=html)
58+ results = yahoo_news.response(response)
59+ self.assertEqual(type(results), list)
60+ self.assertEqual(len(results), 1)
61+ self.assertEqual(results[0]['title'], 'This is the title...')
62+ self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
63+ self.assertEqual(results[0]['content'], 'This is the content')
64+
65+ html = """
66+ <div class="res">
67+ <div>
68+ <h3>
69+ <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
70+ This is
71+ the <b>title</b>...
72+ </a>
73+ </h3>
74+ </div>
75+ <span class="url">Business via Yahoo!</span> <span class="timestamp">2 hours, 22 minutes ago</span>
76+ <div class="abstr">
77+ This is the content
78+ </div>
79+ </div>
80+ <div class="res">
81+ <div>
82+ <h3>
83+ <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
84+ This is
85+ the <b>title</b>...
86+ </a>
87+ </h3>
88+ </div>
89+ <span class="url">Business via Yahoo!</span> <span class="timestamp">22 minutes ago</span>
90+ <div class="abstr">
91+ This is the content
92+ </div>
93+ </div>
94+ <div class="res">
95+ <div>
96+ <h3>
97+ <a class="yschttl spt" href="http://this.is.the.url" target="_blank">
98+ This is
99+ the <b>title</b>...
100+ </a>
101+ </h3>
102+ </div>
103+ <span class="url">Business via Yahoo!</span> <span class="timestamp">Feb 03 09:45am 1900</span>
104+ <div class="abstr">
105+ This is the content
106+ </div>
107+ </div>
108+ """
109+ response = mock.Mock(text=html)
110+ results = yahoo_news.response(response)
111+ self.assertEqual(type(results), list)
112+ self.assertEqual(len(results), 3)
113+ self.assertEqual(results[0]['title'], 'This is the title...')
114+ self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
115+ self.assertEqual(results[0]['content'], 'This is the content')
116+ self.assertEqual(results[2]['publishedDate'].year, datetime.now().year)
117+
118+ html = """
119+ <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
120+ <div Class="sa_mc">
121+ <div class="sb_tlst">
122+ <h2>
123+ <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
124+ <strong>This</strong> should be the title</a>
125+ </h2>
126+ </div>
127+ <div class="sb_meta">
128+ <cite>
129+ <strong>this</strong>.meta.com</cite>
130+ <span class="c_tlbxTrg">
131+ <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
132+ </span>
133+ </span>
134+ </div>
135+ <p>
136+ <strong>This</strong> should be the content.</p>
137+ </div>
138+ </li>
139+ """
140+ response = mock.Mock(text=html)
141+ results = yahoo_news.response(response)
142+ self.assertEqual(type(results), list)
143+ self.assertEqual(len(results), 0)