Spaces:
Sleeping
Sleeping
File size: 9,082 Bytes
de76d9d | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | """
Test Suite for Document Processing Pipeline
Tests each module independently and the full pipeline.
"""
import io
import sys
import os
import pytest
# Ensure the project root is in path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# ββ Text Cleaner Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestTextCleaner:
def setup_method(self):
from app.modules.text_cleaner import TextCleaner
self.cleaner = TextCleaner()
def test_basic_cleaning(self):
text = "Hello World\n\n\n\nThis is a test."
result = self.cleaner.clean(text)
assert "Hello World" in result
assert "\n\n\n" not in result
def test_ligature_replacement(self):
text = "\ufb01le \ufb02ow" # fi fl ligatures
result = self.cleaner.clean(text)
assert "file flow" in result
def test_noise_removal(self):
text = "Important text\n1\n---\nMore text"
result = self.cleaner.clean(text)
assert "Important text" in result
assert "More text" in result
def test_empty_input(self):
assert self.cleaner.clean("") == ""
assert self.cleaner.clean(" ") == ""
# ββ Entity Extractor Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestEntityExtractor:
def setup_method(self):
from app.modules.entity_extractor import EntityExtractor
self.extractor = EntityExtractor()
def test_extract_persons(self):
text = "John Smith and Jane Doe signed the agreement."
result = self.extractor.extract(text)
assert "persons" in result
# At least one person should be found
persons_lower = [p.lower() for p in result["persons"]]
assert any("john" in p or "jane" in p for p in persons_lower)
def test_extract_organizations(self):
text = "Google LLC and Microsoft Corporation announced a partnership."
result = self.extractor.extract(text)
orgs_lower = [o.lower() for o in result["organizations"]]
assert any("google" in o or "microsoft" in o for o in orgs_lower)
def test_extract_money(self):
text = "The deal was worth $5 million."
result = self.extractor.extract(text)
assert len(result["amounts"]) > 0
assert any("5" in a for a in result["amounts"])
def test_extract_dates(self):
text = "The contract was signed on January 15, 2024."
result = self.extractor.extract(text)
assert len(result["dates"]) > 0
def test_extract_locations(self):
text = "The company is headquartered in New York."
result = self.extractor.extract(text)
locs_lower = [l.lower() for l in result["locations"]]
assert any("new york" in l for l in locs_lower)
def test_empty_text(self):
result = self.extractor.extract("")
assert result["persons"] == []
assert result["organizations"] == []
def test_result_structure(self):
result = self.extractor.extract("Test text")
assert "persons" in result
assert "organizations" in result
assert "dates" in result
assert "amounts" in result
assert "locations" in result
# ββ Sentiment Analyzer Tests ββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestSentimentAnalyzer:
def setup_method(self):
from app.modules.sentiment_analyzer import SentimentAnalyzer
self.analyzer = SentimentAnalyzer()
def test_positive_sentiment(self):
text = "This is an excellent product! We are very happy with the outstanding results and remarkable performance."
result = self.analyzer.analyze(text)
assert result in ("positive", "negative", "neutral") # Must be valid
def test_negative_sentiment(self):
text = "This is a terrible disaster. The project completely failed and caused enormous losses."
result = self.analyzer.analyze(text)
assert result in ("positive", "negative", "neutral")
def test_neutral_sentiment(self):
text = "The report was submitted on Monday. It includes figures from Q3 2024."
result = self.analyzer.analyze(text)
assert result in ("positive", "negative", "neutral")
def test_empty_text(self):
result = self.analyzer.analyze("")
assert result == "neutral"
def test_short_text(self):
result = self.analyzer.analyze("ok")
assert result == "neutral"
# ββ Summarizer Tests ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestSummarizer:
def setup_method(self):
from app.modules.summarizer import Summarizer
self.summarizer = Summarizer()
def test_summarize_returns_string(self):
text = (
"Artificial intelligence is transforming industries worldwide. "
"Companies are investing billions in AI research and development. "
"Machine learning models are being applied to healthcare, finance, and education. "
"The technology promises to automate complex tasks and improve decision making. "
"However, concerns about job displacement and ethical AI remain significant challenges."
)
result = self.summarizer.summarize(text)
assert isinstance(result, str)
assert len(result) > 0
def test_short_text_passthrough(self):
text = "Short text."
result = self.summarizer.summarize(text)
assert isinstance(result, str)
def test_empty_text(self):
result = self.summarizer.summarize("")
assert result == ""
# ββ OCR Processor Tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestOCRProcessor:
def setup_method(self):
from app.modules.ocr_processor import OCRProcessor
self.ocr = OCRProcessor()
def test_preprocess_returns_array(self):
from PIL import Image
import numpy as np
img = Image.new("RGB", (100, 100), color="white")
result = self.ocr.preprocess(img)
assert isinstance(result, np.ndarray)
def test_clean_ocr_output(self):
raw = "Hello World\n\n\n\ntest"
result = self.ocr._clean_ocr_output(raw)
assert "Hello World" in result
# ββ API Integration Test βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class TestAPIEndpoint:
def test_health_check(self):
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
def test_analyze_empty_file(self):
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
response = client.post(
"/analyze",
files={"file": ("test.txt", b"", "text/plain")},
)
assert response.status_code in (400, 422)
def test_analyze_text_as_pdf_structure(self):
"""Test that the API returns the correct response structure."""
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
# Create a minimal valid DOCX in memory
from docx import Document
doc = Document()
doc.add_paragraph(
"Google and Microsoft announced a $10 million partnership in New York on January 15, 2024. "
"CEO John Smith stated the collaboration would bring excellent results for both companies."
)
buf = io.BytesIO()
doc.save(buf)
buf.seek(0)
response = client.post(
"/analyze",
files={"file": ("test.docx", buf.read(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document")},
)
assert response.status_code == 200
data = response.json()
assert "summary" in data
assert "entities" in data
assert "sentiment" in data
assert "persons" in data["entities"]
assert "organizations" in data["entities"]
assert "dates" in data["entities"]
assert "amounts" in data["entities"]
assert "locations" in data["entities"]
assert data["sentiment"] in ("positive", "negative", "neutral")
if __name__ == "__main__":
pytest.main([__file__, "-v", "--tb=short"])
|