mirror of
https://github.com/VinciGit00/Scrapegraph-ai.git
synced 2026-07-09 21:19:20 +08:00
test: Update coverage improvement test for tests/test_json_scraper_graph.py
This commit is contained in:
parent
d022e5c53e
commit
f5ebe8ac10
@ -95,4 +95,42 @@ class TestJSONScraperGraph:
|
||||
mock_execute.assert_called_once_with({"user_prompt": "Analyze the data from the JSON file", "json": "path/to/single/file.json"})
|
||||
mock_fetch_node.assert_called_once()
|
||||
mock_generate_answer_node.assert_called_once()
|
||||
mock_create_llm.assert_called_once_with({"model": "test-model", "temperature": 0})
|
||||
|
||||
@patch('scrapegraphai.graphs.json_scraper_graph.FetchNode')
|
||||
@patch('scrapegraphai.graphs.json_scraper_graph.GenerateAnswerNode')
|
||||
@patch.object(JSONScraperGraph, '_create_llm')
|
||||
def test_json_scraper_graph_no_answer_found(self, mock_create_llm, mock_generate_answer_node, mock_fetch_node, mock_llm_model, mock_embedder_model):
|
||||
"""
|
||||
Test JSONScraperGraph when no answer is found.
|
||||
This test checks if the graph correctly handles the scenario where no answer is generated,
|
||||
ensuring it returns the default "No answer found." message.
|
||||
"""
|
||||
# Mock the _create_llm method to return a mock LLM model
|
||||
mock_create_llm.return_value = mock_llm_model
|
||||
|
||||
# Mock the execute method of BaseGraph to return an empty answer
|
||||
with patch('scrapegraphai.graphs.json_scraper_graph.BaseGraph.execute') as mock_execute:
|
||||
mock_execute.return_value = ({}, {}) # Empty state and execution info
|
||||
|
||||
# Create a JSONScraperGraph instance
|
||||
graph = JSONScraperGraph(
|
||||
prompt="Query that produces no answer",
|
||||
source="path/to/empty/file.json",
|
||||
config={"llm": {"model": "test-model", "temperature": 0}},
|
||||
schema=BaseModel
|
||||
)
|
||||
|
||||
# Set mocked embedder model
|
||||
graph.embedder_model = mock_embedder_model
|
||||
|
||||
# Run the graph
|
||||
result = graph.run()
|
||||
|
||||
# Assertions
|
||||
assert result == "No answer found."
|
||||
assert graph.input_key == "json"
|
||||
mock_execute.assert_called_once_with({"user_prompt": "Query that produces no answer", "json": "path/to/empty/file.json"})
|
||||
mock_fetch_node.assert_called_once()
|
||||
mock_generate_answer_node.assert_called_once()
|
||||
mock_create_llm.assert_called_once_with({"model": "test-model", "temperature": 0})
|
||||
Loading…
Reference in New Issue
Block a user