from __future__ import annotations import sys import tempfile import unittest from pathlib import Path SCRIPTS = Path(__file__).resolve().parents[1] / "scripts" if str(SCRIPTS) not in sys.path: sys.path.insert(0, str(SCRIPTS)) import invoice_tool class InvoiceTicketCountTests(unittest.TestCase): def test_extract_ticket_count_from_recorded_invoice_page(self) -> None: self.assertEqual( invoice_tool.extract_invoice_ticket_count("发票信息\n开票数量\n2张\n提交"), 2, ) def test_extract_ticket_count_allows_one_ticket(self) -> None: self.assertEqual(invoice_tool.extract_invoice_ticket_count("开票数量:1 张"), 1) def test_extract_ticket_count_returns_none_when_page_has_no_count(self) -> None: self.assertIsNone(invoice_tool.extract_invoice_ticket_count("发票信息\n提交")) def test_extract_ticket_count_rejects_implausible_value(self) -> None: with self.assertRaisesRegex(RuntimeError, "异常"): invoice_tool.extract_invoice_ticket_count("开票数量 99张") def test_old_progress_csv_is_upgraded_without_losing_row_data(self) -> None: with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "progress.csv" path.write_text( "masked_name,real_name,id_last8,status,reason,updated_at\n" "张*三,张三,12345678,in_progress,,2026-07-12 21:00:00\n", encoding="utf-8-sig", ) rows = invoice_tool.read_progress(path) rows[0]["ticket_count"] = "2" invoice_tool.write_progress(rows, path) upgraded = invoice_tool.read_progress(path) self.assertEqual(upgraded[0]["real_name"], "张三") self.assertEqual(upgraded[0]["ticket_count"], "2") if __name__ == "__main__": unittest.main()