"""Tests for the validator module - no LLM calls required.""" import pytest from decimal import Decimal from src.schemas import DocumentExtraction, LineItem, ValidationResult from src.validator import ( validate_line_items, validate_subtotal, validate_total, validate_dates, validate_required_fields, run_all_validations, ) def make_line_item(description, qty, price, total=None): """Helper to create a LineItem with decimal values.""" item = LineItem( description=description, quantity=str(qty), unit_price=str(price), line_total=str(total) if total is not None else None, ) item.quantity_decimal = Decimal(str(qty)) item.unit_price_decimal = Decimal(str(price)) if total is not None: item.line_total_decimal = Decimal(str(total)) return item def make_extraction(**kwargs) -> DocumentExtraction: """Helper to create a DocumentExtraction with decimal fields.""" e = DocumentExtraction(**kwargs) if e.subtotal: e.subtotal_decimal = Decimal(e.subtotal) if e.tax: e.tax_decimal = Decimal(e.tax) if e.discount: e.discount_decimal = Decimal(e.discount) if e.shipping: e.shipping_decimal = Decimal(e.shipping) if e.total: e.total_decimal = Decimal(e.total) return e class TestValidateLineItems: def test_correct_line_item(self): items = [make_line_item("Laptop Stand", 2, 1500, 3000)] checks = validate_line_items(items) assert len(checks) == 1 assert checks[0].passed is True def test_incorrect_line_item(self): items = [make_line_item("Keyboard", 3, 2000, 7000)] # Should be 6000 checks = validate_line_items(items) assert len(checks) == 1 assert checks[0].passed is False assert checks[0].severity == "ERROR" assert checks[0].difference == "1000.00" def test_multiple_items_mixed(self): items = [ make_line_item("Item A", 2, 1500, 3000), # Correct make_line_item("Item B", 3, 2000, 5000), # Wrong (should be 6000) ] checks = validate_line_items(items) assert checks[0].passed is True assert checks[1].passed is False def test_missing_line_total(self): items = [make_line_item("Item", 2, 100)] # no total checks = validate_line_items(items) assert checks[0].passed is True # INFO, not fail assert checks[0].severity == "INFO" def test_missing_quantity(self): item = LineItem(description="Item", unit_price="100", line_total="200") # No decimal set checks = validate_line_items([item]) assert checks[0].passed is True # Skipped def test_tolerance(self): # Difference of exactly 0.01 should pass items = [make_line_item("Item", 2, 1500, Decimal("3000.01"))] checks = validate_line_items(items) assert checks[0].passed is True class TestValidateSubtotal: def test_matching_subtotal(self): items = [ make_line_item("A", 2, 1500, 3000), make_line_item("B", 3, 2000, 6000), make_line_item("C", 2, 1250, 2500), ] ext = make_extraction( document_type="invoice", subtotal="11500", total="13570", ) ext.line_items = items check = validate_subtotal(ext) assert check is not None assert check.passed is True def test_mismatching_subtotal(self): items = [make_line_item("A", 2, 1500, 3000)] ext = make_extraction(document_type="invoice", subtotal="5000", total="5500") ext.line_items = items check = validate_subtotal(ext) assert check.passed is False def test_no_line_items(self): ext = make_extraction(document_type="invoice", subtotal="11500", total="13570") check = validate_subtotal(ext) assert check is None class TestValidateTotal: def test_correct_total(self): ext = make_extraction( document_type="invoice", subtotal="11500", tax="2070", discount="0", shipping="0", total="13570", ) check = validate_total(ext) assert check.passed is True def test_incorrect_total(self): # The main demo case: correct calc = 13570, document says 14000 ext = make_extraction( document_type="invoice", subtotal="11500", tax="2070", discount=None, shipping=None, total="14000", ) check = validate_total(ext) assert check.passed is False assert check.severity == "ERROR" assert check.expected == "13570.00" assert check.actual == "14000.00" assert check.difference == "430.00" def test_missing_subtotal(self): ext = make_extraction(document_type="invoice", total="14000") check = validate_total(ext) assert check.passed is True # Skipped assert check.severity == "INFO" def test_with_discount(self): ext = make_extraction( document_type="invoice", subtotal="10000", tax="1800", discount="500", shipping="200", total="11500", ) check = validate_total(ext) assert check.passed is True def test_tolerance_boundary(self): ext = make_extraction( document_type="invoice", subtotal="11500", tax="2070", total="13570.00", ) check = validate_total(ext) assert check.passed is True class TestValidateDates: def test_valid_document_date(self): ext = make_extraction(document_type="invoice", document_date="2026-08-18", total="1000") checks = validate_dates(ext) date_check = next(c for c in checks if c.name == "date_validation") assert date_check.passed is True def test_due_date_after_doc_date(self): ext = make_extraction( document_type="invoice", document_date="2026-08-18", due_date="2026-09-17", total="1000", ) checks = validate_dates(ext) due_check = next((c for c in checks if c.name == "due_date_validation"), None) assert due_check is not None assert due_check.passed is True def test_due_date_before_doc_date(self): ext = make_extraction( document_type="invoice", document_date="2026-08-18", due_date="2026-07-01", total="1000", ) checks = validate_dates(ext) due_check = next((c for c in checks if c.name == "due_date_validation"), None) assert due_check is not None assert due_check.passed is False assert due_check.severity == "WARNING" def test_missing_date(self): ext = make_extraction(document_type="invoice", total="1000") checks = validate_dates(ext) assert len(checks) >= 1 class TestValidateRequiredFields: def test_valid_invoice(self): ext = make_extraction( document_type="invoice", document_id="INV-001", vendor="Test Vendor", document_date="2026-08-18", total="1000", ) ext.line_items = [make_line_item("Item", 1, 1000, 1000)] checks = validate_required_fields(ext) error_checks = [c for c in checks if not c.passed and c.severity == "ERROR"] assert len(error_checks) == 0 def test_missing_required_invoice_field(self): ext = make_extraction( document_type="invoice", # missing document_id, vendor, total document_date="2026-08-18", ) checks = validate_required_fields(ext) error_checks = [c for c in checks if not c.passed and c.severity == "ERROR"] assert len(error_checks) >= 2 # document_id, vendor, total missing def test_unknown_type_no_required(self): ext = make_extraction(document_type="unknown") checks = validate_required_fields(ext) assert len(checks) == 0 class TestRunAllValidations: def test_valid_invoice_passes(self): items = [ make_line_item("Laptop Stand", 2, 1500, 3000), make_line_item("Keyboard", 3, 2000, 6000), make_line_item("USB Hub", 2, 1250, 2500), ] ext = make_extraction( document_type="invoice", document_id="INV-2026-1042", vendor="NovaTech Supplies Pvt. Ltd.", customer="Vertex Office Solutions", document_date="2026-08-18", due_date="2026-09-17", subtotal="11500", tax="2070", discount="0", shipping="0", total="13570", ) ext.line_items = items result = run_all_validations(ext) assert result.overall_status == "VALID" assert result.is_valid is True assert len(result.errors) == 0 def test_invalid_total_fails(self): items = [ make_line_item("Laptop Stand", 2, 1500, 3000), make_line_item("Keyboard", 3, 2000, 6000), make_line_item("USB Hub", 2, 1250, 2500), ] ext = make_extraction( document_type="invoice", document_id="INV-2026-1099", vendor="NovaTech Supplies Pvt. Ltd.", document_date="2026-08-18", subtotal="11500", tax="2070", total="14000", # WRONG - should be 13570 ) ext.line_items = items result = run_all_validations(ext) assert result.overall_status == "INVALID" assert result.is_valid is False assert len(result.errors) > 0 total_check = next(c for c in result.checks if c.name == "total_reconciliation") assert total_check.difference == "430.00"