-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_news.py
63 lines (49 loc) · 2.32 KB
/
test_news.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import unittest
from unittest.mock import patch, MagicMock
import news
class TestNews(unittest.TestCase):
@patch('news.requests.get')
def test_get_hn_stories(self, mock_get):
# Mock response text
mock_html = """
<html>
<body>
<table>
<tr class="athing"><td class="titleline">
<a href="https://example.com/story1" class="titlelink">Story 1</a>
</td></tr>
<tr class="subtext">
<span class="score">300 points</span>
</tr>
</table>
</body>
</html>
"""
# Prepare the mock response
mock_response = MagicMock()
mock_response.text = mock_html
mock_get.return_value = mock_response
links, subtext = news.get_hn_stories('https://news.ycombinator.com/news')
# Test the links output
self.assertEqual(len(links), 1)
self.assertEqual(links[0].getText(), 'Story 1')
# Test the subtext output
self.assertEqual(len(subtext), 1)
self.assertEqual(subtext[0].select('.score')[0].getText(), '300 points')
def test_create_custom_hn_with_votes(self):
links = [MagicMock(getText=lambda: 'Story 1', get=lambda x: 'https://example.com/story1'),
MagicMock(getText=lambda: 'Story 2', get=lambda x: 'https://example.com/story2')]
subtext = [MagicMock(select=lambda x: [MagicMock(getText=lambda: '300 points')]),
MagicMock(select=lambda x: [MagicMock(getText=lambda: '150 points')])]
hn = news.create_custom_hn(links, subtext)
# Test that only the story with over 200 points is returned
self.assertEqual(len(hn), 1)
self.assertEqual(hn[0], 'Story 1 (300) https://example.com/story1')
def test_create_custom_hn_no_high_points(self):
links = [MagicMock(getText=lambda: 'Story 1', get=lambda x: 'https://example.com/story1')]
subtext = [MagicMock(select=lambda x: [MagicMock(getText=lambda: '150 points')])]
hn = news.create_custom_hn(links, subtext)
# No stories should be returned
self.assertEqual(hn, [])
if __name__ == '__main__':
unittest.main()