-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_empty_inputs.py
More file actions
486 lines (374 loc) · 17.3 KB
/
test_empty_inputs.py
File metadata and controls
486 lines (374 loc) · 17.3 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
"""
Comprehensive edge case tests for empty/None inputs.
Tests covering:
- SpellChecker.check() with empty, whitespace, None inputs
- SpellChecker.check_batch() with empty lists, None, mixed inputs
- Config with disabled validators, empty/None values
- Individual validators with edge case inputs
"""
import pytest
from myspellchecker import SpellChecker
from myspellchecker.core.config import SpellCheckerConfig
from myspellchecker.core.exceptions import ProcessingError, TokenizationError
from myspellchecker.core.response import Response
from myspellchecker.providers import MemoryProvider
class TestSpellCheckerEmptyInputs:
"""Test SpellChecker.check() with empty and edge case inputs."""
@pytest.fixture
def checker(self):
"""Create a SpellChecker with MemoryProvider for testing."""
provider = MemoryProvider()
config = SpellCheckerConfig(
fallback_to_empty_provider=True,
use_context_checker=False,
)
return SpellChecker(config=config, provider=provider)
def test_check_empty_string(self, checker):
"""Test check() with empty string."""
result = checker.check("")
assert isinstance(result, Response)
assert result.text == ""
assert result.has_errors is False
assert result.errors == []
def test_check_whitespace_only(self, checker):
"""Test check() with whitespace-only string."""
result = checker.check(" ")
assert isinstance(result, Response)
assert result.has_errors is False
def test_check_tabs_and_newlines(self, checker):
"""Test check() with tabs and newlines."""
result = checker.check("\n\t")
assert isinstance(result, Response)
assert result.has_errors is False
def test_check_mixed_whitespace(self, checker):
"""Test check() with mixed whitespace characters."""
result = checker.check(" \t\n \r\n ")
assert isinstance(result, Response)
assert result.has_errors is False
def test_check_none_input(self, checker):
"""Test check() with None input raises TypeError."""
with pytest.raises(TypeError, match="text must be a string"):
checker.check(None)
def test_check_single_space(self, checker):
"""Test check() with single space."""
result = checker.check(" ")
assert isinstance(result, Response)
assert result.has_errors is False
def test_check_single_myanmar_character(self, checker):
"""Test check() with single Myanmar character."""
result = checker.check("က")
assert isinstance(result, Response)
# Single consonant may or may not be valid depending on rules
def test_check_single_non_myanmar_character(self, checker):
"""Test check() with single non-Myanmar character."""
result = checker.check("A")
assert isinstance(result, Response)
def test_check_zero_width_characters(self, checker):
"""Test check() with zero-width characters only."""
# Zero-width space, zero-width non-joiner
result = checker.check("\u200b\u200c\u200d")
assert isinstance(result, Response)
class TestSpellCheckerBatchEmptyInputs:
"""Test SpellChecker.check_batch() with empty and edge case inputs."""
@pytest.fixture
def checker(self):
"""Create a SpellChecker with MemoryProvider for testing."""
provider = MemoryProvider()
config = SpellCheckerConfig(
fallback_to_empty_provider=True,
use_context_checker=False,
)
return SpellChecker(config=config, provider=provider)
def test_check_batch_empty_list(self, checker):
"""Test check_batch() with empty list raises ValueError."""
with pytest.raises(ProcessingError, match="texts list cannot be empty"):
checker.check_batch([])
def test_check_batch_list_of_empty_strings(self, checker):
"""Test check_batch() with list of empty strings."""
results = checker.check_batch(["", "", ""])
assert isinstance(results, list)
assert len(results) == 3
for result in results:
assert isinstance(result, Response)
assert result.has_errors is False
def test_check_batch_none_input(self, checker):
"""Test check_batch() with None input raises TypeError."""
with pytest.raises((TypeError, AttributeError)):
checker.check_batch(None)
def test_check_batch_mixed_empty_and_valid(self, checker):
"""Test check_batch() with mixed empty and valid inputs."""
results = checker.check_batch(["", "ကောင်း", " ", "မြန်မာ", "\n\t"])
assert isinstance(results, list)
assert len(results) == 5
def test_check_batch_single_empty_item(self, checker):
"""Test check_batch() with single empty item."""
results = checker.check_batch([""])
assert isinstance(results, list)
assert len(results) == 1
assert results[0].has_errors is False
def test_check_batch_whitespace_items(self, checker):
"""Test check_batch() with various whitespace items."""
results = checker.check_batch([" ", " ", "\t", "\n", "\r\n"])
assert isinstance(results, list)
assert len(results) == 5
def test_check_batch_with_none_in_list(self, checker):
"""Test check_batch() with None items in list raises ValueError."""
with pytest.raises(ProcessingError, match="must be a string"):
checker.check_batch(["valid", None, "text"])
class TestConfigEdgeCases:
"""Test SpellCheckerConfig with edge case values."""
def test_config_all_validators_disabled(self):
"""Test config with all validators disabled."""
config = SpellCheckerConfig(
use_phonetic=False,
use_context_checker=False,
use_ner=False,
use_rule_based_validation=False,
fallback_to_empty_provider=True,
)
provider = MemoryProvider()
checker = SpellChecker(config=config, provider=provider)
# Should still work with basic validation
result = checker.check("ကောင်း")
assert isinstance(result, Response)
def test_config_minimal_settings(self):
"""Test config with minimal settings."""
config = SpellCheckerConfig(
max_edit_distance=1,
max_suggestions=1,
fallback_to_empty_provider=True,
)
provider = MemoryProvider()
checker = SpellChecker(config=config, provider=provider)
result = checker.check("ကောင်း")
assert isinstance(result, Response)
def test_config_empty_provider(self):
"""Test with empty MemoryProvider (no words/syllables)."""
provider = MemoryProvider()
config = SpellCheckerConfig(fallback_to_empty_provider=True)
checker = SpellChecker(config=config, provider=provider)
# Should handle empty dictionary gracefully
result = checker.check("ကောင်း")
assert isinstance(result, Response)
def test_config_max_values(self):
"""Test config with maximum allowed values."""
config = SpellCheckerConfig(
max_edit_distance=3, # Maximum allowed
max_suggestions=100,
fallback_to_empty_provider=True,
)
provider = MemoryProvider()
checker = SpellChecker(config=config, provider=provider)
result = checker.check("ကောင်း")
assert isinstance(result, Response)
def test_config_zero_suggestions(self):
"""Test that zero max_suggestions raises ValueError."""
with pytest.raises(ValueError, match="greater than or equal to 1"):
SpellCheckerConfig(
max_suggestions=0,
fallback_to_empty_provider=True,
)
class TestValidatorEmptyInputs:
"""Test individual validators with empty/None inputs."""
def test_syllable_validator_requires_args(self):
"""Test SyllableValidator requires constructor arguments."""
from myspellchecker.core.validators import SyllableValidator
with pytest.raises(TypeError, match="missing .* required positional argument"):
SyllableValidator()
def test_word_validator_requires_args(self):
"""Test WordValidator requires constructor arguments."""
from myspellchecker.core.validators import WordValidator
with pytest.raises(TypeError, match="missing .* required positional argument"):
WordValidator()
def test_context_validator_behavior(self):
"""Test context validation behavior through SpellChecker."""
# ContextValidator is an internal implementation detail
# Test context validation through SpellChecker interface
from myspellchecker import SpellChecker
from myspellchecker.core.config import SpellCheckerConfig
from myspellchecker.providers import MemoryProvider
provider = MemoryProvider()
config = SpellCheckerConfig(
use_context_checker=True,
fallback_to_empty_provider=True,
)
checker = SpellChecker(config=config, provider=provider)
result = checker.check("")
assert result is not None
class TestNormalizationEmptyInputs:
"""Test text normalization with empty/None inputs."""
def test_normalize_empty_string(self):
"""Test normalize_myanmar_text with empty string."""
from myspellchecker.text.normalize import normalize_myanmar_text
result = normalize_myanmar_text("")
assert result == ""
def test_normalize_whitespace_only(self):
"""Test normalize_myanmar_text with whitespace only."""
from myspellchecker.text.normalize import normalize_myanmar_text
result = normalize_myanmar_text(" ")
assert isinstance(result, str)
def test_normalize_none_input(self):
"""Test normalize_myanmar_text with None input returns None."""
from myspellchecker.text.normalize import normalize_myanmar_text
result = normalize_myanmar_text(None)
assert result is None
class TestSegmenterEmptyInputs:
"""Test segmenters with empty/None inputs."""
def test_default_segmenter_empty_input(self):
"""Test DefaultSegmenter with empty input raises ValueError."""
from myspellchecker.segmenters import DefaultSegmenter
segmenter = DefaultSegmenter()
with pytest.raises(TokenizationError, match="cannot be empty"):
segmenter.segment_syllables("")
def test_default_segmenter_whitespace_input(self):
"""Test DefaultSegmenter with whitespace input raises ValueError."""
from myspellchecker.segmenters import DefaultSegmenter
segmenter = DefaultSegmenter()
with pytest.raises(TokenizationError, match="cannot be empty"):
segmenter.segment_syllables(" ")
def test_regex_segmenter_empty_input(self):
"""Test RegexSegmenter with empty input raises ValueError."""
from myspellchecker.segmenters.regex import RegexSegmenter
segmenter = RegexSegmenter()
with pytest.raises(TokenizationError, match="cannot be empty"):
segmenter.segment_syllables("")
def test_regex_segmenter_whitespace_input(self):
"""Test RegexSegmenter with whitespace input raises ValueError."""
from myspellchecker.segmenters.regex import RegexSegmenter
segmenter = RegexSegmenter()
with pytest.raises(TokenizationError, match="cannot be empty"):
segmenter.segment_syllables(" ")
class TestProviderEmptyInputs:
"""Test providers with empty/None inputs."""
def test_memory_provider_empty_syllable(self):
"""Test MemoryProvider with empty syllable lookup."""
from myspellchecker.providers import MemoryProvider
provider = MemoryProvider()
assert provider.is_valid_syllable("") is False
assert provider.get_syllable_frequency("") == 0
def test_memory_provider_empty_word(self):
"""Test MemoryProvider with empty word lookup."""
from myspellchecker.providers import MemoryProvider
provider = MemoryProvider()
assert provider.is_valid_word("") is False
assert provider.get_word_frequency("") == 0
def test_memory_provider_whitespace_input(self):
"""Test MemoryProvider with whitespace inputs."""
from myspellchecker.providers import MemoryProvider
provider = MemoryProvider()
assert provider.is_valid_syllable(" ") is False
assert provider.is_valid_word(" ") is False
def test_memory_provider_none_input(self):
"""Test MemoryProvider with None inputs returns False."""
from myspellchecker.providers import MemoryProvider
provider = MemoryProvider()
assert provider.is_valid_syllable(None) is False
class TestResponseEmptyInputs:
"""Test Response object creation with edge cases."""
def test_response_empty_text(self):
"""Test Response with empty text."""
from myspellchecker.core.response import Response
response = Response(
text="",
corrected_text="",
has_errors=False,
level="syllable",
errors=[],
metadata={},
)
assert response.text == ""
assert response.has_errors is False
assert len(response.errors) == 0
def test_response_whitespace_text(self):
"""Test Response with whitespace text."""
from myspellchecker.core.response import Response
response = Response(
text=" ",
corrected_text=" ",
has_errors=False,
level="syllable",
errors=[],
metadata={},
)
assert response.text == " "
assert response.has_errors is False
def test_response_to_dict_empty(self):
"""Test Response.to_dict() with empty text."""
from myspellchecker.core.response import Response
response = Response(
text="",
corrected_text="",
has_errors=False,
level="syllable",
errors=[],
metadata={},
)
result = response.to_dict()
assert isinstance(result, dict)
assert result["text"] == ""
assert result["has_errors"] is False
class TestSyllableRuleValidatorEmptyInputs:
"""Test SyllableRuleValidator with empty inputs."""
def test_rule_validator_empty_string(self):
"""Test SyllableRuleValidator with empty string."""
from myspellchecker.core.syllable_rules import SyllableRuleValidator
validator = SyllableRuleValidator()
result = validator.validate("")
assert result is False # Empty string is not a valid syllable
def test_rule_validator_whitespace(self):
"""Test SyllableRuleValidator with whitespace."""
from myspellchecker.core.syllable_rules import SyllableRuleValidator
validator = SyllableRuleValidator()
result = validator.validate(" ")
assert result is False # Whitespace is not a valid syllable
def test_rule_validator_single_consonant(self):
"""Test SyllableRuleValidator with single consonant."""
from myspellchecker.core.syllable_rules import SyllableRuleValidator
validator = SyllableRuleValidator()
# Single Myanmar consonant may or may not be valid
result = validator.validate("က")
assert isinstance(result, bool)
class TestPhoneticEmptyInputs:
"""Test phonetic hashing with empty inputs."""
def test_phonetic_hasher_empty_input(self):
"""Test PhoneticHasher with empty input."""
from myspellchecker.text.phonetic import PhoneticHasher
hasher = PhoneticHasher()
if hasattr(hasher, "hash"):
result = hasher.hash("")
# Empty string should return empty or specific marker
assert result is not None
def test_phonetic_hasher_whitespace(self):
"""Test PhoneticHasher with whitespace input."""
from myspellchecker.text.phonetic import PhoneticHasher
hasher = PhoneticHasher()
if hasattr(hasher, "hash"):
result = hasher.hash(" ")
assert result is not None
class TestBuilderEmptyInputs:
"""Test SpellCheckerBuilder with edge case inputs."""
def test_builder_minimal_build(self):
"""Test builder with minimal configuration."""
from myspellchecker.core.builder import SpellCheckerBuilder
from myspellchecker.providers import MemoryProvider
builder = SpellCheckerBuilder()
builder.with_provider(MemoryProvider())
checker = builder.build()
assert checker is not None
result = checker.check("")
assert isinstance(result, Response)
def test_builder_all_features_disabled(self):
"""Test builder with all features disabled."""
from myspellchecker.core.builder import SpellCheckerBuilder
from myspellchecker.providers import MemoryProvider
builder = SpellCheckerBuilder()
builder.with_provider(MemoryProvider())
builder.with_phonetic(False)
builder.with_context_checking(False)
builder.with_ner(False)
builder.with_rule_based_validation(False)
checker = builder.build()
result = checker.check("ကောင်း")
assert isinstance(result, Response)
if __name__ == "__main__":
pytest.main([__file__, "-v"])