Skip to content

Python scraper based on AI

add tests

VinciGit00a46f45de1446
diff --git a/tests/graphs/.env.example b/tests/graphs/.env.example
new file mode 100644
index 00000000..42454237
--- /dev/null
+++ b/tests/graphs/.env.example
@@ -0,0 +1 @@
+OPENAI_APIKEY="your openai.com api key"
\ No newline at end of file
diff --git a/tests/graphs/custom_graph_test.py b/tests/graphs/custom_graph_test.py
new file mode 100644
index 00000000..89ad7c6f
--- /dev/null
+++ b/tests/graphs/custom_graph_test.py
@@ -0,0 +1,58 @@
+"""
+Module for testing the class custom_graph class
+"""
+import unittest
+import os
+from dotenv import load_dotenv
+from scrapegraphai.models import OpenAI
+from scrapegraphai.graphs import BaseGraph
+from scrapegraphai.nodes import FetchHTMLNode, ParseHTMLNode, GenerateAnswerNode
+
+
+class TestCustomGraph(unittest.TestCase):
+ """
+ class for testing the class custom_graph
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ load_dotenv()
+ openai_key = os.getenv("OPENAI_APIKEY")
+ llm_config = {
+ "api_key": openai_key,
+ "model_name": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+ }
+ cls.model = OpenAI(llm_config)
+ cls.fetch_html_node = FetchHTMLNode("fetch_html")
+ cls.parse_document_node = ParseHTMLNode("parse_document")
+ cls.generate_answer_node = GenerateAnswerNode(
+ cls.model, "generate_answer")
+ cls.graph = BaseGraph(
+ nodes={
+ cls.fetch_html_node,
+ cls.parse_document_node,
+ cls.generate_answer_node
+ },
+ edges={
+ (cls.fetch_html_node, cls.parse_document_node),
+ (cls.parse_document_node, cls.generate_answer_node)
+ },
+ entry_point=cls.fetch_html_node
+ )
+
+ def test_execution(self):
+ """
+ Execution of the test
+ """
+ inputs = {"user_input": "Give me the news",
+ "url": "https://www.ansa.it/sito/notizie/topnews/index.shtml"}
+ result = self.graph.execute(inputs)
+ answer = result.get("answer", "No answer found.")
+ self.assertIsNotNone(answer)
+ self.assertNotEqual(answer, "No answer found.")
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/graphs/smart_scraper_test.py b/tests/graphs/smart_scraper_test.py
new file mode 100644
index 00000000..acb94f1e
--- /dev/null
+++ b/tests/graphs/smart_scraper_test.py
@@ -0,0 +1,38 @@
+"""
+Module for testing the class SmartScraperGraph
+"""
+import unittest
+import os
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SmartScraperGraph
+
+
+class TestSmartScraperGraph(unittest.TestCase):
+ """
+ class for testing the class SmartScraperGraph
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ load_dotenv()
+ openai_key = os.getenv("OPENAI_APIKEY")
+ cls.llm_config = {
+ "api_key": openai_key,
+ "model_name": "gpt-3.5-turbo",
+ }
+ cls.URL = "https://perinim.github.io/projects/"
+ cls.PROMPT = "List me all the titles and project descriptions and give me an audio"
+ cls.smart_scraper_graph = SmartScraperGraph(
+ cls.PROMPT, cls.URL, cls.llm_config)
+
+ def test_scraper_execution(self):
+ """
+ Execution of the test
+ """
+ answer = self.smart_scraper_graph.run()
+ self.assertIsNotNone(answer)
+ self.assertNotEqual(answer, "")
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tests/graphs/speech_summary_test.py b/tests/graphs/speech_summary_test.py
new file mode 100644
index 00000000..ad9d9ecd
--- /dev/null
+++ b/tests/graphs/speech_summary_test.py
@@ -0,0 +1,42 @@
+"""
+Module for testing the class SpeechSummaryGraph
+"""
+import unittest
+import os
+from dotenv import load_dotenv
+from scrapegraphai.graphs import SpeechSummaryGraph
+
+
+class TestSpeechSummaryGraph(unittest.TestCase):
+ """
+ class for testing the class SpeechSummaryGraph
+ """
+
+ def setUp(self):
+ load_dotenv()
+ openai_key = os.getenv("OPENAI_APIKEY")
+ self.llm_config = {
+ "api_key": openai_key,
+ }
+ self.curr_dir = os.path.dirname(os.path.realpath(__file__))
+ self.output_file_path = os.path.join(
+ self.curr_dir, "website_summary.mp3")
+
+ def test_summary_generation(self):
+ """
+ Execution of the test
+ """
+ speech_summary_graph = SpeechSummaryGraph("""Make a summary of the news to be
+ converted to audio for
+ blind people.""",
+ "https://www.wired.com/category/science/",
+ self.llm_config,
+ self.output_file_path)
+ final_state = speech_summary_graph.run()
+ result = final_state.get("answer", "No answer found.")
+ self.assertIsNotNone(result)
+ self.assertNotEqual(result, "No answer found.")
+
+
+if __name__ == '__main__':
+ unittest.main()