File size: 2,813 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 |
"""
Tests for the EmailParser class.
Run with: pytest tests/test_parser.py -v
"""
import pytest
import sys
from pathlib import Path
# Add src to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
class TestEmailParserImport:
"""Test that EmailParser can be imported."""
def test_import_parser(self):
"""Test that parser module can be imported."""
try:
from data.parser import EmailParser
assert EmailParser is not None
except ImportError as e:
pytest.skip(f"Could not import EmailParser: {e}")
class TestEmailParserMethods:
"""Test EmailParser methods with mock data."""
def test_clean_text_removes_urls(self):
"""Test URL removal in _clean_text."""
from data.parser import EmailParser
# Create a mock parser (won't actually load MBOX)
class MockParser(EmailParser):
def __init__(self):
# Skip file check in init
pass
parser = MockParser()
text = "Visit https://example.com for more info"
result = parser._clean_text(text)
assert "https://example.com" not in result
def test_clean_text_normalizes_whitespace(self):
"""Test whitespace normalization."""
from data.parser import EmailParser
class MockParser(EmailParser):
def __init__(self):
pass
parser = MockParser()
text = "Hello World\n\nTest"
result = parser._clean_text(text)
assert " " not in result # No double spaces
def test_decode_header_none(self):
"""Test handling of None header."""
from data.parser import EmailParser
class MockParser(EmailParser):
def __init__(self):
pass
parser = MockParser()
result = parser._decode_header(None)
assert result == ""
def test_decode_header_plain_string(self):
"""Test decoding plain string header."""
from data.parser import EmailParser
class MockParser(EmailParser):
def __init__(self):
pass
parser = MockParser()
result = parser._decode_header("Simple Subject")
assert result == "Simple Subject"
class TestEmailParserIntegration:
"""Integration tests that require actual files."""
def test_parser_file_not_found(self):
"""Test that missing file raises error."""
from data.parser import EmailParser
with pytest.raises(FileNotFoundError):
EmailParser(Path("/nonexistent/path/file.mbox"))
if __name__ == "__main__":
pytest.main([__file__, "-v"])
|