Spaces:
Sleeping
Sleeping
Download tests/test_form_logic.py from procurement-tools/auto-form: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/procurement-tools/auto-form/resolve/main/tests/test_form_logic.py
- Command line
-
hf download hf://spaces/procurement-tools/auto-form/tests/test_form_logic.py
-
curl -L -o test_form_logic.py https://huggingface.co/spaces/procurement-tools/auto-form/resolve/main/tests/test_form_logic.py
3.31 kB
| # -*- coding: utf-8 -*- | |
| """form_logic — pure docx/text helpers.""" | |
| import datetime | |
| from docx import Document | |
| import form_logic | |
| # --- clean_vendor_name ------------------------------------------------------- | |
| def test_clean_vendor_name_strips_sap_code(): | |
| assert form_logic.clean_vendor_name( | |
| "0010006270 บริษัท ลินเด้ (ประเทศไทย) จำกัด (มหาชน)" | |
| ) == "บริษัท ลินเด้ (ประเทศไทย) จำกัด (มหาชน)" | |
| def test_clean_vendor_name_passthrough(): | |
| assert form_logic.clean_vendor_name("บริษัท ทดสอบ จำกัด") == "บริษัท ทดสอบ จำกัด" | |
| assert form_logic.clean_vendor_name(None) == "" | |
| assert form_logic.clean_vendor_name("") == "" | |
| def test_clean_vendor_name_all_digits_kept(): | |
| # stripping would leave nothing → the original value is returned | |
| assert form_logic.clean_vendor_name("1234567890") == "1234567890" | |
| # --- signer line ------------------------------------------------------------- | |
| def test_center_text_in_parens_width(): | |
| out = form_logic.center_text_in_parens("นายทดสอบ", total_width=44) | |
| assert out.startswith("(") and out.endswith(")") | |
| assert len(out) == 46 # 44 inner + the two parens | |
| inner = out[1:-1] | |
| assert inner.strip() == "นายทดสอบ" | |
| left = len(inner) - len(inner.lstrip()) | |
| right = len(inner) - len(inner.rstrip()) | |
| assert abs(left - right) <= 1 # centered | |
| def test_center_text_in_parens_overwidth_passthrough(): | |
| name = "x" * 50 | |
| assert form_logic.center_text_in_parens(name, total_width=44) == f"({name})" | |
| # --- docx helpers (in-memory documents) -------------------------------------- | |
| def test_fill_letter_date(): | |
| doc = Document() | |
| doc.add_paragraph("วันที่ เดือน พ.ศ.") | |
| assert form_logic.fill_letter_date(doc, datetime.date(2026, 7, 16)) is True | |
| assert "พ.ศ. 2569" in doc.paragraphs[0].text | |
| assert "กรกฎาคม" in doc.paragraphs[0].text | |
| def test_fill_letter_date_no_line(): | |
| doc = Document() | |
| doc.add_paragraph("ไม่มีบรรทัดวันที่") | |
| assert form_logic.fill_letter_date(doc, datetime.date(2026, 7, 16)) is False | |
| def test_replace_text_across_split_runs(): | |
| doc = Document() | |
| p = doc.add_paragraph() | |
| p.add_run("เลขที่ PO_") | |
| p.add_run("No ครับ") # placeholder split across two runs | |
| n = form_logic.replace_text(doc, "PO_No", "4500987654") | |
| assert n == 1 | |
| assert p.text == "เลขที่ 4500987654 ครับ" | |
| def test_insert_body_paragraphs(): | |
| doc = Document() | |
| doc.add_paragraph("หัวเรื่อง") | |
| doc.add_paragraph("[เนื้อความ]") | |
| n = form_logic.insert_body_paragraphs(doc, "[เนื้อความ]", "ย่อหน้าหนึ่ง\n\nย่อหน้าสอง") | |
| assert n == 2 | |
| texts = [p.text for p in doc.paragraphs] | |
| assert "[เนื้อความ]" not in " ".join(texts) | |
| assert "ย่อหน้าหนึ่ง" in texts and "ย่อหน้าสอง" in texts | |