Skip to content

Commit

Permalink
Merge pull request #11 from MahmoudHousam/write_unit_itegration_tests
Browse files Browse the repository at this point in the history
write unit/integration tests
  • Loading branch information
MahmoudHousam authored Dec 15, 2024
2 parents 11f79e5 + 6a4dad6 commit aef7446
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 10 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Test and Format Workflow

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
ci-workflow:
name: Python CI Workflow
runs-on: ubuntu-latest

steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.9"

- name: Install Dependencies
run: |
python -m pip install pip --upgrade
pip install -e .
pip install -r requirements.txt
- name: Format
run: black --check .

- name: Test
run: pytest





10 changes: 0 additions & 10 deletions .github/workflows/test.yml

This file was deleted.

6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
plotly==5.24.1
black==24.10.0
pytest==8.3.4
pandas==2.2.3
jinja2==3.1.4
bs4==0.0.2
30 changes: 30 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from setuptools import setup, find_packages

setup(
name="vizblend", # Name of your package
version="1.0.0", # Initial version
author="Mahmoud Housam",
author_email="[email protected]",
description="A Python package to generate HTML reports from Plotly figures using Jinja2 templates.",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/MahmoudHousam/VizBlend",
packages=find_packages(),
include_package_data=True,
install_requires=[
"plotly==5.24.1",
"black==24.10.0",
"pytest==8.3.4",
"pandas==2.2.3",
"jinja2==3.1.4",
"bs4==0.0.2",
],
classifiers=[
"Programming Language :: Python :: 3.9",
"Operating System :: OS Independent",
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Scientific/Engineering :: Visualization",
],
python_requires=">=3.9",
)
151 changes: 151 additions & 0 deletions tests/test_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import os
import pytest
import plotly.graph_objs as go
from bs4 import BeautifulSoup
from vizblend.create_report import CreateReport


@pytest.fixture
def sample_report():
"""Fixture to create a sample report object."""
return CreateReport(report_title="Test Report")


@pytest.fixture
def sample_figure():
"""Fixture to create a sample Plotly figure."""
return go.Figure(go.Bar(x=["A", "B", "C"], y=[1, 2, 3]))


def test_add_regular_figure(sample_report, sample_figure):
"""Test adding a regular Plotly figure."""
options = {"title": "Bar Chart"}
sample_report.add_figure(sample_figure, options)

assert len(sample_report.figures) == 1
assert sample_report.figures[0] == sample_figure


def test_add_figure_from_function(sample_report):
"""Test adding a figure returned by a function."""

def example_graph(options):
fig = go.Figure(go.Pie(labels=["A", "B", "C"], values=[30, 20, 50]))
fig.update_layout(title=options["title"])
return fig

options = {"title": "Pie Chart"}
sample_report.add_figure(example_graph, options)

assert len(sample_report.figures) == 1
assert sample_report.figures[0].layout.title.text == "Pie Chart"


def test_blend_graphs_to_html(sample_report, sample_figure, tmpdir):
"""Test blending graphs to an HTML report."""
options = {"title": "Bar Chart"}
sample_report.add_figure(sample_figure, options)

output_file = sample_report.blend_graphs_to_html()
assert os.path.isfile(output_file)

with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "Bar Chart" in content


def test_html_generation_content(sample_report, tmpdir):
"""Test the generated HTML file for expected content."""
fig = go.Figure(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]))
options = {"title": "Scatter Plot"}
sample_report.add_figure(fig, options)

output_file = sample_report.blend_graphs_to_html()
assert os.path.isfile(output_file)

with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "Scatter Plot" in content
assert "Test Report" in content


def test_empty_figures_list(sample_report, tmpdir):
"""Test blending graphs with no figures added."""
output_file = sample_report.blend_graphs_to_html()
assert os.path.isfile(output_file)

with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "Test Report" in content


def test_invalid_add_figure(sample_report):
"""Test adding invalid objects to the report."""
with pytest.raises(Exception): # Adjust the exception type if specific
sample_report.add_figure(123, {"title": "Invalid Figure"})


def test_large_number_of_figures(sample_report, tmpdir):
"""Test blending a large number of figures."""
for i in range(100): # Add 100 figures
fig = go.Figure(go.Bar(x=[i], y=[i]))
sample_report.add_figure(fig, {"title": f"Figure {i}"})

output_file = sample_report.blend_graphs_to_html()
assert os.path.isfile(output_file)

with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
assert "Figure 99" in content # Check if the last figure's title exists


def test_valid_html_output(sample_report, sample_figure, tmpdir):
"""Test the validity of the generated HTML."""
options = {"title": "Valid HTML Test"}
sample_report.add_figure(sample_figure, options)
output_file = sample_report.blend_graphs_to_html()

with open(output_file, "r", encoding="utf-8") as f:
soup = BeautifulSoup(f, "html.parser")

assert soup.find("div", {"class": "page"}) is not None # Check for page divs
assert soup.find("title").text == "Test Report" # Check the title


def test_integration_user_workflow():
"""Integration test simulating a user workflow of adding figures and generating a report."""
# Create a report
report = CreateReport(report_title="Integration Test Report")

# Add multiple figures
# Bar chart
bar_fig = go.Figure(go.Bar(x=["A", "B", "C"], y=[10, 20, 30]))
report.add_figure(bar_fig, {"title": "Bar Chart"})

# Scatter plot
scatter_fig = go.Figure(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="markers"))
report.add_figure(scatter_fig, {"title": "Scatter Plot"})

# Pie chart
pie_fig = go.Figure(
go.Pie(labels=["Apple", "Banana", "Cherry"], values=[30, 40, 30])
)
report.add_figure(pie_fig, {"title": "Pie Chart"})

# Step 3: Generate the HTML report
output_file = report.blend_graphs_to_html()
assert os.path.isfile(output_file), "The report HTML file was not created."

# Verify the report content
with open(output_file, "r", encoding="utf-8") as f:
content = f.read()
soup = BeautifulSoup(content, "html.parser")

# Check if all figures' titles are present
assert "Bar Chart" in content
assert "Scatter Plot" in content
assert "Pie Chart" in content

# Check if all div elements for pages are present
pages = soup.find_all("div", {"class": "page"})
assert len(pages) == 4 # 3 figures + 1 title page

0 comments on commit aef7446

Please sign in to comment.