Files
CustomBingo/tests/test_pdf_generator.py

93 lines
3.1 KiB
Python

import pytest
import os
import tempfile
from custom_bingo.pdf_generator import export_cards_to_pdf, draw_multiline_text, wrap_text_to_fit_box
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
def test_wrap_text_to_fit_box():
"""Test wrapping text to fit within specified dimensions."""
# Test with text that fits in one line
lines, font_size = wrap_text_to_fit_box("Short", 2*inch, 0.5*inch, "Helvetica", 12)
assert len(lines) == 1
assert lines[0] == "Short"
assert font_size <= 12
# Test with text that needs to be wrapped
lines, font_size = wrap_text_to_fit_box("This is a very long text that should be wrapped",
1*inch, 0.5*inch, "Helvetica", 12)
assert len(lines) >= 1 # Might be multiple lines depending on width
assert font_size <= 12
def test_wrap_text_to_fit_box_empty():
"""Test wrapping empty text."""
lines, font_size = wrap_text_to_fit_box("", 2*inch, 0.5*inch, "Helvetica", 12)
assert lines == []
assert font_size == 12
def test_export_cards_to_pdf():
"""Test exporting cards to PDF."""
# Create a simple test card
test_cards = [[
["B1", "I1", "N1", "G1", "O1"],
["B2", "I2", "N2", "G2", "O2"],
["B3", "I3", "FREE", "G3", "O3"], # Free space in center
["B4", "I4", "N4", "G4", "O4"],
["B5", "I5", "N5", "G5", "O5"]
]]
# Create a temporary output file
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as f:
temp_output_file = f.name
try:
# This should not raise an exception
export_cards_to_pdf(test_cards, temp_output_file)
# Check that the file was created and is not empty
assert os.path.exists(temp_output_file)
assert os.path.getsize(temp_output_file) > 0
finally:
# Clean up
if os.path.exists(temp_output_file):
os.remove(temp_output_file)
def test_export_cards_to_pdf_multiple():
"""Test exporting multiple cards to PDF."""
# Create two test cards
test_cards = [
[
["B1", "I1", "N1", "G1", "O1"],
["B2", "I2", "N2", "G2", "O2"],
["B3", "I3", "FREE", "G3", "O3"],
["B4", "I4", "N4", "G4", "O4"],
["B5", "I5", "N5", "G5", "O5"]
],
[
["B6", "I6", "N6", "G6", "O6"],
["B7", "I7", "N7", "G7", "O7"],
["B8", "I8", "FREE", "G8", "O8"],
["B9", "I9", "N9", "G9", "O9"],
["B10", "I10", "N10", "G10", "O10"]
]
]
# Create a temporary output file
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as f:
temp_output_file = f.name
try:
# This should not raise an exception
export_cards_to_pdf(test_cards, temp_output_file)
# Check that the file was created and is not empty
assert os.path.exists(temp_output_file)
assert os.path.getsize(temp_output_file) > 0
finally:
# Clean up
if os.path.exists(temp_output_file):
os.remove(temp_output_file)