-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ocr_processor.py
More file actions
81 lines (58 loc) · 2.61 KB
/
Copy pathtest_ocr_processor.py
File metadata and controls
81 lines (58 loc) · 2.61 KB
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
"""
Tests for OCRProcessor — focuses on the regex extraction logic
which can run without Tesseract installed.
"""
import pytest
from src.ocr_processor import OCRProcessor, ExtractionResult
@pytest.fixture
def processor():
return OCRProcessor()
class TestURNExtraction:
def test_standard_urn_format(self, processor):
text = "Patient URN-2024-001234 admitted on 01/03/2024"
result = processor._extract_fields(text, page_count=1)
assert "URN-2024-001234" in result.urns
def test_urn_with_spaces(self, processor):
text = "Ref: URN 2024 005678"
result = processor._extract_fields(text, page_count=1)
assert len(result.urns) > 0
def test_patient_id_label(self, processor):
text = "Patient ID: ABC1234567"
result = processor._extract_fields(text, page_count=1)
assert len(result.urns) > 0
def test_no_urn_returns_empty(self, processor):
text = "This document has no patient identifiers."
result = processor._extract_fields(text, page_count=1)
assert result.urns == []
assert not result.success
def test_deduplication(self, processor):
text = "URN-2024-001234 ... URN-2024-001234 appears twice"
result = processor._extract_fields(text, page_count=1)
assert result.urns.count("URN-2024-001234") == 1
class TestDateExtraction:
def test_slash_date_format(self, processor):
text = "Admission date: 15/03/2024"
result = processor._extract_fields(text, page_count=1)
assert any("15" in d and "2024" in d for d in result.dates)
def test_written_date_format(self, processor):
text = "Discharged 3 April 2024"
result = processor._extract_fields(text, page_count=1)
assert len(result.dates) > 0
class TestConfidence:
def test_empty_text_zero_confidence(self, processor):
result = processor._extract_fields("", page_count=1)
assert result.confidence == 0.0
def test_dense_text_high_confidence(self, processor):
text = " ".join(["word"] * 300)
result = processor._extract_fields(text, page_count=1)
assert result.confidence == 1.0
class TestExtractionResult:
def test_success_requires_urns_and_no_errors(self):
r = ExtractionResult(raw_text="x", urns=["URN-001"], errors=[])
assert r.success is True
def test_failure_with_errors(self):
r = ExtractionResult(raw_text="", errors=["File not found"])
assert r.success is False
def test_failure_without_urns(self):
r = ExtractionResult(raw_text="some text", urns=[], errors=[])
assert r.success is False