File size: 2,814 Bytes
0e39d80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Pipeline tests for the EasyOCR + Gemini document service."""

import json
import types
from pathlib import Path

import pytest

from ml_utils.confidence import build_score_breakdown, decide_status
from services.experience_service import verify_experience

BACKEND = Path(__file__).resolve().parent.parent
TEST_DOCS = BACKEND.parent / "test_documents"
FIXTURES = Path(__file__).parent / "fixtures"


class TestDecisionEngine:
    def test_critical_flag_rejects(self):
        status = decide_status(95.0, ["VERHOEFF_CHECKSUM_FAILED"])
        assert status == "Rejected"

    def test_high_score_verified(self):
        status = decide_status(92.0, [])
        assert status == "Auto-Verified"

    def test_score_breakdown_caps_at_100(self):
        breakdown = build_score_breakdown([], {"name": "x", "pan_number": "y"}, "pan", 20.0, 8.0, 7.0)
        assert breakdown["overall"] <= 100.0
        assert "ocr_quality" in breakdown


class TestCastePdf:
    def test_caste_sample_pdf_text(self):
        from ml_utils.text_extractor import extract_plain_text

        path = TEST_DOCS / "caste_certificate.pdf"
        if not path.exists():
            path = FIXTURES / "caste_sample.pdf"
        if not path.exists():
            pytest.skip("caste PDF fixture missing")
        text, source = extract_plain_text(str(path))
        assert source == "native"
        assert "CASTE" in text.upper()


class TestExperience:
    def test_experience_pdf_with_email_text(self):
        from ml_utils.text_extractor import extract_plain_text

        path = TEST_DOCS / "experience_letter.pdf"
        if not path.exists():
            pytest.skip("experience_letter.pdf missing")
        text, _ = extract_plain_text(str(path))
        assert "TechNova" in text or "EXPERIENCE" in text.upper()
        assert "@" in text

    def test_experience_pdf_no_email_text(self):
        from ml_utils.text_extractor import extract_plain_text

        path = TEST_DOCS / "experience_letter_no_email.pdf"
        if not path.exists():
            pytest.skip("experience_letter_no_email.pdf missing")
        text, _ = extract_plain_text(str(path))
        assert "@" not in text

    def test_experience_demo_mode_email(self):
        doc = types.SimpleNamespace(
            extracted_fields=json.dumps({"hr_email": "hr@acme.com", "company_name": "Acme", "employee_name": "John"}),
            flags=json.dumps([]),
        )
        outcome = verify_experience(doc)
        assert outcome.get("demo_mode") is True
        assert "DEMO_MODE_EMAIL" in outcome["flags"]


class TestUploadConfig:
    def test_only_image_extensions_accepted(self):
        from config import ACCEPTED_EXTENSIONS

        for doc_type, exts in ACCEPTED_EXTENSIONS.items():
            assert exts == {".jpg", ".jpeg", ".png", ".pdf"}, doc_type