48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import pytest
|
|
from openpyxl import Workbook
|
|
import io
|
|
from core.renderer import ExcelRenderer
|
|
from PIL import Image
|
|
|
|
@pytest.fixture
|
|
def sample_excel_bytes():
|
|
wb = Workbook()
|
|
ws = wb.active
|
|
ws.title = "TestSheet"
|
|
ws['A1'] = "Hello"
|
|
ws['B1'] = "World"
|
|
ws['A2'] = 123
|
|
ws['B2'] = 456.78
|
|
|
|
# Add some color
|
|
from openpyxl.styles import PatternFill
|
|
fill = PatternFill(start_color="FFFF0000", end_color="FFFF0000", fill_type="solid")
|
|
ws['A1'].fill = fill
|
|
|
|
out = io.BytesIO()
|
|
wb.save(out)
|
|
out.seek(0)
|
|
return out.getvalue()
|
|
|
|
def test_renderer_initialization(sample_excel_bytes):
|
|
renderer = ExcelRenderer(sample_excel_bytes)
|
|
assert renderer is not None
|
|
|
|
def test_render_to_bytes(sample_excel_bytes):
|
|
renderer = ExcelRenderer(sample_excel_bytes)
|
|
img_bytes = renderer.render_to_bytes(sheet_name="TestSheet")
|
|
|
|
assert isinstance(img_bytes, bytes)
|
|
assert len(img_bytes) > 0
|
|
|
|
# Verify it's a valid image
|
|
img = Image.open(io.BytesIO(img_bytes))
|
|
assert img.format == "PNG"
|
|
assert img.width > 0
|
|
assert img.height > 0
|
|
|
|
def test_render_invalid_sheet(sample_excel_bytes):
|
|
renderer = ExcelRenderer(sample_excel_bytes)
|
|
with pytest.raises(ValueError, match="Sheet 'NonExistent' not found"):
|
|
renderer.render_to_bytes(sheet_name="NonExistent")
|