Skip to content

Commit b626b4d

Browse files
committed
test: adding new tests for the configure function
1 parent 35032d6 commit b626b4d

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

tests/fetchers/async/test_requests.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
AsyncFetcher.adaptive = True
77

88

9+
@pytest.fixture
10+
def _reset_async_fetcher_config():
11+
"""Snapshot and restore the mutable class-level parser config around a test."""
12+
snapshot = {k: getattr(AsyncFetcher, k) for k in AsyncFetcher.parser_keywords}
13+
try:
14+
yield
15+
finally:
16+
for k, v in snapshot.items():
17+
setattr(AsyncFetcher, k, v)
18+
19+
920
@pytest_httpbin.use_class_based_httpbin
1021
@pytest.mark.asyncio
1122
class TestAsyncFetcher:
@@ -124,3 +135,28 @@ async def test_delete_properties(self, fetcher, urls):
124135
timeout=None,
125136
)
126137
).status == 200
138+
139+
async def test_configure_propagates_to_response(
140+
self, fetcher, urls, _reset_async_fetcher_config
141+
):
142+
"""`AsyncFetcher.configure()` must reach the Response's Selector on the HTTP path."""
143+
AsyncFetcher.configure(adaptive=False, adaptive_domain="")
144+
baseline = await fetcher.get(urls["html_url"])
145+
assert baseline._storage is None
146+
147+
AsyncFetcher.configure(adaptive=True, adaptive_domain="configured.test")
148+
configured = await fetcher.get(urls["html_url"])
149+
assert configured._storage is not None
150+
assert configured.url == "configured.test"
151+
152+
async def test_selector_config_overrides_configure(
153+
self, fetcher, urls, _reset_async_fetcher_config
154+
):
155+
"""A per-request ``selector_config`` overrides the class-level configure()."""
156+
AsyncFetcher.configure(adaptive=True, adaptive_domain="from-configure.test")
157+
response = await fetcher.get(
158+
urls["html_url"],
159+
selector_config={"adaptive_domain": "from-request.test"},
160+
)
161+
assert response._storage is not None
162+
assert response.url == "from-request.test"

tests/fetchers/sync/test_requests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
Fetcher.adaptive = True
77

88

9+
@pytest.fixture
10+
def _reset_fetcher_config():
11+
"""Snapshot and restore the mutable class-level parser config around a test."""
12+
snapshot = {k: getattr(Fetcher, k) for k in Fetcher.parser_keywords}
13+
try:
14+
yield
15+
finally:
16+
for k, v in snapshot.items():
17+
setattr(Fetcher, k, v)
18+
19+
920
@pytest_httpbin.use_class_based_httpbin
1021
class TestFetcher:
1122
@pytest.fixture(scope="class")
@@ -119,3 +130,24 @@ def test_delete_properties(self, fetcher):
119130
).status
120131
== 200
121132
)
133+
134+
def test_configure_propagates_to_response(self, fetcher, _reset_fetcher_config):
135+
"""`Fetcher.configure()` must reach the Response's Selector on the HTTP path."""
136+
Fetcher.configure(adaptive=False, adaptive_domain="")
137+
baseline = fetcher.get(self.html_url)
138+
assert baseline._storage is None
139+
140+
Fetcher.configure(adaptive=True, adaptive_domain="configured.test")
141+
configured = fetcher.get(self.html_url)
142+
assert configured._storage is not None
143+
assert configured.url == "configured.test"
144+
145+
def test_selector_config_overrides_configure(self, fetcher, _reset_fetcher_config):
146+
"""A per-request ``selector_config`` overrides the class-level configure()."""
147+
Fetcher.configure(adaptive=True, adaptive_domain="from-configure.test")
148+
response = fetcher.get(
149+
self.html_url,
150+
selector_config={"adaptive_domain": "from-request.test"},
151+
)
152+
assert response._storage is not None
153+
assert response.url == "from-request.test"

0 commit comments

Comments
 (0)