"""Unit tests for the regex/heuristic parsing logic in paperpod.ocr.extract. These test the parsing functions directly on strings so they stay deterministic and don't depend on Tesseract's OCR accuracy. End-to-end OCR accuracy against rendered receipts is exercised in test_ocr_integration.py. """ from paperpod.ocr.extract import extract_date, extract_total def test_extract_date_iso(): assert extract_date("2023-03-14 09:41 #4821") == "2023-03-14" def test_extract_date_slash_mdy(): assert extract_date("Purchased on 03/14/2023 at noon") == "2023-03-14" def test_extract_date_month_name(): assert extract_date("Invoice date: March 14, 2023") == "2023-03-14" assert extract_date("Mar 14 2023") == "2023-03-14" def test_extract_date_none_when_absent(): assert extract_date("no date here, just text") is None def test_extract_date_rejects_implausible_values(): # Not a real month/day -> not treated as a date. assert extract_date("13/45/2023") is None def test_extract_total_basic(): text = "SUBTOTAL 72.14\nHST 13% 9.38\nTOTAL 81.52\n" assert extract_total(text) == "81.52" def test_extract_total_ignores_subtotal(): text = "SUBTOTAL 30.63\n" assert extract_total(text) is None def test_extract_total_word_boundary_with_prefix(): # "FUEL TOTAL" should still match "total" as its own word. text = "FUEL TOTAL 65.10\n" assert extract_total(text) == "65.10" def test_extract_total_amount_on_next_line(): text = "TOTAL\n81.52\n" assert extract_total(text) == "81.52" def test_extract_total_none_when_absent(): assert extract_total("no totals here") is None