1+ import pytest
2+
3+ from pydantic import BaseModel
4+ from scrapegraphai .graphs .json_scraper_graph import JSONScraperGraph
5+ from unittest .mock import Mock , patch
6+
7+ class TestJSONScraperGraph :
8+ @pytest .fixture
9+ def mock_llm_model (self ):
10+ return Mock ()
11+
12+ @pytest .fixture
13+ def mock_embedder_model (self ):
14+ return Mock ()
15+
16+ @patch ('scrapegraphai.graphs.json_scraper_graph.FetchNode' )
17+ @patch ('scrapegraphai.graphs.json_scraper_graph.GenerateAnswerNode' )
18+ @patch .object (JSONScraperGraph , '_create_llm' )
19+ def test_json_scraper_graph_with_directory (self , mock_create_llm , mock_generate_answer_node , mock_fetch_node , mock_llm_model , mock_embedder_model ):
20+ """
21+ Test JSONScraperGraph with a directory of JSON files.
22+ This test checks if the graph correctly handles multiple JSON files input
23+ and processes them to generate an answer.
24+ """
25+ # Mock the _create_llm method to return a mock LLM model
26+ mock_create_llm .return_value = mock_llm_model
27+
28+ # Mock the execute method of BaseGraph
29+ with patch ('scrapegraphai.graphs.json_scraper_graph.BaseGraph.execute' ) as mock_execute :
30+ mock_execute .return_value = ({"answer" : "Mocked answer for multiple JSON files" }, {})
31+
32+ # Create a JSONScraperGraph instance
33+ graph = JSONScraperGraph (
34+ prompt = "Summarize the data from all JSON files" ,
35+ source = "path/to/json/directory" ,
36+ config = {"llm" : {"model" : "test-model" , "temperature" : 0 }},
37+ schema = BaseModel
38+ )
39+
40+ # Set mocked embedder model
41+ graph .embedder_model = mock_embedder_model
42+
43+ # Run the graph
44+ result = graph .run ()
45+
46+ # Assertions
47+ assert result == "Mocked answer for multiple JSON files"
48+ assert graph .input_key == "json_dir"
49+ mock_execute .assert_called_once_with ({"user_prompt" : "Summarize the data from all JSON files" , "json_dir" : "path/to/json/directory" })
50+ mock_fetch_node .assert_called_once ()
51+ mock_generate_answer_node .assert_called_once ()
52+ mock_create_llm .assert_called_once_with ({"model" : "test-model" , "temperature" : 0 })
0 commit comments