-
Notifications
You must be signed in to change notification settings - Fork 711
Expand file tree
/
Copy pathtest_context_pipeline.py
More file actions
203 lines (164 loc) · 6.4 KB
/
test_context_pipeline.py
File metadata and controls
203 lines (164 loc) · 6.4 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
from __future__ import annotations
import logging
from dataclasses import dataclass
from typing import TYPE_CHECKING
from unittest.mock import AsyncMock
import pytest
from crawlee import Request
from crawlee._types import BasicCrawlingContext
from crawlee.crawlers import ContextPipeline
from crawlee.errors import ContextPipelineFinalizationError, ContextPipelineInitializationError, RequestHandlerError
from crawlee.sessions._session import Session
if TYPE_CHECKING:
from collections.abc import AsyncGenerator
@dataclass(frozen=True)
class EnhancedCrawlingContext(BasicCrawlingContext):
foo: str
@dataclass(frozen=True)
class MoreEnhancedCrawlingContext(EnhancedCrawlingContext):
bar: int
async def test_calls_consumer_without_middleware() -> None:
consumer = AsyncMock()
pipeline = ContextPipeline()
context = BasicCrawlingContext(
request=Request.from_url(url='https://test.io/'),
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=Session(),
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=lambda _: None,
)
await pipeline(context, consumer)
consumer.assert_called_once_with(context)
async def test_calls_consumers_and_middlewares() -> None:
events = list[str]()
async def consumer(context: MoreEnhancedCrawlingContext) -> None:
events.append('consumer_called')
assert context.bar == 4
async def middleware_a(context: BasicCrawlingContext) -> AsyncGenerator[EnhancedCrawlingContext, None]:
events.append('middleware_a_in')
yield EnhancedCrawlingContext(
request=context.request,
foo='foo',
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=context.session,
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=context.register_deferred_cleanup,
)
events.append('middleware_a_out')
async def middleware_b(context: EnhancedCrawlingContext) -> AsyncGenerator[MoreEnhancedCrawlingContext, None]:
events.append('middleware_b_in')
yield MoreEnhancedCrawlingContext(
request=context.request,
foo=context.foo,
bar=4,
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=context.session,
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=context.register_deferred_cleanup,
)
events.append('middleware_b_out')
pipeline = ContextPipeline[BasicCrawlingContext]().compose(middleware_a).compose(middleware_b)
context = BasicCrawlingContext(
request=Request.from_url(url='https://test.io/'),
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=Session(),
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=lambda _: None,
)
await pipeline(context, consumer)
assert events == [
'middleware_a_in',
'middleware_b_in',
'consumer_called',
'middleware_b_out',
'middleware_a_out',
]
async def test_wraps_consumer_errors() -> None:
consumer = AsyncMock(side_effect=RuntimeError('Arbitrary crash for testing purposes'))
pipeline = ContextPipeline()
context = BasicCrawlingContext(
request=Request.from_url(url='https://test.io/'),
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=Session(),
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=lambda _: None,
)
with pytest.raises(RequestHandlerError):
await pipeline(context, consumer)
async def test_handles_exceptions_in_middleware_initialization() -> None:
consumer = AsyncMock()
cleanup = AsyncMock()
async def step_1(context: BasicCrawlingContext) -> AsyncGenerator[BasicCrawlingContext, None]:
yield context
await cleanup()
async def step_2(context: BasicCrawlingContext) -> AsyncGenerator[BasicCrawlingContext, None]:
raise RuntimeError('Crash during middleware initialization')
yield context
pipeline = ContextPipeline().compose(step_1).compose(step_2)
context = BasicCrawlingContext(
request=Request.from_url(url='https://test.io/'),
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=Session(),
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=lambda _: None,
)
with pytest.raises(ContextPipelineInitializationError):
await pipeline(context, consumer)
assert not consumer.called
assert cleanup.called
async def test_handles_exceptions_in_middleware_finalization() -> None:
consumer = AsyncMock()
cleanup = AsyncMock()
async def step_1(context: BasicCrawlingContext) -> AsyncGenerator[BasicCrawlingContext, None]:
yield context
await cleanup()
async def step_2(context: BasicCrawlingContext) -> AsyncGenerator[BasicCrawlingContext, None]:
yield context
raise RuntimeError('Crash during middleware finalization')
pipeline = ContextPipeline().compose(step_1).compose(step_2)
context = BasicCrawlingContext(
request=Request.from_url(url='https://test.io/'),
send_request=AsyncMock(),
add_requests=AsyncMock(),
session=Session(),
proxy_info=AsyncMock(),
push_data=AsyncMock(),
use_state=AsyncMock(),
get_key_value_store=AsyncMock(),
log=logging.getLogger(),
register_deferred_cleanup=lambda _: None,
)
with pytest.raises(ContextPipelineFinalizationError):
await pipeline(context, consumer)
assert consumer.called
assert not cleanup.called