@@ -95,4 +95,42 @@ def test_json_scraper_graph_with_single_file(self, mock_create_llm, mock_generat
9595 mock_execute .assert_called_once_with ({"user_prompt" : "Analyze the data from the JSON file" , "json" : "path/to/single/file.json" })
9696 mock_fetch_node .assert_called_once ()
9797 mock_generate_answer_node .assert_called_once ()
98+ mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
99+
100+ @patch ('scrapegraphai.graphs.json_scraper_graph.FetchNode' )
101+ @patch ('scrapegraphai.graphs.json_scraper_graph.GenerateAnswerNode' )
102+ @patch .object (JSONScraperGraph , '_create_llm' )
103+ 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 ):
104+ """
105+ Test JSONScraperGraph when no answer is found.
106+ This test checks if the graph correctly handles the scenario where no answer is generated,
107+ ensuring it returns the default "No answer found." message.
108+ """
109+ # Mock the _create_llm method to return a mock LLM model
110+ mock_create_llm .return_value = mock_llm_model
111+
112+ # Mock the execute method of BaseGraph to return an empty answer
113+ with patch ('scrapegraphai.graphs.json_scraper_graph.BaseGraph.execute' ) as mock_execute :
114+ mock_execute .return_value = ({}, {}) # Empty state and execution info
115+
116+ # Create a JSONScraperGraph instance
117+ graph = JSONScraperGraph (
118+ prompt = "Query that produces no answer" ,
119+ source = "path/to/empty/file.json" ,
120+ config = {"llm" : {"model" : "test-model" , "temperature" : 0 }},
121+ schema = BaseModel
122+ )
123+
124+ # Set mocked embedder model
125+ graph .embedder_model = mock_embedder_model
126+
127+ # Run the graph
128+ result = graph .run ()
129+
130+ # Assertions
131+ assert result == "No answer found."
132+ assert graph .input_key == "json"
133+ mock_execute .assert_called_once_with ({"user_prompt" : "Query that produces no answer" , "json" : "path/to/empty/file.json" })
134+ mock_fetch_node .assert_called_once ()
135+ mock_generate_answer_node .assert_called_once ()
98136 mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
0 commit comments