File size: 12,113 Bytes
dcc24f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
API Tests - Comprehensive Tests for FastAPI Endpoints.

This module contains unit tests for the REST API, including:
- Import tests
- Endpoint functionality tests
- Integration tests with TestClient

Run tests:
    $ pytest tests/test_api.py -v

Author: Ranjit Behera
License: MIT
"""

from __future__ import annotations

import json
import sys
from pathlib import Path

import pytest

# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))


# =============================================================================
# Import Tests
# =============================================================================

class TestAPIImports:
    """Test that all API components can be imported."""
    
    def test_import_server(self) -> None:
        """Test server module import."""
        from api.server import app, create_app
        assert app is not None
        assert callable(create_app)
    
    def test_import_extractor(self) -> None:
        """Test extractor import from data module."""
        from data.extractor import EntityExtractor
        extractor = EntityExtractor()
        assert extractor is not None
    
    def test_import_classifier(self) -> None:
        """Test classifier import from data module."""
        from data.classifier import EmailClassifier
        classifier = EmailClassifier()
        assert classifier is not None
    
    def test_import_models(self) -> None:
        """Test Pydantic models import."""
        from api.server import (
            EmailInput,
            EntityResponse,
            ClassificationResponse,
            HealthResponse,
        )
        assert EmailInput is not None
        assert EntityResponse is not None


# =============================================================================
# Logic Tests
# =============================================================================

class TestExtractionLogic:
    """Test entity extraction logic directly."""
    
    def test_extraction_basic(self) -> None:
        """Test basic entity extraction."""
        from data.extractor import EntityExtractor
        
        extractor = EntityExtractor()
        result = extractor.extract(
            "Rs.2500 debited from account 1234 on 05-01-26"
        )
        
        assert result.amount == "2500"
        assert result.type == "debit"
        assert result.account is not None
    
    def test_extraction_merchants(self) -> None:
        """Test merchant detection."""
        from data.extractor import EntityExtractor
        
        extractor = EntityExtractor()
        result = extractor.extract(
            "Rs.500 debited to swiggy@ybl via UPI"
        )
        
        assert result.merchant == "swiggy"
        assert result.payment_method == "upi"
    
    def test_extraction_full_email(self) -> None:
        """Test full email extraction."""
        from data.extractor import EntityExtractor
        
        extractor = EntityExtractor()
        result = extractor.extract(
            "HDFC Bank: Rs.2500.00 debited from A/c **3545 "
            "on 05-01-26 to VPA swiggy@ybl. Ref: 123456789012"
        )
        
        assert result.is_valid()
        assert result.confidence_score() >= 0.8


class TestClassificationLogic:
    """Test classification logic directly."""
    
    def test_finance_classification(self) -> None:
        """Test finance email classification."""
        from data.classifier import EmailClassifier
        
        classifier = EmailClassifier()
        result = classifier.classify(
            subject="Transaction Alert",
            sender="HDFC Bank",
            body="Rs.500 debited from your account"
        )
        
        assert result.category == "finance"
        assert result.is_transaction is True
    
    def test_shopping_classification(self) -> None:
        """Test shopping email classification."""
        from data.classifier import EmailClassifier
        
        classifier = EmailClassifier()
        result = classifier.classify(
            subject="Your order has shipped",
            sender="Amazon.in",
            body="Your order #12345 is on the way"
        )
        
        assert result.category == "shopping"
    
    def test_non_finance_classification(self) -> None:
        """Test non-finance classification."""
        from data.classifier import EmailClassifier
        
        classifier = EmailClassifier()
        result = classifier.classify(
            subject="Weekly Newsletter",
            sender="Substack",
            body="Top 10 articles this week"
        )
        
        assert result.category == "newsletter"
        assert result.is_transaction is False


# =============================================================================
# FastAPI Endpoint Tests
# =============================================================================

class TestFastAPIClient:
    """Test API endpoints using TestClient."""
    
    @pytest.fixture
    def client(self):
        """Create test client."""
        from fastapi.testclient import TestClient
        from api.server import app
        return TestClient(app)
    
    def test_root_endpoint(self, client) -> None:
        """Test root endpoint returns API info."""
        response = client.get("/")
        
        assert response.status_code == 200
        data = response.json()
        assert "name" in data
        assert "endpoints" in data
        assert data["name"] == "LLM Mail Trainer API"
    
    def test_health_endpoint(self, client) -> None:
        """Test health check endpoint."""
        response = client.get("/health")
        
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "healthy"
        assert "version" in data
        assert "uptime_seconds" in data
    
    def test_stats_endpoint(self, client) -> None:
        """Test statistics endpoint."""
        response = client.get("/stats")
        
        assert response.status_code == 200
        data = response.json()
        assert "total_requests" in data
        assert "uptime_seconds" in data
    
    def test_extract_endpoint(self, client) -> None:
        """Test entity extraction endpoint."""
        response = client.post(
            "/extract",
            json={
                "subject": "Transaction Alert",
                "body": "Rs.2500.00 debited from account 3545 on 05-01-26",
                "sender": "HDFC Bank"
            }
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "success" in data
        assert "entities" in data
        assert data["entities"]["amount"] == "2500.00"
    
    def test_extract_endpoint_validation(self, client) -> None:
        """Test extract endpoint validation."""
        response = client.post(
            "/extract",
            json={
                "body": ""  # Empty body should fail validation
            }
        )
        
        assert response.status_code == 422  # Validation error
    
    def test_classify_endpoint(self, client) -> None:
        """Test classification endpoint."""
        response = client.post(
            "/classify",
            json={
                "subject": "Transaction Alert",
                "body": "Your account has been debited",
                "sender": "HDFC Bank"
            }
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["category"] == "finance"
        assert "confidence" in data
    
    def test_analyze_endpoint(self, client) -> None:
        """Test full analysis endpoint."""
        response = client.post(
            "/analyze",
            json={
                "subject": "Transaction Alert",
                "body": "Rs.500 debited from account 1234 on 01-01-26",
                "sender": "HDFC Bank"
            }
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "classification" in data
        assert "entities" in data  # Should have entities for finance
        assert data["classification"]["category"] == "finance"
    
    def test_batch_endpoint(self, client) -> None:
        """Test batch processing endpoint."""
        response = client.post(
            "/batch",
            json={
                "emails": [
                    {
                        "subject": "Transaction 1",
                        "body": "Rs.100 debited",
                        "sender": "Bank"
                    },
                    {
                        "subject": "Transaction 2",
                        "body": "Rs.200 credited",
                        "sender": "Bank"
                    }
                ]
            }
        )
        
        assert response.status_code == 200
        data = response.json()
        assert data["total_processed"] == 2
        assert "results" in data
        assert len(data["results"]) == 2


# =============================================================================
# Edge Case Tests
# =============================================================================

class TestEdgeCases:
    """Test edge cases and error handling."""
    
    @pytest.fixture
    def client(self):
        """Create test client."""
        from fastapi.testclient import TestClient
        from api.server import app
        return TestClient(app)
    
    def test_empty_body(self, client) -> None:
        """Test handling of empty body."""
        response = client.post(
            "/extract",
            json={
                "body": "   "  # Whitespace only
            }
        )
        
        assert response.status_code == 422
    
    def test_very_long_body(self, client) -> None:
        """Test handling of very long body."""
        long_body = "Rs.100 debited. " * 100
        response = client.post(
            "/extract",
            json={"body": long_body}
        )
        
        assert response.status_code == 200
    
    def test_unicode_content(self, client) -> None:
        """Test handling of unicode content."""
        response = client.post(
            "/extract",
            json={
                "body": "₹500 डेबिट from खाता 1234"
            }
        )
        
        assert response.status_code == 200
    
    def test_batch_empty_list(self, client) -> None:
        """Test batch with empty list."""
        response = client.post(
            "/batch",
            json={"emails": []}
        )
        
        assert response.status_code == 422  # Validation error


# =============================================================================
# Performance Tests
# =============================================================================

class TestPerformance:
    """Test API performance."""
    
    @pytest.fixture
    def client(self):
        """Create test client."""
        from fastapi.testclient import TestClient
        from api.server import app
        return TestClient(app)
    
    def test_extraction_speed(self, client) -> None:
        """Test extraction completes quickly."""
        import time
        
        start = time.time()
        response = client.post(
            "/extract",
            json={"body": "Rs.500 debited on 01-01-26"}
        )
        elapsed = time.time() - start
        
        assert response.status_code == 200
        assert elapsed < 1.0  # Should complete in under 1 second
    
    def test_batch_performance(self, client) -> None:
        """Test batch processing performance."""
        import time
        
        emails = [
            {"body": f"Rs.{i*100} debited", "subject": f"Txn {i}"}
            for i in range(10)
        ]
        
        start = time.time()
        response = client.post("/batch", json={"emails": emails})
        elapsed = time.time() - start
        
        assert response.status_code == 200
        assert elapsed < 5.0  # 10 emails in under 5 seconds


if __name__ == "__main__":
    pytest.main([__file__, "-v"])