11import  pytest 
22
3- from  pydantic  import  BaseModel 
3+ from  pydantic  import  BaseModel ,  Field 
44from  scrapegraphai .graphs .json_scraper_graph  import  JSONScraperGraph 
55from  unittest .mock  import  Mock , patch 
66
@@ -133,4 +133,60 @@ def test_json_scraper_graph_no_answer_found(self, mock_create_llm, mock_generate
133133            mock_execute .assert_called_once_with ({"user_prompt" : "Query that produces no answer" , "json" : "path/to/empty/file.json" })
134134            mock_fetch_node .assert_called_once ()
135135            mock_generate_answer_node .assert_called_once ()
136+             mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
137+ 
138+     @pytest .fixture  
139+     def  mock_llm_model (self ):
140+         return  Mock ()
141+ 
142+     @pytest .fixture  
143+     def  mock_embedder_model (self ):
144+         return  Mock ()
145+ 
146+     @patch ('scrapegraphai.graphs.json_scraper_graph.FetchNode' ) 
147+     @patch ('scrapegraphai.graphs.json_scraper_graph.GenerateAnswerNode' ) 
148+     @patch .object (JSONScraperGraph , '_create_llm' ) 
149+     def  test_json_scraper_graph_with_custom_schema (self , mock_create_llm , mock_generate_answer_node , mock_fetch_node , mock_llm_model , mock_embedder_model ):
150+         """ 
151+         Test JSONScraperGraph with a custom schema. 
152+         This test checks if the graph correctly handles a custom schema input 
153+         and passes it to the GenerateAnswerNode. 
154+         """ 
155+         # Define a custom schema 
156+         class  CustomSchema (BaseModel ):
157+             name : str  =  Field (..., description = "Name of the attraction" )
158+             description : str  =  Field (..., description = "Description of the attraction" )
159+ 
160+         # Mock the _create_llm method to return a mock LLM model 
161+         mock_create_llm .return_value  =  mock_llm_model 
162+ 
163+         # Mock the execute method of BaseGraph 
164+         with  patch ('scrapegraphai.graphs.json_scraper_graph.BaseGraph.execute' ) as  mock_execute :
165+             mock_execute .return_value  =  ({"answer" : "Mocked answer with custom schema" }, {})
166+ 
167+             # Create a JSONScraperGraph instance with a custom schema 
168+             graph  =  JSONScraperGraph (
169+                 prompt = "List attractions in Chioggia" ,
170+                 source = "path/to/chioggia.json" ,
171+                 config = {"llm" : {"model" : "test-model" , "temperature" : 0 }},
172+                 schema = CustomSchema 
173+             )
174+ 
175+             # Set mocked embedder model 
176+             graph .embedder_model  =  mock_embedder_model 
177+ 
178+             # Run the graph 
179+             result  =  graph .run ()
180+ 
181+             # Assertions 
182+             assert  result  ==  "Mocked answer with custom schema" 
183+             assert  graph .input_key  ==  "json" 
184+             mock_execute .assert_called_once_with ({"user_prompt" : "List attractions in Chioggia" , "json" : "path/to/chioggia.json" })
185+             mock_fetch_node .assert_called_once ()
186+             mock_generate_answer_node .assert_called_once ()
187+ 
188+             # Check if the custom schema was passed to GenerateAnswerNode 
189+             generate_answer_node_call  =  mock_generate_answer_node .call_args [1 ]
190+             assert  generate_answer_node_call ['node_config' ]['schema' ] ==  CustomSchema 
191+ 
136192            mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
0 commit comments