|
| 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