File size: 2,856 Bytes
1041734 e7b4937 1041734 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | """
Generate test fixtures for file parser tests
Author: @mangubee
"""
from pathlib import Path
# ============================================================================
# CONFIG
# ============================================================================
FIXTURES_DIR = Path(__file__).parent
# ============================================================================
# Generate PDF
# ============================================================================
def generate_pdf():
"""Generate sample PDF file using fpdf"""
try:
from fpdf import FPDF
except ImportError:
print("Skipping PDF generation (fpdf not installed)")
return
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Test PDF Document", ln=True)
pdf.cell(200, 10, txt="This is page 1 content.", ln=True)
pdf.add_page()
pdf.cell(200, 10, txt="Page 2", ln=True)
pdf.cell(200, 10, txt="This is page 2 content.", ln=True)
pdf_path = FIXTURES_DIR / "sample.pdf"
pdf.output(str(pdf_path))
print(f"Created: {pdf_path}")
# ============================================================================
# Generate Excel
# ============================================================================
def generate_excel():
"""Generate sample Excel file"""
from openpyxl import Workbook
wb = Workbook()
# Sheet 1
ws1 = wb.active
ws1.title = "Data"
ws1.append(["Product", "Price", "Quantity"])
ws1.append(["Apple", 1.50, 100])
ws1.append(["Banana", 0.75, 150])
ws1.append(["Orange", 2.00, 80])
# Sheet 2
ws2 = wb.create_sheet("Summary")
ws2.append(["Total Products", 3])
ws2.append(["Total Quantity", 330])
excel_path = FIXTURES_DIR / "sample.xlsx"
wb.save(excel_path)
print(f"Created: {excel_path}")
# ============================================================================
# Generate Word
# ============================================================================
def generate_word():
"""Generate sample Word document"""
from docx import Document
doc = Document()
doc.add_heading("Test Word Document", 0)
doc.add_paragraph("This is the first paragraph.")
doc.add_paragraph("This is the second paragraph with some content.")
doc.add_heading("Section 2", level=1)
doc.add_paragraph("Content in section 2.")
word_path = FIXTURES_DIR / "sample.docx"
doc.save(word_path)
print(f"Created: {word_path}")
# ============================================================================
# Main
# ============================================================================
if __name__ == "__main__":
print("Generating test fixtures...")
generate_pdf()
generate_excel()
generate_word()
print("All fixtures generated successfully!")
|