forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_scraping.py
More file actions
335 lines (276 loc) · 11.2 KB
/
test_async_scraping.py
File metadata and controls
335 lines (276 loc) · 11.2 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env python3
"""
Tests for async scraping functionality
Tests the async/await implementation for parallel web scraping
"""
import asyncio
import os
import tempfile
import unittest
from unittest.mock import AsyncMock, patch
from skill_seekers.cli.doc_scraper import DocToSkillConverter
class TestAsyncConfiguration(unittest.TestCase):
"""Test async mode configuration and initialization"""
def setUp(self):
"""Save original working directory"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Restore original working directory"""
os.chdir(self.original_cwd)
def test_async_mode_default_false(self):
"""Test async mode is disabled by default"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 10,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.async_mode)
finally:
os.chdir(self.original_cwd)
def test_async_mode_enabled_from_config(self):
"""Test async mode can be enabled via config"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"max_pages": 10,
"async_mode": True,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.async_mode)
finally:
os.chdir(self.original_cwd)
def test_async_mode_with_workers(self):
"""Test async mode works with multiple workers"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"workers": 4,
"async_mode": True,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.async_mode)
self.assertEqual(converter.workers, 4)
finally:
os.chdir(self.original_cwd)
class TestAsyncScrapeMethods(unittest.TestCase):
"""Test async scraping methods exist and have correct signatures"""
def setUp(self):
"""Set up test fixtures"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Clean up"""
os.chdir(self.original_cwd)
def test_scrape_page_async_exists(self):
"""Test scrape_page_async method exists"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(hasattr(converter, "scrape_page_async"))
self.assertTrue(asyncio.iscoroutinefunction(converter.scrape_page_async))
finally:
os.chdir(self.original_cwd)
def test_scrape_all_async_exists(self):
"""Test scrape_all_async method exists"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(hasattr(converter, "scrape_all_async"))
self.assertTrue(asyncio.iscoroutinefunction(converter.scrape_all_async))
finally:
os.chdir(self.original_cwd)
class TestAsyncRouting(unittest.TestCase):
"""Test that scrape_all() correctly routes to async version"""
def setUp(self):
"""Set up test fixtures"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Clean up"""
os.chdir(self.original_cwd)
def test_scrape_all_routes_to_async_when_enabled(self):
"""Test scrape_all calls async version when async_mode=True"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"max_pages": 1,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
# Mock scrape_all_async to verify it gets called
with patch.object(
converter, "scrape_all_async", new_callable=AsyncMock
) as mock_async:
converter.scrape_all()
# Verify async version was called
mock_async.assert_called_once()
finally:
os.chdir(self.original_cwd)
def test_scrape_all_uses_sync_when_async_disabled(self):
"""Test scrape_all uses sync version when async_mode=False"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": False,
"max_pages": 1,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
# Mock scrape_all_async to verify it does NOT get called
with (
patch.object(
converter, "scrape_all_async", new_callable=AsyncMock
) as mock_async,
patch.object(converter, "_try_llms_txt", return_value=False),
):
converter.scrape_all()
# Verify async version was NOT called
mock_async.assert_not_called()
finally:
os.chdir(self.original_cwd)
class TestAsyncDryRun(unittest.TestCase):
"""Test async scraping in dry-run mode"""
def setUp(self):
"""Set up test fixtures"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Clean up"""
os.chdir(self.original_cwd)
def test_async_dry_run_completes(self):
"""Test async dry run completes without errors"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"max_pages": 5,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
# Mock _try_llms_txt to skip llms.txt detection
with patch.object(converter, "_try_llms_txt", return_value=False):
# Should complete without errors
converter.scrape_all()
# Verify dry run mode was used
self.assertTrue(converter.dry_run)
finally:
os.chdir(self.original_cwd)
class TestAsyncErrorHandling(unittest.TestCase):
"""Test error handling in async scraping"""
def setUp(self):
"""Set up test fixtures"""
self.original_cwd = os.getcwd()
def tearDown(self):
"""Clean up"""
os.chdir(self.original_cwd)
def test_async_handles_http_errors(self):
"""Test async scraping handles HTTP errors gracefully"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"workers": 2,
"max_pages": 1,
}
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
# Mock httpx to simulate errors
import httpx
async def run_test():
semaphore = asyncio.Semaphore(2)
async with httpx.AsyncClient() as client:
# Mock client.get to raise exception
with patch.object(client, "get", side_effect=httpx.HTTPError("Test error")):
# Should not raise exception, just log error
await converter.scrape_page_async(
"https://example.com/test", semaphore, client
)
# Run async test
asyncio.run(run_test())
# If we got here without exception, test passed
finally:
os.chdir(self.original_cwd)
class TestAsyncPerformance(unittest.TestCase):
"""Test async performance characteristics"""
def test_async_uses_semaphore_for_concurrency_control(self):
"""Test async mode uses semaphore instead of threading lock"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"workers": 4,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
# Async mode should NOT create threading lock
# (async uses asyncio.Semaphore instead)
self.assertTrue(converter.async_mode)
finally:
os.chdir(original_cwd)
class TestAsyncLlmsTxtIntegration(unittest.TestCase):
"""Test async mode with llms.txt detection"""
def test_async_respects_llms_txt(self):
"""Test async mode respects llms.txt and skips HTML scraping"""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
# Mock _try_llms_txt to return True (llms.txt found)
with (
patch.object(converter, "_try_llms_txt", return_value=True),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
# If llms.txt succeeded, async scraping should be skipped
# Verify by checking that pages were not scraped
self.assertEqual(len(converter.visited_urls), 0)
finally:
os.chdir(original_cwd)
if __name__ == "__main__":
unittest.main()