excel2pic/tests/test_font_size.py

51 lines
1.4 KiB
Python

import pytest
from openpyxl import Workbook
from openpyxl.styles import Font
import io
from core.renderer import ExcelRenderer
from PIL import Image
@pytest.fixture
def font_size_excel_bytes():
wb = Workbook()
ws = wb.active
ws.title = "FontTest"
# Standard size
ws['A1'] = "Standard 12"
ws['A1'].font = Font(size=12)
# Large size
ws['A2'] = "Large 20"
ws['A2'].font = Font(size=20)
# Small size
ws['A3'] = "Small 8"
ws['A3'].font = Font(size=8)
out = io.BytesIO()
wb.save(out)
out.seek(0)
return out.getvalue()
def test_font_size_rendering(font_size_excel_bytes):
"""
Test that rendering handles different font sizes without crashing.
Note: Visual verification is hard in unit tests, but we can ensure
the code paths for dynamic font loading are executed.
"""
renderer = ExcelRenderer(font_size_excel_bytes)
try:
img_bytes = renderer.render_to_bytes(sheet_name="FontTest")
except Exception as e:
pytest.fail(f"Rendering failed with error: {e}")
assert isinstance(img_bytes, bytes)
img = Image.open(io.BytesIO(img_bytes))
assert img.format == "PNG"
# We can also inspect the internal cache to see if different fonts were loaded
# (Accessing private attribute for testing purpose)
assert len(renderer.font_cache) >= 3, "Should have cached at least 3 different font configurations"