test: Enhance JSON scraping pipeline test

This commit enhances the test suite for the JSON scraping pipeline by introducing the following improvements:

- Separate configuration from the test code by loading it from a JSON file (config.json)
- Use a parametrized fixture to run the test with multiple configurations automatically
- Read the sample JSON file from a separate inputs directory for better organization
- Add explicit assertions to verify the expected output (list of titles)
- Improve test organization and separation of concerns using fixtures
- Promote better coding practices and make the test suite more extensible

These changes aim to improve the testability, maintainability, and flexibility of the test suite. They make it easier to manage configurations, add or modify test cases, and ensure the robustness of the scraping pipeline. The test suite now follows best practices and is better prepared for future changes and requirements.
This commit is contained in:
Tejas Amol Hande 2024-06-07 13:00:54 +05:30 committed by GitHub
parent c17daca409
commit d845a1ba7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,56 +1,50 @@
""" """
Module for scraping json documents Module for scraping JSON documents
""" """
import os import os
import json
import pytest import pytest
from scrapegraphai.graphs import JSONScraperGraph from scrapegraphai.graphs import JSONScraperGraph
# Load configuration from a JSON file
CONFIG_FILE = "config.json"
with open(CONFIG_FILE, "r") as f:
CONFIG = json.load(f)
# Fixture to read the sample JSON file
@pytest.fixture @pytest.fixture
def sample_json(): def sample_json():
""" """
Example of text Read the sample JSON file
""" """
file_name = "inputs/example.json" file_path = os.path.join(os.path.dirname(__file__), "inputs", "example.json")
curr_dir = os.path.dirname(os.path.realpath(__file__)) with open(file_path, "r", encoding="utf-8") as file:
file_path = os.path.join(curr_dir, file_name)
with open(file_path, 'r', encoding="utf-8") as file:
text = file.read() text = file.read()
return text return text
# Parametrized fixture to load graph configurations
@pytest.fixture(params=CONFIG["graph_configs"])
def graph_config(request):
"""
Load graph configuration
"""
return request.param
@pytest.fixture # Test function for the scraping pipeline
def graph_config(): def test_scraping_pipeline(sample_json, graph_config):
""" """
Configuration of the graph Test the scraping pipeline
""" """
return { expected_titles = ["Title 1", "Title 2", "Title 3"] # Replace with expected titles
"llm": {
"model": "ollama/mistral",
"temperature": 0,
"format": "json",
"base_url": "http://localhost:11434",
},
"embeddings": {
"model": "ollama/nomic-embed-text",
"temperature": 0,
"base_url": "http://localhost:11434",
}
}
def test_scraping_pipeline(sample_json: str, graph_config: dict):
"""
Start of the scraping pipeline
"""
smart_scraper_graph = JSONScraperGraph( smart_scraper_graph = JSONScraperGraph(
prompt="List me all the titles", prompt="List me all the titles",
source=sample_json, source=sample_json,
config=graph_config config=graph_config
) )
result = smart_scraper_graph.run() result = smart_scraper_graph.run()
assert result is not None assert result is not None
assert isinstance(result, list)
assert sorted(result) == sorted(expected_titles)