Skip to content

Commit c55b026

Browse files
Expand async extract tool error-handling coverage
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 43e1268 commit c55b026

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

tests/test_tools_extract.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,35 @@ def test_extract_tool_runnable_wraps_serialization_failures():
342342
assert exc_info.value.original_error is not None
343343

344344

345+
def test_extract_tool_async_runnable_returns_empty_string_for_none_data():
346+
client = _AsyncClient(response_data=None)
347+
348+
async def run():
349+
return await WebsiteExtractTool.async_runnable(
350+
client, {"urls": ["https://example.com"]}
351+
)
352+
353+
output = asyncio.run(run())
354+
355+
assert output == ""
356+
357+
358+
def test_extract_tool_async_runnable_wraps_serialization_failures():
359+
client = _AsyncClient(response_data={1, 2})
360+
361+
async def run():
362+
return await WebsiteExtractTool.async_runnable(
363+
client, {"urls": ["https://example.com"]}
364+
)
365+
366+
with pytest.raises(
367+
HyperbrowserError, match="Failed to serialize extract tool response data"
368+
) as exc_info:
369+
asyncio.run(run())
370+
371+
assert exc_info.value.original_error is not None
372+
373+
345374
def test_extract_tool_runnable_wraps_unexpected_schema_parse_failures(
346375
monkeypatch: pytest.MonkeyPatch,
347376
):
@@ -364,6 +393,31 @@ def _raise_recursion_error(_: str):
364393
assert exc_info.value.original_error is not None
365394

366395

396+
def test_extract_tool_async_runnable_wraps_unexpected_schema_parse_failures(
397+
monkeypatch: pytest.MonkeyPatch,
398+
):
399+
def _raise_recursion_error(_: str):
400+
raise RecursionError("schema parsing recursion overflow")
401+
402+
monkeypatch.setattr(tools_module.json, "loads", _raise_recursion_error)
403+
404+
async def run():
405+
await WebsiteExtractTool.async_runnable(
406+
_AsyncClient(),
407+
{
408+
"urls": ["https://example.com"],
409+
"schema": '{"type":"object"}',
410+
},
411+
)
412+
413+
with pytest.raises(
414+
HyperbrowserError, match="Invalid JSON string provided for `schema`"
415+
) as exc_info:
416+
asyncio.run(run())
417+
418+
assert exc_info.value.original_error is not None
419+
420+
367421
def test_extract_tool_runnable_preserves_hyperbrowser_schema_parse_errors(
368422
monkeypatch: pytest.MonkeyPatch,
369423
):
@@ -384,3 +438,28 @@ def _raise_hyperbrowser_error(_: str):
384438
)
385439

386440
assert exc_info.value.original_error is None
441+
442+
443+
def test_extract_tool_async_runnable_preserves_hyperbrowser_schema_parse_errors(
444+
monkeypatch: pytest.MonkeyPatch,
445+
):
446+
def _raise_hyperbrowser_error(_: str):
447+
raise HyperbrowserError("custom schema parse failure")
448+
449+
monkeypatch.setattr(tools_module.json, "loads", _raise_hyperbrowser_error)
450+
451+
async def run():
452+
await WebsiteExtractTool.async_runnable(
453+
_AsyncClient(),
454+
{
455+
"urls": ["https://example.com"],
456+
"schema": '{"type":"object"}',
457+
},
458+
)
459+
460+
with pytest.raises(
461+
HyperbrowserError, match="custom schema parse failure"
462+
) as exc_info:
463+
asyncio.run(run())
464+
465+
assert exc_info.value.original_error is None

0 commit comments

Comments
 (0)