Skip to content

Python scraper based on AI

add tests

VinciGit005f20299cf34e
diff --git a/tests/graphs/custom_graph_test.py b/tests/graphs/custom_graph_test.py
deleted file mode 100644
index 89ad7c6f..00000000
--- a/tests/graphs/custom_graph_test.py
+++ /dev/null
@@ -1,58 +0,0 @@
-"""
-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/results.json b/tests/graphs/results.json
new file mode 100644
index 00000000..15980f90
--- /dev/null
+++ b/tests/graphs/results.json
@@ -0,0 +1,62 @@
+[
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=1",
+ "name": "T-shirt with Owl Print",
+ "description": "Comfortable cotton t-shirt featuring an owl print.",
+ "price": "€20.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=2",
+ "name": "Classic Leather Wallet",
+ "description": "Genuine leather wallet with multiple compartments.",
+ "price": "€35.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=3",
+ "name": "Canvas Backpack",
+ "description": "Spacious canvas backpack perfect for daily use.",
+ "price": "€45.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=4",
+ "name": "Stainless Steel Water Bottle",
+ "description": "Durable stainless steel water bottle, eco-friendly.",
+ "price": "€15.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=5",
+ "name": "Wooden Desk Organizer",
+ "description": "Handcrafted wooden desk organizer with compartments.",
+ "price": "€25.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=6",
+ "name": "Ceramic Coffee Mug",
+ "description": "Stylish ceramic coffee mug, microwave safe.",
+ "price": "€10.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=7",
+ "name": "Smartphone Stand",
+ "description": "Adjustable smartphone stand for hands-free viewing.",
+ "price": "€8.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=8",
+ "name": "Soft Cotton Bath Towel",
+ "description": "Luxuriously soft cotton bath towel for spa-like comfort.",
+ "price": "€18.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=9",
+ "name": "Wireless Bluetooth Earphones",
+ "description": "High-quality wireless Bluetooth earphones with noise cancellation.",
+ "price": "€50.00"
+ },
+ {
+ "imageUrl": "https://picsum.photos/200/300?random=10",
+ "name": "Glass Fruit Bowl",
+ "description": "Elegant glass fruit bowl to display fruits beautifully.",
+ "price": "€30.00"
+ }
+]
diff --git a/tests/graphs/smart_scraper_test.py b/tests/graphs/smart_scraper_test.py
index acb94f1e..70702feb 100644
--- a/tests/graphs/smart_scraper_test.py
+++ b/tests/graphs/smart_scraper_test.py
@@ -1,37 +1,37 @@
-"""
-Module for testing the class SmartScraperGraph
-"""
-import unittest
import os
+import unittest
from dotenv import load_dotenv
from scrapegraphai.graphs import SmartScraperGraph
+load_dotenv()
-class TestSmartScraperGraph(unittest.TestCase):
- """
- class for testing the class SmartScraperGraph
- """
- @classmethod
- def setUpClass(cls):
- load_dotenv()
+class TestScrapingPipeline(unittest.TestCase):
+ """Test class for SmartScraperGraph"""
+
+ def setUp(self):
+ # Define the configuration for the language model
openai_key = os.getenv("OPENAI_APIKEY")
- cls.llm_config = {
+ 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, "")
+
+ # Define URL and PROMPT
+ url = "https://perinim.github.io/projects/"
+ prompt = "List me all the titles and project descriptions"
+
+ # Create the SmartScraperGraph instance
+ self.smart_scraper_graph = SmartScraperGraph(prompt, url, llm_config)
+
+ def test_scraping(self):
+ """Main test method"""
+ # Run the scraper
+ result = self.smart_scraper_graph.run()
+ print(result)
+
+ # Check if the result is not empty
+ self.assertNotEqual(result, {})
if __name__ == '__main__':
diff --git a/tests/graphs/speech_summary_test.py b/tests/graphs/speech_summary_test.py
index ad9d9ecd..15e190e6 100644
--- a/tests/graphs/speech_summary_test.py
+++ b/tests/graphs/speech_summary_test.py
@@ -1,41 +1,42 @@
-"""
-Module for testing the class SpeechSummaryGraph
-"""
-import unittest
import os
+import unittest
from dotenv import load_dotenv
from scrapegraphai.graphs import SpeechSummaryGraph
+load_dotenv()
+
class TestSpeechSummaryGraph(unittest.TestCase):
- """
- class for testing the class SpeechSummaryGraph
- """
+ """Test class for SpeechSummaryGraph"""
def setUp(self):
- load_dotenv()
+ # Define the configuration for the language model
openai_key = os.getenv("OPENAI_APIKEY")
- self.llm_config = {
+ 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")
+
+ # Save the audio to a file
+ curr_dir = os.path.dirname(os.path.realpath(__file__))
+ output_file_path = os.path.join(curr_dir, "website_summary.mp3")
+
+ # Initialize SpeechSummaryGraph instance
+ self.speech_summary_graph = SpeechSummaryGraph(
+ "Make a summary of the news to be converted to audio for blind people.",
+ "https://www.wired.com/category/science/",
+ llm_config,
+ output_file_path
+ )
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.")
+ """Test summary generation and audio conversion"""
+ # Run the summary graph
+ final_state = self.speech_summary_graph.run()
+ print(final_state)
+
+ # Check if the final state contains non-empty 'answer' key
+ self.assertTrue(final_state.get("answer")
+ is not None and final_state["answer"] != "")
if __name__ == '__main__':