70 lines
2.8 KiB
Python
70 lines
2.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from app.api.deps import CurrentUserContext
|
||
|
|
from app.core.config import get_settings
|
||
|
|
from app.schemas.ocr import OcrRecognizeDocumentRead
|
||
|
|
from app.services.receipt_folder import ReceiptFolderService
|
||
|
|
|
||
|
|
|
||
|
|
def test_receipt_folder_train_ticket_uses_invoice_date_and_enriches_fields(monkeypatch, tmp_path) -> None:
|
||
|
|
monkeypatch.setenv("STORAGE_ROOT_DIR", str(tmp_path / "storage"))
|
||
|
|
get_settings.cache_clear()
|
||
|
|
try:
|
||
|
|
current_user = CurrentUserContext(
|
||
|
|
username="pytest",
|
||
|
|
name="Py Test",
|
||
|
|
role_codes=[],
|
||
|
|
is_admin=False,
|
||
|
|
)
|
||
|
|
service = ReceiptFolderService()
|
||
|
|
receipt = service.save_receipt(
|
||
|
|
filename="2月23_上海-武汉.pdf",
|
||
|
|
content=b"%PDF-1.4 fake",
|
||
|
|
media_type="application/pdf",
|
||
|
|
current_user=current_user,
|
||
|
|
document=OcrRecognizeDocumentRead(
|
||
|
|
filename="2月23_上海-武汉.pdf",
|
||
|
|
media_type="application/pdf",
|
||
|
|
text=(
|
||
|
|
"电子发票(铁路电子客票)\n"
|
||
|
|
"发票号码:26319166100006175398\n"
|
||
|
|
"电子客票号:E1234567890123\n"
|
||
|
|
"开票日期:2026-02-18\n"
|
||
|
|
"上海虹桥站\n"
|
||
|
|
"武汉站\n"
|
||
|
|
"G456\n"
|
||
|
|
"二等座\n"
|
||
|
|
"06车01B号\n"
|
||
|
|
"2026-02-20 08:30开\n"
|
||
|
|
"票价:¥354.00\n"
|
||
|
|
"1101011990****1234\n"
|
||
|
|
"张三"
|
||
|
|
),
|
||
|
|
summary="铁路电子客票,上海虹桥至武汉,票价 354 元。",
|
||
|
|
document_type="train_ticket",
|
||
|
|
document_type_label="火车/高铁票",
|
||
|
|
scene_code="travel",
|
||
|
|
scene_label="差旅票据",
|
||
|
|
),
|
||
|
|
)
|
||
|
|
|
||
|
|
assert receipt.document_date == "2026-02-18"
|
||
|
|
assert receipt.merchant_name == "中国铁路"
|
||
|
|
assert receipt.amount == "354.00元"
|
||
|
|
|
||
|
|
detail = service.get_receipt(receipt.id, current_user)
|
||
|
|
fields = {field.label: field.value for field in detail.fields}
|
||
|
|
assert fields["开票日期"] == "2026-02-18"
|
||
|
|
assert fields["乘车人"] == "张三"
|
||
|
|
assert fields["出发地点"] == "上海虹桥"
|
||
|
|
assert fields["到达地点"] == "武汉"
|
||
|
|
assert fields["车次"] == "G456"
|
||
|
|
assert fields["电子客票号"] == "E1234567890123"
|
||
|
|
assert fields["身份证号"] == "1101011990****1234"
|
||
|
|
assert fields["席别"] == "二等座"
|
||
|
|
assert fields["车厢"] == "06车"
|
||
|
|
assert fields["座位号"] == "01B"
|
||
|
|
assert fields["列车出发时间"] == "2026-02-20 08:30"
|
||
|
|
finally:
|
||
|
|
get_settings.cache_clear()
|