Skip to content

Commit 7dba46a

Browse files
committed
add tests
1 parent 7797631 commit 7dba46a

File tree

5 files changed

+293
-1
lines changed

5 files changed

+293
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
code_generator_graph_openai_test module
3+
"""
4+
import os
5+
from typing import List
6+
import pytest
7+
from dotenv import load_dotenv
8+
from pydantic import BaseModel, Field
9+
from scrapegraphai.graphs import CodeGeneratorGraph
10+
11+
load_dotenv()
12+
13+
# ************************************************
14+
# Define the output schema for the graph
15+
# ************************************************
16+
17+
class Project(BaseModel):
18+
title: str = Field(description="The title of the project")
19+
description: str = Field(description="The description of the project")
20+
21+
class Projects(BaseModel):
22+
projects: List[Project]
23+
24+
@pytest.fixture
25+
def graph_config():
26+
"""
27+
Configuration for the CodeGeneratorGraph
28+
"""
29+
openai_key = os.getenv("OPENAI_APIKEY")
30+
return {
31+
"llm": {
32+
"api_key": openai_key,
33+
"model": "openai/gpt-4o-mini",
34+
},
35+
"verbose": True,
36+
"headless": False,
37+
"reduction": 2,
38+
"max_iterations": {
39+
"overall": 10,
40+
"syntax": 3,
41+
"execution": 3,
42+
"validation": 3,
43+
"semantic": 3
44+
},
45+
"output_file_name": "extracted_data.py"
46+
}
47+
48+
def test_code_generator_graph(graph_config: dict):
49+
"""
50+
Test the CodeGeneratorGraph scraping pipeline
51+
"""
52+
code_generator_graph = CodeGeneratorGraph(
53+
prompt="List me all the projects with their description",
54+
source="https://perinim.github.io/projects/",
55+
schema=Projects,
56+
config=graph_config
57+
)
58+
59+
result = code_generator_graph.run()
60+
61+
assert result is not None
62+
63+
64+
def test_code_generator_execution_info(graph_config: dict):
65+
"""
66+
Test getting the execution info of CodeGeneratorGraph
67+
"""
68+
code_generator_graph = CodeGeneratorGraph(
69+
prompt="List me all the projects with their description",
70+
source="https://perinim.github.io/projects/",
71+
schema=Projects,
72+
config=graph_config
73+
)
74+
75+
code_generator_graph.run()
76+
77+
graph_exec_info = code_generator_graph.get_execution_info()
78+
79+
assert graph_exec_info is not None
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
depth_search_graph test
3+
"""
4+
import os
5+
import pytest
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import DepthSearchGraph
8+
9+
load_dotenv()
10+
11+
@pytest.fixture
12+
def graph_config():
13+
"""
14+
Configuration for the DepthSearchGraph
15+
"""
16+
openai_key = os.getenv("OPENAI_APIKEY")
17+
return {
18+
"llm": {
19+
"api_key": openai_key,
20+
"model": "openai/gpt-4o-mini",
21+
},
22+
"verbose": True,
23+
"headless": False,
24+
"depth": 2,
25+
"only_inside_links": False,
26+
}
27+
28+
def test_depth_search_graph(graph_config: dict):
29+
"""
30+
Test the DepthSearchGraph scraping pipeline
31+
"""
32+
search_graph = DepthSearchGraph(
33+
prompt="List me all the projects with their description",
34+
source="https://perinim.github.io",
35+
config=graph_config
36+
)
37+
38+
result = search_graph.run()
39+
40+
assert result is not None
41+
42+
43+
def test_depth_search_execution_info(graph_config: dict):
44+
"""
45+
Test getting the execution info of DepthSearchGraph
46+
"""
47+
search_graph = DepthSearchGraph(
48+
prompt="List me all the projects with their description",
49+
source="https://perinim.github.io",
50+
config=graph_config
51+
)
52+
53+
search_graph.run()
54+
55+
graph_exec_info = search_graph.get_execution_info()
56+
57+
assert graph_exec_info is not None
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
search_graph_openai_test.py module
3+
"""
4+
import os
5+
import pytest
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import SearchGraph
8+
9+
load_dotenv()
10+
11+
# ************************************************
12+
# Define the test fixtures and helpers
13+
# ************************************************
14+
15+
@pytest.fixture
16+
def graph_config():
17+
"""
18+
Configuration for the SearchGraph
19+
"""
20+
openai_key = os.getenv("OPENAI_APIKEY")
21+
return {
22+
"llm": {
23+
"api_key": openai_key,
24+
"model": "openai/gpt-4o",
25+
},
26+
"max_results": 2,
27+
"verbose": True,
28+
}
29+
30+
# ************************************************
31+
# Define the test cases
32+
# ************************************************
33+
34+
def test_search_graph(graph_config: dict):
35+
"""
36+
Test the SearchGraph functionality
37+
"""
38+
search_graph = SearchGraph(
39+
prompt="List me Chioggia's famous dishes",
40+
config=graph_config
41+
)
42+
43+
result = search_graph.run()
44+
45+
assert result is not None
46+
assert len(result) > 0
47+
48+
49+
def test_search_graph_execution_info(graph_config: dict):
50+
"""
51+
Test getting the execution info of SearchGraph
52+
"""
53+
search_graph = SearchGraph(
54+
prompt="List me Chioggia's famous dishes",
55+
config=graph_config
56+
)
57+
58+
search_graph.run()
59+
60+
graph_exec_info = search_graph.get_execution_info()
61+
62+
assert graph_exec_info is not None

tests/graphs/smart_scraper_ernie_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ def test_get_execution_info(graph_config: dict):
4949

5050
graph_exec_info = smart_scraper_graph.get_execution_info()
5151

52-
assert graph_exec_info is not None
52+
assert graph_exec_info is not None
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
xml_scraper_test
3+
"""
4+
import os
5+
import pytest
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import XMLScraperGraph
8+
from scrapegraphai.utils import convert_to_csv, convert_to_json, prettify_exec_info
9+
10+
load_dotenv()
11+
12+
# ************************************************
13+
# Define the test fixtures and helpers
14+
# ************************************************
15+
16+
@pytest.fixture
17+
def graph_config():
18+
"""
19+
Configuration for the XMLScraperGraph
20+
"""
21+
openai_key = os.getenv("OPENAI_APIKEY")
22+
return {
23+
"llm": {
24+
"api_key": openai_key,
25+
"model": "openai/gpt-4o",
26+
},
27+
"verbose": False,
28+
}
29+
30+
@pytest.fixture
31+
def xml_content():
32+
"""
33+
Fixture to read the XML file content
34+
"""
35+
FILE_NAME = "inputs/books.xml"
36+
curr_dir = os.path.dirname(os.path.realpath(__file__))
37+
file_path = os.path.join(curr_dir, FILE_NAME)
38+
39+
with open(file_path, 'r', encoding="utf-8") as file:
40+
return file.read()
41+
42+
# ************************************************
43+
# Define the test cases
44+
# ************************************************
45+
46+
def test_xml_scraper_graph(graph_config: dict, xml_content: str):
47+
"""
48+
Test the XMLScraperGraph scraping pipeline
49+
"""
50+
xml_scraper_graph = XMLScraperGraph(
51+
prompt="List me all the authors, title and genres of the books",
52+
source=xml_content, # Pass the XML content
53+
config=graph_config
54+
)
55+
56+
result = xml_scraper_graph.run()
57+
58+
assert result is not None
59+
60+
def test_xml_scraper_execution_info(graph_config: dict, xml_content: str):
61+
"""
62+
Test getting the execution info of XMLScraperGraph
63+
"""
64+
xml_scraper_graph = XMLScraperGraph(
65+
prompt="List me all the authors, title and genres of the books",
66+
source=xml_content, # Pass the XML content
67+
config=graph_config
68+
)
69+
70+
xml_scraper_graph.run()
71+
72+
graph_exec_info = xml_scraper_graph.get_execution_info()
73+
74+
assert graph_exec_info is not None
75+
print(prettify_exec_info(graph_exec_info))
76+
77+
def test_xml_scraper_save_results(graph_config: dict, xml_content: str):
78+
"""
79+
Test saving the results of XMLScraperGraph to CSV and JSON
80+
"""
81+
xml_scraper_graph = XMLScraperGraph(
82+
prompt="List me all the authors, title and genres of the books",
83+
source=xml_content, # Pass the XML content
84+
config=graph_config
85+
)
86+
87+
result = xml_scraper_graph.run()
88+
89+
# Save to csv and json
90+
convert_to_csv(result, "result")
91+
convert_to_json(result, "result")
92+
93+
assert os.path.exists("result.csv")
94+
assert os.path.exists("result.json")

0 commit comments

Comments
 (0)