-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
import pandas as pd | ||
from cellpack.autopack.writers import MarkdownWriter | ||
|
||
|
||
@pytest.fixture | ||
def setup_md_writer(tmp_path): | ||
title = "Test Report" | ||
output_path = tmp_path / "output" | ||
output_image_location = tmp_path / "images" | ||
report_name = "test_report.md" | ||
|
||
output_path.mkdir(parents=True, exist_ok=True) | ||
output_image_location.mkdir(parents=True, exist_ok=True) | ||
|
||
writer = MarkdownWriter(title, output_path, output_image_location, report_name) | ||
return writer, output_path / report_name | ||
|
||
|
||
def test_add_header(setup_md_writer): | ||
writer, report_path = setup_md_writer | ||
writer.add_header("Header Level 2", level=2) | ||
writer.write_file() | ||
|
||
with open(report_path, "r") as f: | ||
report = f.read() | ||
assert "# Header Level 2" in report | ||
|
||
|
||
def test_add_table(setup_md_writer): | ||
writer, report_path = setup_md_writer | ||
header = "Test Table" | ||
data = { | ||
"col1": [1, 2, 3], | ||
"col2": [4, 5, 6], | ||
} | ||
df = pd.DataFrame(data) | ||
writer.add_table(header, df) | ||
writer.write_file() | ||
|
||
with open(report_path, "r") as f: | ||
report = f.read() | ||
assert "Test Table" in report | ||
assert "|1|4|" in report | ||
|
||
|
||
def test_add_table_from_csv(setup_md_writer, tmp_path): | ||
writer, report_path = setup_md_writer | ||
header = "Test Table" | ||
data = { | ||
"col1": [5, 6], | ||
"col2": [7, 8], | ||
} | ||
df = pd.DataFrame(data) | ||
csv_path = tmp_path / "test_table.csv" | ||
df.to_csv(csv_path, index=False) | ||
|
||
writer.add_table_from_csv(header, csv_path) | ||
writer.write_file() | ||
|
||
with open(report_path, "r") as f: | ||
report = f.read() | ||
assert "Test Table" in report | ||
assert "|5|7|" in report | ||
|
||
|
||
def test_write_file(setup_md_writer): | ||
writer, report_path = setup_md_writer | ||
writer.add_header("Header Level 2", level=2) | ||
writer.add_header("Header Level 3", level=3) | ||
writer.add_header("Header Level 4", level=4) | ||
writer.write_file() | ||
|
||
with open(report_path, "r") as f: | ||
report = f.read() | ||
assert "# Header Level 2" in report | ||
assert "## Header Level 3" in report | ||
assert "### Header Level 4" in report | ||
|
||
|
||
def test_add_image(setup_md_writer, tmp_path): | ||
writer, report_path = setup_md_writer | ||
header = "Test Image" | ||
image_text = ["Image 1", "Image 2"] | ||
filepaths = ["image1.png", "image2.png"] | ||
|
||
for image in filepaths: | ||
(tmp_path / image).touch() | ||
|
||
writer.add_images(header, image_text, filepaths) | ||
writer.write_file() | ||
|
||
with open(report_path, "r") as f: | ||
report = f.read() | ||
assert "Test Image" in report | ||
assert "![Image 1]" in report | ||
assert "![Image 2]" in report | ||
assert "images/image1.png" in report |