@@ -49,4 +49,50 @@ def test_json_scraper_graph_with_directory(self, mock_create_llm, mock_generate_
4949 mock_execute .assert_called_once_with ({"user_prompt" : "Summarize the data from all JSON files" , "json_dir" : "path/to/json/directory" })
5050 mock_fetch_node .assert_called_once ()
5151 mock_generate_answer_node .assert_called_once ()
52+ mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
53+
54+ @pytest .fixture
55+ def mock_llm_model (self ):
56+ return Mock ()
57+
58+ @pytest .fixture
59+ def mock_embedder_model (self ):
60+ return Mock ()
61+
62+ @patch ('scrapegraphai.graphs.json_scraper_graph.FetchNode' )
63+ @patch ('scrapegraphai.graphs.json_scraper_graph.GenerateAnswerNode' )
64+ @patch .object (JSONScraperGraph , '_create_llm' )
65+ def test_json_scraper_graph_with_single_file (self , mock_create_llm , mock_generate_answer_node , mock_fetch_node , mock_llm_model , mock_embedder_model ):
66+ """
67+ Test JSONScraperGraph with a single JSON file.
68+ This test checks if the graph correctly handles a single JSON file input
69+ and processes it to generate an answer.
70+ """
71+ # Mock the _create_llm method to return a mock LLM model
72+ mock_create_llm .return_value = mock_llm_model
73+
74+ # Mock the execute method of BaseGraph
75+ with patch ('scrapegraphai.graphs.json_scraper_graph.BaseGraph.execute' ) as mock_execute :
76+ mock_execute .return_value = ({"answer" : "Mocked answer for single JSON file" }, {})
77+
78+ # Create a JSONScraperGraph instance with a single JSON file
79+ graph = JSONScraperGraph (
80+ prompt = "Analyze the data from the JSON file" ,
81+ source = "path/to/single/file.json" ,
82+ config = {"llm" : {"model" : "test-model" , "temperature" : 0 }},
83+ schema = BaseModel
84+ )
85+
86+ # Set mocked embedder model
87+ graph .embedder_model = mock_embedder_model
88+
89+ # Run the graph
90+ result = graph .run ()
91+
92+ # Assertions
93+ assert result == "Mocked answer for single JSON file"
94+ assert graph .input_key == "json"
95+ mock_execute .assert_called_once_with ({"user_prompt" : "Analyze the data from the JSON file" , "json" : "path/to/single/file.json" })
96+ mock_fetch_node .assert_called_once ()
97+ mock_generate_answer_node .assert_called_once ()
5298 mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
0 commit comments