Skip to content

Commit a46f45d

Browse files
committed
add tests
1 parent 582c99d commit a46f45d

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

tests/graphs/.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
OPENAI_APIKEY="your openai.com api key"

tests/graphs/custom_graph_test.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Module for testing the class custom_graph class
3+
"""
4+
import unittest
5+
import os
6+
from dotenv import load_dotenv
7+
from scrapegraphai.models import OpenAI
8+
from scrapegraphai.graphs import BaseGraph
9+
from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
10+
11+
12+
class TestCustomGraph(unittest.TestCase):
13+
"""
14+
class for testing the class custom_graph
15+
"""
16+
17+
@classmethod
18+
def setUpClass(cls):
19+
load_dotenv()
20+
openai_key = os.getenv("OPENAI_APIKEY")
21+
llm_config = {
22+
"api_key": openai_key,
23+
"model_name": "gpt-3.5-turbo",
24+
"temperature": 0,
25+
"streaming": True
26+
}
27+
cls.model = OpenAI(llm_config)
28+
cls.fetch_html_node = FetchHTMLNode("fetch_html")
29+
cls.parse_document_node = ParseHTMLNode("parse_document")
30+
cls.generate_answer_node = GenerateAnswerNode(
31+
cls.model, "generate_answer")
32+
cls.graph = BaseGraph(
33+
nodes={
34+
cls.fetch_html_node,
35+
cls.parse_document_node,
36+
cls.generate_answer_node
37+
},
38+
edges={
39+
(cls.fetch_html_node, cls.parse_document_node),
40+
(cls.parse_document_node, cls.generate_answer_node)
41+
},
42+
entry_point=cls.fetch_html_node
43+
)
44+
45+
def test_execution(self):
46+
"""
47+
Execution of the test
48+
"""
49+
inputs = {"user_input": "Give me the news",
50+
"url": "https://www.ansa.it/sito/notizie/topnews/index.shtml"}
51+
result = self.graph.execute(inputs)
52+
answer = result.get("answer", "No answer found.")
53+
self.assertIsNotNone(answer)
54+
self.assertNotEqual(answer, "No answer found.")
55+
56+
57+
if __name__ == '__main__':
58+
unittest.main()

tests/graphs/smart_scraper_test.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Module for testing the class SmartScraperGraph
3+
"""
4+
import unittest
5+
import os
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import SmartScraperGraph
8+
9+
10+
class TestSmartScraperGraph(unittest.TestCase):
11+
"""
12+
class for testing the class SmartScraperGraph
13+
"""
14+
15+
@classmethod
16+
def setUpClass(cls):
17+
load_dotenv()
18+
openai_key = os.getenv("OPENAI_APIKEY")
19+
cls.llm_config = {
20+
"api_key": openai_key,
21+
"model_name": "gpt-3.5-turbo",
22+
}
23+
cls.URL = "https://perinim.github.io/projects/"
24+
cls.PROMPT = "List me all the titles and project descriptions and give me an audio"
25+
cls.smart_scraper_graph = SmartScraperGraph(
26+
cls.PROMPT, cls.URL, cls.llm_config)
27+
28+
def test_scraper_execution(self):
29+
"""
30+
Execution of the test
31+
"""
32+
answer = self.smart_scraper_graph.run()
33+
self.assertIsNotNone(answer)
34+
self.assertNotEqual(answer, "")
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

tests/graphs/speech_summary_test.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Module for testing the class SpeechSummaryGraph
3+
"""
4+
import unittest
5+
import os
6+
from dotenv import load_dotenv
7+
from scrapegraphai.graphs import SpeechSummaryGraph
8+
9+
10+
class TestSpeechSummaryGraph(unittest.TestCase):
11+
"""
12+
class for testing the class SpeechSummaryGraph
13+
"""
14+
15+
def setUp(self):
16+
load_dotenv()
17+
openai_key = os.getenv("OPENAI_APIKEY")
18+
self.llm_config = {
19+
"api_key": openai_key,
20+
}
21+
self.curr_dir = os.path.dirname(os.path.realpath(__file__))
22+
self.output_file_path = os.path.join(
23+
self.curr_dir, "website_summary.mp3")
24+
25+
def test_summary_generation(self):
26+
"""
27+
Execution of the test
28+
"""
29+
speech_summary_graph = SpeechSummaryGraph("""Make a summary of the news to be
30+
converted to audio for
31+
blind people.""",
32+
"https://www.wired.com/category/science/",
33+
self.llm_config,
34+
self.output_file_path)
35+
final_state = speech_summary_graph.run()
36+
result = final_state.get("answer", "No answer found.")
37+
self.assertIsNotNone(result)
38+
self.assertNotEqual(result, "No answer found.")
39+
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)