-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pyread.py
More file actions
224 lines (177 loc) · 6.73 KB
/
Copy pathtest_pyread.py
File metadata and controls
224 lines (177 loc) · 6.73 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
#!/usr/bin/env python3
"""
Simple tests for PyRead functionality.
Run with: python test_pyread.py
"""
import sys
from pathlib import Path
import tempfile
import shutil
# Add parent directory to path
sys.path.insert(0, str(Path(__file__).parent))
from pyread import (
TextCleaner,
FormatDetector,
FileFormat,
TxtExtractor,
OutputManager,
)
def test_text_cleaner():
"""Test text cleaning and normalization"""
print("Testing TextCleaner...")
# Test basic cleaning
dirty_text = " Hello World \n\n\n\n Test "
clean = TextCleaner.clean(dirty_text)
assert "Hello World" in clean
assert clean.count('\n') <= 2
print(" ✓ Basic cleaning")
# Test NFKC normalization
unicode_text = "fi ①" # ligature and circled digit
clean = TextCleaner.clean(unicode_text)
assert "fi" in clean or "1" in clean # Should normalize
print(" ✓ NFKC normalization")
# Test control character removal
control_text = "Hello\x00\x01World"
clean = TextCleaner.clean(control_text)
assert '\x00' not in clean
assert '\x01' not in clean
print(" ✓ Control character removal")
# Test ASCII forcing
unicode_text = "Hello 世界 World"
clean = TextCleaner.clean(unicode_text, force_ascii=True)
assert all(ord(c) < 128 for c in clean)
print(" ✓ ASCII forcing")
print("✅ TextCleaner tests passed\n")
def test_format_detector():
"""Test format detection"""
print("Testing FormatDetector...")
# Test extension-based detection
assert FormatDetector.detect(Path("test.pdf")) == FileFormat.PDF
assert FormatDetector.detect(Path("test.txt")) == FileFormat.TXT
assert FormatDetector.detect(Path("test.epub")) == FileFormat.EPUB
assert FormatDetector.detect(Path("test.html")) == FileFormat.HTML
print(" ✓ Extension-based detection")
print("✅ FormatDetector tests passed\n")
def test_txt_extractor():
"""Test text file extraction"""
print("Testing TxtExtractor...")
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
f.write("Hello, World!\nThis is a test.")
temp_path = Path(f.name)
try:
result = TxtExtractor.extract(temp_path)
assert result.success
assert "Hello, World!" in result.text
assert "test" in result.text
print(" ✓ UTF-8 text extraction")
finally:
temp_path.unlink()
print("✅ TxtExtractor tests passed\n")
def test_output_manager():
"""Test output path generation and versioning"""
print("Testing OutputManager...")
# Test same directory output
input_path = Path("/home/user/docs/file.pdf")
output_path = OutputManager.get_output_path(input_path)
assert output_path.name == "file.txt"
assert output_path.parent == input_path.parent
print(" ✓ Same directory output")
# Test custom output directory
output_dir = Path("/home/user/output")
output_path = OutputManager.get_output_path(input_path, output_dir)
assert output_path.name == "file.txt"
assert output_path.parent == output_dir
print(" ✓ Custom output directory")
# Test structure preservation
base_dir = Path("/home/user/docs")
nested_input = Path("/home/user/docs/subdir/file.pdf")
output_path = OutputManager.get_output_path(nested_input, output_dir, base_dir)
assert output_path == output_dir / "subdir" / "file.txt"
print(" ✓ Directory structure preservation")
# Test versioning
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
# Create first file
test_file = tmpdir / "test.txt"
test_file.write_text("version 1")
# Test get_versioned_path
versioned = OutputManager.get_versioned_path(test_file)
assert versioned.name == "test_v2.txt"
print(" ✓ Versioned path generation")
# Create v2 and test again
versioned.write_text("version 2")
versioned_3 = OutputManager.get_versioned_path(test_file)
assert versioned_3.name == "test_v3.txt"
print(" ✓ Multiple version handling")
# Test write_text with auto_version
success, error, actual_path = OutputManager.write_text(
test_file, "new content", overwrite=False, auto_version=True
)
assert success
assert actual_path.name == "test_v3.txt"
assert actual_path.exists()
print(" ✓ Auto-versioning on write")
# Test write_text with overwrite
original_content = test_file.read_text()
success, error, actual_path = OutputManager.write_text(
test_file, "overwritten", overwrite=True, auto_version=False
)
assert success
assert actual_path == test_file
assert test_file.read_text() == "overwritten"
print(" ✓ Overwrite mode")
print("✅ OutputManager tests passed\n")
def test_write_and_read():
"""Test end-to-end write and read"""
print("Testing end-to-end processing...")
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = Path(tmpdir)
# Create test input file
input_file = tmpdir / "test.txt"
input_file.write_text("Hello, World!\n\nThis is a test.", encoding='utf-8')
# Process it
from pyread import process_file
output_file = tmpdir / "test_output.txt"
success, message = process_file(
input_file,
output_dir=tmpdir.parent,
base_dir=tmpdir,
force_ascii=False,
use_ocr=False,
overwrite=True
)
if success:
print(f" ✓ Processing succeeded: {message}")
else:
print(f" ✗ Processing failed: {message}")
print("✅ End-to-end test completed\n")
def run_all_tests():
"""Run all tests"""
# Ensure UTF-8 output on Windows
import io
if hasattr(sys.stdout, 'reconfigure'):
sys.stdout.reconfigure(encoding='utf-8')
print("=" * 60)
print("Running PyRead Tests")
print("=" * 60 + "\n")
try:
test_text_cleaner()
test_format_detector()
test_txt_extractor()
test_output_manager()
test_write_and_read()
print("=" * 60)
print("✅ All tests passed!")
print("=" * 60)
return True
except AssertionError as e:
print(f"\n❌ Test failed: {e}")
return False
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = run_all_tests()
sys.exit(0 if success else 1)