fix: support bilingual Booking Excel headers
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import date
|
||||
@@ -1077,5 +1078,26 @@ class ControlledDownloadTests(unittest.TestCase):
|
||||
reader.read(bad)
|
||||
|
||||
|
||||
class BookingHeaderI18nContractTests(unittest.TestCase):
|
||||
def test_booking_header_errors_have_three_locale_messages(self) -> None:
|
||||
script = (STATIC_ROOT / "i18n.js").read_text(encoding="utf-8")
|
||||
expected = {
|
||||
"BOOKING_EXCEL_HEADERS_MISSING": "error.booking_headers_missing",
|
||||
"BOOKING_EXCEL_HEADERS_AMBIGUOUS": "error.booking_headers_ambiguous",
|
||||
}
|
||||
for code, key in expected.items():
|
||||
self.assertIn(f'{code}: "{key}"', script)
|
||||
match = re.search(
|
||||
rf'^\s*"{re.escape(key)}": (\[.*\]),$',
|
||||
script,
|
||||
re.MULTILINE,
|
||||
)
|
||||
self.assertIsNotNone(match, key)
|
||||
values = json.loads(match.group(1))
|
||||
self.assertEqual(len(values), 3, key)
|
||||
self.assertTrue(all(values), key)
|
||||
self.assertRegex(values[2], r"[\u0e00-\u0e7f]", key)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -31,6 +31,37 @@ def valid_workbook() -> bytes:
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def bilingual_workbook(*, exact_headers: bool = True) -> bytes:
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
worksheet.title = "QBD"
|
||||
worksheet.append(["QBD export"])
|
||||
worksheet.append([])
|
||||
tour_header = "Tour Code / 主团号" if exact_headers else "Tour Code / 主团号(备用)"
|
||||
worksheet.append(
|
||||
[
|
||||
"Month",
|
||||
"NO.",
|
||||
tour_header,
|
||||
"Tour Days",
|
||||
"Raw Hotel Detail / วางข้อมูลที่นี่ / 酒店明细",
|
||||
]
|
||||
)
|
||||
worksheet.append(
|
||||
[
|
||||
"2026-08",
|
||||
1,
|
||||
"SYN-G1",
|
||||
"",
|
||||
"酒店 (【U-TWN8.5】 2,【U-เตียงเสริม13】 1)",
|
||||
]
|
||||
)
|
||||
output = io.BytesIO()
|
||||
workbook.save(output)
|
||||
workbook.close()
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
def summary(disposition: str = "imported_and_activated") -> BookingSourceSummary:
|
||||
return BookingSourceSummary(
|
||||
source_batch_id=7,
|
||||
@@ -199,6 +230,31 @@ class ProgramBookingSourceCoordinatorTests(unittest.TestCase):
|
||||
self.assertEqual(len(repository.created), 1)
|
||||
self.assertEqual(repository.created[0][1].original_filename, "Booking 报表.xlsx")
|
||||
|
||||
def test_bilingual_header_upload_is_stored_then_becomes_review_draft(self) -> None:
|
||||
store = FakeStore()
|
||||
repository = FakeRepository()
|
||||
coordinator = ProgramBookingSourceCoordinator(store, repository)
|
||||
|
||||
result = coordinator.submit("QBD 八月.xlsx", bilingual_workbook())
|
||||
|
||||
self.assertEqual(result["summary"]["filename"], "Booking 报表.xlsx")
|
||||
self.assertEqual(result["summary"]["pending_items"], 1)
|
||||
self.assertEqual(len(store.calls), 1)
|
||||
self.assertEqual(store.calls[0]["original_filename"], "QBD 八月.xlsx")
|
||||
self.assertEqual(len(repository.created), 1)
|
||||
|
||||
def test_unrecognized_bilingual_header_never_reaches_storage_or_database(self) -> None:
|
||||
store = FakeStore()
|
||||
repository = FakeRepository()
|
||||
coordinator = ProgramBookingSourceCoordinator(store, repository)
|
||||
|
||||
with self.assertRaises(PortalError) as raised:
|
||||
coordinator.submit("not.xlsx", bilingual_workbook(exact_headers=False))
|
||||
|
||||
self.assertEqual(raised.exception.code, "BOOKING_EXCEL_HEADERS_MISSING")
|
||||
self.assertEqual(store.calls, [])
|
||||
self.assertEqual(repository.created, [])
|
||||
|
||||
def test_reextracting_same_content_still_creates_a_fresh_draft(self) -> None:
|
||||
store = FakeStore()
|
||||
repository = FakeRepository(summary())
|
||||
|
||||
@@ -9,6 +9,7 @@ from openpyxl.styles import PatternFill
|
||||
|
||||
from booking_ingestion.excel import (
|
||||
BookingExcelError,
|
||||
PROCESSOR_VERSION,
|
||||
REVIEW_CONFIRMED,
|
||||
REVIEW_PENDING,
|
||||
build_excel_parse_result,
|
||||
@@ -113,6 +114,128 @@ class BookingExcelParserTests(unittest.TestCase):
|
||||
self.assertEqual(parsed["status"], "needs_review")
|
||||
self.assertEqual(len(parsed["room_items"]), 2)
|
||||
self.assertEqual(parsed["warnings"][0]["code"], "BOOKING_ROOM_TYPE_REVIEW_REQUIRED")
|
||||
self.assertEqual(parsed["processor_version"], "2.1.0")
|
||||
self.assertEqual(PROCESSOR_VERSION, "2.1.0")
|
||||
|
||||
def test_bilingual_headers_accept_lian_tai_and_qbd_variants(self) -> None:
|
||||
payload = workbook_bytes(
|
||||
[
|
||||
(
|
||||
"AUTO_CALC",
|
||||
[["BLOCK_CODE", "RATE_CODE"], ["LT-NEW", "RACK"]],
|
||||
),
|
||||
(
|
||||
"LianTai",
|
||||
[
|
||||
["Booking export"],
|
||||
[
|
||||
"Tour Code \n主团号",
|
||||
"Tour Days",
|
||||
"Notes",
|
||||
"วางข้อมูลที่นี่ / 酒店明细",
|
||||
],
|
||||
["LT-NEW", "", "", "酒店 (【U-TWN8.5】 2)"],
|
||||
],
|
||||
),
|
||||
(
|
||||
"QBD",
|
||||
[
|
||||
["QBD export"],
|
||||
[None],
|
||||
[
|
||||
"Month",
|
||||
"NO.",
|
||||
"Tour Code \n主团号",
|
||||
"Tour Days",
|
||||
"Raw Hotel Detail / วางข้อมูลที่นี่ / 酒店明细",
|
||||
],
|
||||
["", 1, "QBD-NEW", "", "酒店 (【U-DBL12】 3)"],
|
||||
],
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
document = parse_excel_bytes(payload)
|
||||
|
||||
self.assertEqual(document.worksheets, ("LianTai", "QBD"))
|
||||
self.assertEqual(
|
||||
[row.group_code_raw for row in document.rows], ["LT-NEW", "QBD-NEW"]
|
||||
)
|
||||
self.assertEqual(document.extracted_item_count, 2)
|
||||
self.assertEqual(document.pending_item_count, 0)
|
||||
|
||||
def test_legacy_tour_code_aliases_remain_supported(self) -> None:
|
||||
for tour_header in (
|
||||
"Tour Code",
|
||||
"Group Code",
|
||||
"Gourp Code",
|
||||
"团号",
|
||||
"团队代码",
|
||||
):
|
||||
with self.subTest(tour_header=tour_header):
|
||||
payload = workbook_bytes(
|
||||
[
|
||||
(
|
||||
"Legacy",
|
||||
[
|
||||
[tour_header, "โรงแรม"],
|
||||
["LEGACY", "酒店 (【U-TWN8.5】 1)"],
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
document = parse_excel_bytes(payload)
|
||||
|
||||
self.assertEqual(document.worksheets, ("Legacy",))
|
||||
self.assertEqual(document.rows[0].group_code_raw, "LEGACY")
|
||||
|
||||
def test_lookalike_bilingual_header_is_rejected(self) -> None:
|
||||
payload = workbook_bytes(
|
||||
[
|
||||
(
|
||||
"Data",
|
||||
[
|
||||
[
|
||||
"Tour Code / 主团号(备用)",
|
||||
"Raw Hotel Detail / วางข้อมูลที่นี่ / 酒店明细",
|
||||
],
|
||||
["SYN", "酒店 (【U-TWN8.5】 1)"],
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
with self.assertRaises(BookingExcelError) as raised:
|
||||
parse_excel_bytes(payload)
|
||||
|
||||
self.assertEqual(raised.exception.code, "BOOKING_EXCEL_HEADERS_MISSING")
|
||||
self.assertIn("Tour Code/主团号", raised.exception.safe_message)
|
||||
self.assertIn("酒店明细", raised.exception.safe_message)
|
||||
|
||||
def test_duplicate_recognized_headers_are_ambiguous(self) -> None:
|
||||
payload = workbook_bytes(
|
||||
[
|
||||
(
|
||||
"Data",
|
||||
[
|
||||
[
|
||||
"Tour Code",
|
||||
"Tour Code / 主团号",
|
||||
"วางข้อมูลที่นี่ / 酒店明细",
|
||||
],
|
||||
["SYN", "SYN", "酒店 (【U-TWN8.5】 1)"],
|
||||
],
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
with self.assertRaises(BookingExcelError) as raised:
|
||||
parse_excel_bytes(payload)
|
||||
|
||||
self.assertEqual(raised.exception.code, "BOOKING_EXCEL_HEADERS_AMBIGUOUS")
|
||||
self.assertIn("Tour Code/主团号", raised.exception.safe_message)
|
||||
self.assertIn("酒店明细", raised.exception.safe_message)
|
||||
|
||||
def test_physically_last_tour_row_replaces_earlier_and_cancel_removes_it(self) -> None:
|
||||
payload = booking_rows(
|
||||
|
||||
Reference in New Issue
Block a user