| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import io
- import zipfile
- from openpyxl import load_workbook
- from app.services.export_attachment_zip import (
- build_attachment_zip_bytes,
- urls_to_attachment_items,
- )
- from app.utils.excel_export import ExportColumn
- def test_urls_to_attachment_items():
- items = urls_to_attachment_items("https://a/1.jpg,https://a/2.pdf")
- assert items == [
- {"url": "https://a/1.jpg", "label": "附件1"},
- {"url": "https://a/2.pdf", "label": "附件2"},
- ]
- def test_recharge_apply_zip_structure_and_hyperlink():
- columns = [
- ExportColumn(title="申请单号", field="applyNo"),
- ExportColumn(title="车牌号", field="licensePlate"),
- ]
- rows = [
- {
- "id": "r1",
- "rowNo": 1,
- "applyNo": "RA001",
- "licensePlate": "陕A12345",
- }
- ]
- attachments_by_id = {
- "r1": urls_to_attachment_items("https://cdn.example/a.jpg,https://cdn.example/b.pdf"),
- }
- def fake_download(url: str) -> bytes:
- return b"fake-bytes"
- stamp = "202609270900"
- data, failures = build_attachment_zip_bytes(
- db=None,
- tenant_no="t1",
- columns=columns,
- rows=rows,
- attachments_by_id=attachments_by_id,
- stamp=stamp,
- package_label="充值申请",
- id_field="id",
- sheet_title="充值申请",
- xlsx_basename="(总表)充值申请.xlsx",
- download_fn=fake_download,
- )
- assert failures == []
- with zipfile.ZipFile(io.BytesIO(data)) as zf:
- names = zf.namelist()
- assert f"充值申请({stamp})/(总表)充值申请.xlsx" in names
- att = [n for n in names if n.startswith(f"充值申请({stamp})/附件/")]
- assert len(att) == 2
- assert any("01_附件1_" in n for n in att)
- assert any("01_附件2_" in n for n in att)
- xlsx_bytes = zf.read(f"充值申请({stamp})/(总表)充值申请.xlsx")
- wb = load_workbook(io.BytesIO(xlsx_bytes))
- ws = wb.active
- headers = [c.value for c in ws[1]]
- assert "附件_1" in headers
- assert "附件_2" in headers
- attach_col = headers.index("附件_1") + 1
- assert str(ws.cell(2, attach_col).value).startswith("=HYPERLINK(")
|