-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_web_scraper.py
145 lines (124 loc) · 5.41 KB
/
test_web_scraper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import unittest
from unittest.mock import patch, MagicMock, AsyncMock
import asyncio
from tools.web_scraper import (
validate_url,
parse_html,
fetch_page,
process_urls
)
class TestWebScraper(unittest.TestCase):
def test_validate_url(self):
# Test valid URLs
self.assertTrue(validate_url('https://example.com'))
self.assertTrue(validate_url('http://example.com/path?query=1'))
self.assertTrue(validate_url('https://sub.example.com:8080/path'))
# Test invalid URLs
self.assertFalse(validate_url('not-a-url'))
self.assertFalse(validate_url('http://'))
self.assertFalse(validate_url('https://'))
self.assertFalse(validate_url(''))
def test_parse_html(self):
# Test with empty or None input
self.assertEqual(parse_html(None), "")
self.assertEqual(parse_html(""), "")
# Test with simple HTML
html = """
<html>
<body>
<h1>Title</h1>
<p>Paragraph text</p>
<a href="https://example.com">Link text</a>
<script>var x = 1;</script>
<style>.css { color: red; }</style>
</body>
</html>
"""
result = parse_html(html)
self.assertIn("Title", result)
self.assertIn("Paragraph text", result)
self.assertIn("[Link text](https://example.com)", result)
self.assertNotIn("var x = 1", result) # Script content should be filtered
self.assertNotIn(".css", result) # Style content should be filtered
# Test with nested elements
html = """
<html>
<body>
<div>
<p>Level 1</p>
<div>
<p>Level 2</p>
</div>
</div>
</body>
</html>
"""
result = parse_html(html)
self.assertIn("Level 1", result)
self.assertIn("Level 2", result)
# Test with malformed HTML
html = "<p>Unclosed paragraph"
result = parse_html(html)
self.assertIn("Unclosed paragraph", result)
@patch('tools.web_scraper.logger')
async def test_fetch_page(self, mock_logger):
# Create mock context and page
mock_page = AsyncMock()
mock_page.goto = AsyncMock()
mock_page.wait_for_load_state = AsyncMock()
mock_page.content = AsyncMock(return_value="<html><body>Test content</body></html>")
mock_page.close = AsyncMock()
mock_context = AsyncMock()
mock_context.new_page = AsyncMock(return_value=mock_page)
# Test successful fetch
content = await fetch_page("https://example.com", mock_context)
self.assertEqual(content, "<html><body>Test content</body></html>")
mock_logger.info.assert_any_call("Fetching https://example.com")
mock_logger.info.assert_any_call("Successfully fetched https://example.com")
# Test fetch error
mock_page.goto.side_effect = Exception("Network error")
content = await fetch_page("https://example.com", mock_context)
self.assertIsNone(content)
mock_logger.error.assert_called_with("Error fetching https://example.com: Network error")
@patch('tools.web_scraper.async_playwright')
@patch('tools.web_scraper.Pool')
async def test_process_urls(self, mock_pool, mock_playwright):
# Mock playwright setup
mock_browser = AsyncMock()
mock_context = AsyncMock()
mock_page = AsyncMock()
mock_page.goto = AsyncMock()
mock_page.wait_for_load_state = AsyncMock()
mock_page.content = AsyncMock(return_value="<html><body>Test content</body></html>")
mock_page.close = AsyncMock()
mock_context.new_page = AsyncMock(return_value=mock_page)
mock_browser.new_context = AsyncMock(return_value=mock_context)
mock_browser.close = AsyncMock()
mock_playwright_instance = AsyncMock()
mock_playwright_instance.chromium.launch = AsyncMock(return_value=mock_browser)
mock_playwright.return_value.__aenter__.return_value = mock_playwright_instance
# Mock Pool for parallel HTML parsing
mock_pool_instance = MagicMock()
mock_pool_instance.map.return_value = ["Parsed content 1", "Parsed content 2"]
mock_pool.return_value.__enter__.return_value = mock_pool_instance
# Test processing multiple URLs
urls = ["https://example1.com", "https://example2.com"]
results = await process_urls(urls, max_concurrent=2)
# Verify results
self.assertEqual(len(results), 2)
self.assertEqual(results[0], "Parsed content 1")
self.assertEqual(results[1], "Parsed content 2")
# Verify mocks were called correctly
self.assertEqual(mock_browser.new_context.call_count, 2)
mock_pool_instance.map.assert_called_once()
mock_browser.close.assert_awaited_once()
def async_test(coro):
def wrapper(*args, **kwargs):
loop = asyncio.get_event_loop()
return loop.run_until_complete(coro(*args, **kwargs))
return wrapper
# Patch async tests
TestWebScraper.test_fetch_page = async_test(TestWebScraper.test_fetch_page)
TestWebScraper.test_process_urls = async_test(TestWebScraper.test_process_urls)
if __name__ == '__main__':
unittest.main()