Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(report): cover document gallery coercion
  • Loading branch information
mangeshraut712 committed Oct 30, 2026
commit f2df86123401abf10aaa02581cc05f0a05450799
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ Each release links to full notes on the

### Fixed

- **The HTML report gallery now accepts non-string `document_files` keys and
paths.** Integer and NumPy keys and `pathlib.Path` values no longer make the
gallery step of `generate_report()` fail without writing a page
([#454](https://github.com/awslabs/stickler/issues/454))

- **The HTML report's field performance table now escapes field names.** It
wrote each name into the page as raw HTML, while the chart beside it escaped
the same name, so a name containing `<`, `>` or `&` was read by the browser as
Expand Down
3 changes: 2 additions & 1 deletion src/stickler/reporting/html/section_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ def generate_document_gallery(document_images: Dict[str, str], config: ReportCon
for doc_id, image_path in document_images.items():
# Coerce before escape: html.escape requires str; int keys/paths
# (same shape as JSONL doc_id after 1.0.1) must not crash the gallery.
# str() also makes int 1001 and "1001" agree as the gallery key.
# Conversion can make distinct input keys render identically; preserve
# every ordered mapping entry rather than silently dropping one.
doc_id = str(doc_id)
image_path = str(image_path)
html_string += f'''
Expand Down
23 changes: 23 additions & 0 deletions tests/reporting/html/test_html_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ def test_generate_report_with_document_files(self, mock_makedirs, mock_file):

assert result.success is True
mock_copy_files.assert_called_once_with(document_files, self.output_path)

def test_generate_report_accepts_integer_document_file_key(self, tmp_path):
"""The public report API does not silently fail on an integer key (#454)."""
path = tmp_path / 'scan.png'
path.write_bytes(b'image data')
output_path = tmp_path / 'report.html'
config = ReportConfig(
include_executive_summary=False,
include_field_analysis=False,
include_non_matches=False,
include_confusion_matrix=False,
)

result = self.reporter.generate_report(
evaluation_results={},
output_path=str(output_path),
config=config,
document_files={1001: path},
)

assert result.success is True
assert output_path.is_file()
assert 'alt="1001"' in output_path.read_text(encoding='utf-8')

def test_copy_files_to_report_dir_success(self):
"""Test successful file copying to report directory."""
Expand Down
35 changes: 35 additions & 0 deletions tests/reporting/html/test_section_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
Tests for SectionGenerator class.
"""

from pathlib import Path
from unittest.mock import Mock, patch

import numpy as np

from stickler.reporting.html.report_config import ReportConfig
from stickler.reporting.html.section_generator import SectionGenerator
from stickler.reporting.html.visualization_engine import VisualizationEngine
Expand Down Expand Up @@ -372,6 +375,38 @@ def test_generate_document_gallery_pdf_mode_coerces_int_path(self):
assert 'data-pdf-path="1001"' in result
assert '<p><strong>doc-1</strong></p>' in result

def test_document_gallery_coerces_numpy_key_and_path_in_both_modes(self):
"""The gallery accepts the DataFrame-shaped key and Path value from #454."""
document_files = {np.int64(1001): Path('assets/scan.png')}

image_result = SectionGenerator.generate_document_gallery(
document_files, ReportConfig(document_file_type='image')
)
pdf_result = SectionGenerator.generate_document_gallery(
document_files, ReportConfig(document_file_type='pdf')
)

assert '<img src="assets/scan.png" alt="1001">' in image_result
assert 'data-doc-id="1001"' in pdf_result
assert 'data-pdf-path="assets/scan.png"' in pdf_result

def test_document_gallery_preserves_entries_with_colliding_string_keys(self):
"""Conversion preserves both ordered input entries when their keys collide."""
document_files = {1001: 'images/first.png', '1001': 'images/second.png'}

image_result = SectionGenerator.generate_document_gallery(
document_files, ReportConfig(document_file_type='image')
)
pdf_result = SectionGenerator.generate_document_gallery(
document_files, ReportConfig(document_file_type='pdf')
)

assert image_result.count('<div class="image-item">') == 2
assert image_result.index('images/first.png') < image_result.index('images/second.png')
assert image_result.count('alt="1001"') == 2
assert pdf_result.count('data-doc-id="1001"') == 2
assert pdf_result.index('images/first.png') < pdf_result.index('images/second.png')


class TestSectionGeneratorIntegration:
"""Integration tests for SectionGenerator with real data structures."""
Expand Down