Skip to content

Commit a771564

Browse files
committed
feat: updated tests searchscraper
1 parent 0e1dfd1 commit a771564

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

tests/unit_tests/mocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Optional, Type
22

33
from langchain_core.tools import BaseTool
44
from pydantic import BaseModel, Field
@@ -115,6 +115,7 @@ class MockSearchScraperTool(BaseTool):
115115
args_schema: type[BaseModel] = MockSearchScraperInput
116116
client: Optional[MockClient] = None
117117
api_key: str
118+
llm_output_schema: Optional[Type[BaseModel]] = None
118119

119120
def _run(self, **kwargs: Any) -> Dict:
120121
return {

tests/unit_tests/test_tools.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,22 @@ def tool_invoke_params_example(self) -> dict:
3535
"website_url": "https://example.com",
3636
}
3737

38+
39+
class TestSmartScraperToolCustom:
3840
def test_invoke_with_html(self):
3941
"""Test invoking the tool with HTML content."""
40-
tool = self.tool_constructor(**self.tool_constructor_params)
41-
result = tool.invoke(
42-
{
43-
"user_prompt": "Extract the main heading",
44-
"website_url": "https://example.com",
45-
"website_html": "<html><body><h1>Test</h1></body></html>",
46-
}
47-
)
48-
assert isinstance(result, dict)
49-
assert "main_heading" in result
50-
assert result["main_heading"] == "Test"
42+
with patch("langchain_scrapegraph.tools.smartscraper.Client", MockClient):
43+
tool = MockSmartScraperTool(api_key="sgai-test-api-key")
44+
result = tool.invoke(
45+
{
46+
"user_prompt": "Extract the main heading",
47+
"website_url": "https://example.com",
48+
"website_html": "<html><body><h1>Test</h1></body></html>",
49+
}
50+
)
51+
assert isinstance(result, dict)
52+
assert "main_heading" in result
53+
assert result["main_heading"] == "Test"
5154

5255

5356
class TestSearchScraperToolUnit(ToolsUnitTests):
@@ -66,6 +69,8 @@ def tool_invoke_params_example(self) -> dict:
6669
"user_prompt": "What are the key features of Product X?",
6770
}
6871

72+
73+
class TestSearchScraperToolCustom:
6974
def test_invoke_with_schema(self):
7075
"""Test invoking the tool with a schema."""
7176
from typing import List
@@ -77,14 +82,17 @@ class TestSchema(BaseModel):
7782
features: List[dict] = Field(description="List of features")
7883
reference_urls: List[str] = Field(description="Reference URLs")
7984

80-
tool = self.tool_constructor(**self.tool_constructor_params)
81-
tool.llm_output_schema = TestSchema
82-
result = tool.invoke(self.tool_invoke_params_example)
83-
assert isinstance(result, dict)
84-
assert "product" in result
85-
assert "features" in result
86-
assert "reference_urls" in result
87-
assert isinstance(result["reference_urls"], list)
85+
with patch("langchain_scrapegraph.tools.searchscraper.Client", MockClient):
86+
tool = MockSearchScraperTool(api_key="sgai-test-api-key")
87+
tool.llm_output_schema = TestSchema
88+
result = tool.invoke(
89+
{"user_prompt": "What are the key features of Product X?"}
90+
)
91+
assert isinstance(result, dict)
92+
assert "product" in result
93+
assert "features" in result
94+
assert "reference_urls" in result
95+
assert isinstance(result["reference_urls"], list)
8896

8997

9098
class TestGetCreditsToolUnit(ToolsUnitTests):

0 commit comments

Comments
 (0)