Skip to content

Commit c002bf4

Browse files
authored
Merge pull request #911 from ScrapeGraphAI/codebeaver/pre/beta-904
codebeaver/pre/beta-904 - Unit Tests
2 parents cdb2f61 + ba58568 commit c002bf4

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

tests/test_json_scraper_graph.py

+38
Original file line numberDiff line numberDiff line change
@@ -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})

tests/test_search_graph.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

33
from scrapegraphai.graphs.search_graph import SearchGraph
4-
from unittest.mock import MagicMock, patch
4+
from unittest.mock import MagicMock, call, patch
55

66
class TestSearchGraph:
77
"""Test class for SearchGraph"""
@@ -57,4 +57,26 @@ def test_run_no_answer_found(self, mock_create_llm, mock_base_graph):
5757
result = search_graph.run()
5858

5959
# Assert
60-
assert result == "No answer found."
60+
assert result == "No answer found."
61+
62+
@patch('scrapegraphai.graphs.search_graph.SearchInternetNode')
63+
@patch('scrapegraphai.graphs.search_graph.GraphIteratorNode')
64+
@patch('scrapegraphai.graphs.search_graph.MergeAnswersNode')
65+
@patch('scrapegraphai.graphs.search_graph.BaseGraph')
66+
@patch('scrapegraphai.graphs.abstract_graph.AbstractGraph._create_llm')
67+
def test_max_results_config(self, mock_create_llm, mock_base_graph, mock_merge_answers, mock_graph_iterator, mock_search_internet):
68+
"""
69+
Test that the max_results parameter from the config is correctly passed to the SearchInternetNode.
70+
"""
71+
# Arrange
72+
prompt = "Test prompt"
73+
max_results = 5
74+
config = {"llm": {"model": "test-model"}, "max_results": max_results}
75+
76+
# Act
77+
search_graph = SearchGraph(prompt, config)
78+
79+
# Assert
80+
mock_search_internet.assert_called_once()
81+
call_args = mock_search_internet.call_args
82+
assert call_args.kwargs['node_config']['max_results'] == max_results

0 commit comments

Comments
 (0)