File size: 11,334 Bytes
31f0e50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Unit Tests for Text Preprocessing Module.



Tests text cleaning, normalization, and utility functions.

"""

import pytest

from app.utils.preprocessing import (
    clean_text,
    normalize_text,
    convert_devanagari_digits,
    truncate_text,
    remove_urls,
    extract_numbers,
    mask_sensitive_data,
)


class TestCleanText:
    """Tests for clean_text function."""
    
    def test_empty_string(self):
        """Test empty string returns empty string."""
        assert clean_text("") == ""
    
    def test_none_returns_empty(self):
        """Test None or falsy value returns empty string."""
        assert clean_text(None) == ""
    
    def test_removes_extra_whitespace(self):
        """Test extra whitespace is normalized."""
        text = "Hello   world   here"
        result = clean_text(text)
        assert result == "Hello world here"
    
    def test_removes_leading_trailing_whitespace(self):
        """Test leading/trailing whitespace is stripped."""
        text = "   Hello world   "
        result = clean_text(text)
        assert result == "Hello world"
    
    def test_removes_control_characters(self):
        """Test control characters are removed."""
        text = "Hello\x00\x07world"
        result = clean_text(text)
        assert "\x00" not in result
        assert "\x07" not in result
        assert "Hello" in result
        assert "world" in result
    
    def test_preserves_normal_text(self):
        """Test normal text is preserved."""
        text = "Hello, how are you?"
        result = clean_text(text)
        assert result == text
    
    def test_normalizes_newlines_and_tabs(self):
        """Test newlines and tabs are normalized to spaces."""
        text = "Hello\nworld\there"
        result = clean_text(text)
        assert result == "Hello world here"
    
    def test_handles_unicode(self):
        """Test Unicode text is preserved."""
        text = "नमस्ते दुनिया"
        result = clean_text(text)
        assert result == text


class TestNormalizeText:
    """Tests for normalize_text function."""
    
    def test_basic_normalization(self):
        """Test basic text normalization."""
        text = "  Hello   world  "
        result = normalize_text(text)
        assert result == "Hello world"
    
    def test_lowercase_option(self):
        """Test lowercase option."""
        text = "Hello WORLD"
        result = normalize_text(text, lowercase=True)
        assert result == "hello world"
    
    def test_without_lowercase(self):
        """Test preserves case by default."""
        text = "Hello WORLD"
        result = normalize_text(text, lowercase=False)
        assert result == "Hello WORLD"
    
    def test_converts_devanagari_digits(self):
        """Test Devanagari digits are converted."""
        text = "Amount: ५०००"
        result = normalize_text(text)
        assert "5000" in result


class TestConvertDevanagariDigits:
    """Tests for convert_devanagari_digits function."""
    
    def test_converts_all_digits(self):
        """Test all Devanagari digits are converted."""
        text = "०१२३४५६७८९"
        result = convert_devanagari_digits(text)
        assert result == "0123456789"
    
    def test_preserves_latin_digits(self):
        """Test Latin digits are preserved."""
        text = "123456"
        result = convert_devanagari_digits(text)
        assert result == "123456"
    
    def test_mixed_digits(self):
        """Test mixed Devanagari and Latin digits."""
        text = "Phone: ९८76543२१०"
        result = convert_devanagari_digits(text)
        assert result == "Phone: 9876543210"
    
    def test_preserves_non_digit_text(self):
        """Test non-digit text is preserved."""
        text = "नमस्ते"
        result = convert_devanagari_digits(text)
        assert result == "नमस्ते"
    
    def test_empty_string(self):
        """Test empty string returns empty."""
        assert convert_devanagari_digits("") == ""
    
    def test_phone_number_in_hindi(self):
        """Test phone number conversion in Hindi context."""
        text = "कॉल करें ९८७६५४३२१०"
        result = convert_devanagari_digits(text)
        assert "9876543210" in result


class TestTruncateText:
    """Tests for truncate_text function."""
    
    def test_short_text_unchanged(self):
        """Test text shorter than limit is unchanged."""
        text = "Hello world"
        result = truncate_text(text, max_length=100)
        assert result == text
    
    def test_long_text_truncated(self):
        """Test text longer than limit is truncated."""
        text = "a" * 100
        result = truncate_text(text, max_length=50)
        assert len(result) == 50
        assert result.endswith("...")
    
    def test_custom_suffix(self):
        """Test custom truncation suffix."""
        text = "a" * 100
        result = truncate_text(text, max_length=50, suffix="[...]")
        assert result.endswith("[...]")
    
    def test_exact_length(self):
        """Test text at exact length is unchanged."""
        text = "a" * 50
        result = truncate_text(text, max_length=50)
        assert result == text
    
    def test_default_max_length(self):
        """Test default max_length is 5000."""
        text = "a" * 5000
        result = truncate_text(text)
        assert len(result) == 5000


class TestRemoveUrls:
    """Tests for remove_urls function."""
    
    def test_removes_http_url(self):
        """Test HTTP URLs are removed."""
        text = "Visit http://example.com for more info"
        result = remove_urls(text)
        assert "http://example.com" not in result
        assert "Visit" in result
    
    def test_removes_https_url(self):
        """Test HTTPS URLs are removed."""
        text = "Visit https://secure.example.com for more info"
        result = remove_urls(text)
        assert "https://secure.example.com" not in result
    
    def test_removes_multiple_urls(self):
        """Test multiple URLs are removed."""
        text = "Visit http://one.com and http://two.com"
        result = remove_urls(text)
        assert "http://one.com" not in result
        assert "http://two.com" not in result
    
    def test_preserves_non_url_text(self):
        """Test non-URL text is preserved."""
        text = "Hello world, no URLs here"
        result = remove_urls(text)
        assert result == text
    
    def test_removes_complex_url(self):
        """Test complex URLs with paths are removed."""
        text = "Click http://example.com/path/to/page?query=value"
        result = remove_urls(text)
        assert "http://example.com" not in result


class TestExtractNumbers:
    """Tests for extract_numbers function."""
    
    def test_extracts_single_number(self):
        """Test extracts single number."""
        text = "Amount is 5000"
        result = extract_numbers(text)
        assert "5000" in result
    
    def test_extracts_multiple_numbers(self):
        """Test extracts multiple numbers."""
        text = "Account 123456 and phone 9876543210"
        result = extract_numbers(text)
        assert "123456" in result
        assert "9876543210" in result
    
    def test_handles_devanagari_digits(self):
        """Test handles Devanagari digits."""
        text = "Amount ५०००"
        result = extract_numbers(text)
        assert "5000" in result
    
    def test_no_numbers(self):
        """Test returns empty list when no numbers."""
        text = "No numbers here"
        result = extract_numbers(text)
        assert result == []
    
    def test_mixed_devanagari_and_latin(self):
        """Test mixed digit systems."""
        text = "Phone ९८76543२१० account 123"
        result = extract_numbers(text)
        assert "9876543210" in result
        assert "123" in result


class TestMaskSensitiveData:
    """Tests for mask_sensitive_data function."""
    
    def test_masks_upi_id(self):
        """Test UPI ID is masked."""
        text = "Send to scammer@paytm"
        result = mask_sensitive_data(text)
        assert "scammer@paytm" not in result
        assert "[UPI_MASKED]" in result
    
    def test_masks_bank_account(self):
        """Test bank account number is masked."""
        text = "Account: 123456789012345"
        result = mask_sensitive_data(text)
        assert "123456789012345" not in result
        assert "[ACCOUNT_MASKED]" in result
    
    def test_masks_phone_number(self):
        """Test phone number is masked."""
        text = "Call 9876543210"
        result = mask_sensitive_data(text)
        assert "9876543210" not in result
        # Phone number gets masked (either as phone or account since 10 digits)
        assert "[PHONE_MASKED]" in result or "[ACCOUNT_MASKED]" in result
    
    def test_masks_phone_with_plus91(self):
        """Test phone with +91 prefix is masked."""
        text = "Call +91 9876543210"
        result = mask_sensitive_data(text)
        assert "9876543210" not in result
        # Phone number gets masked (either as phone or account)
        assert "[PHONE_MASKED]" in result or "[ACCOUNT_MASKED]" in result
    
    def test_preserves_non_sensitive_text(self):
        """Test non-sensitive text is preserved."""
        text = "Hello, how are you?"
        result = mask_sensitive_data(text)
        assert result == text
    
    def test_masks_multiple_sensitive_items(self):
        """Test masks multiple sensitive items in one text."""
        text = "Send to fraud@ybl, call 9876543210, account 123456789012"
        result = mask_sensitive_data(text)
        
        assert "fraud@ybl" not in result
        assert "9876543210" not in result
        assert "123456789012" not in result


class TestPreprocessingEdgeCases:
    """Edge case tests for preprocessing functions."""
    
    def test_clean_text_with_emojis(self):
        """Test clean_text preserves emojis."""
        text = "Hello 😀 world 🎉"
        result = clean_text(text)
        assert "😀" in result
        assert "🎉" in result
    
    def test_normalize_very_long_text(self):
        """Test normalize handles very long text."""
        text = "word " * 10000
        result = normalize_text(text)
        assert len(result) > 0
    
    def test_devanagari_mixed_with_special_chars(self):
        """Test Devanagari digits with special characters."""
        text = "Amount: ₹५,०००/-"
        result = convert_devanagari_digits(text)
        assert "5" in result
        assert "0" in result
    
    def test_url_with_hindi_text(self):
        """Test URL removal with surrounding Hindi text."""
        text = "यहाँ क्लिक करें http://fake.com जीतने के लिए"
        result = remove_urls(text)
        assert "http://fake.com" not in result
        assert "यहाँ क्लिक करें" in result