-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parser.py
More file actions
82 lines (61 loc) · 2.29 KB
/
Copy pathtest_parser.py
File metadata and controls
82 lines (61 loc) · 2.29 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
#!/usr/bin/env python3
"""
Test script for the PDF parser to verify functionality.
"""
import json
import sys
from pathlib import Path
from pdf_parser import PDFParser
def test_basic_parsing():
"""Test basic PDF parsing functionality."""
pdf_file = "fund_factsheet.pdf"
if not Path(pdf_file).exists():
print(f"Error: {pdf_file} not found in current directory")
return False
try:
print(f"Testing PDF parsing with {pdf_file}...")
with PDFParser(pdf_file) as parser:
extracted_data = parser.parse_pdf()
# Basic validation
assert "document_info" in extracted_data
assert "pages" in extracted_data
assert len(extracted_data["pages"]) > 0
# Check structure of first page
first_page = extracted_data["pages"][0]
assert "page_number" in first_page
assert "content" in first_page
print(f"✓ Successfully parsed {len(extracted_data['pages'])} pages")
print(f"✓ Found {len(first_page['content'])} content items on first page")
# Count content types
content_types = {}
for page in extracted_data["pages"]:
for item in page["content"]:
content_type = item.get("type", "unknown")
content_types[content_type] = content_types.get(content_type, 0) + 1
print("Content type summary:")
for content_type, count in content_types.items():
print(f" - {content_type}: {count}")
# Save test output
output_file = "test_output.json"
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(extracted_data, f, indent=2, ensure_ascii=False)
print(f"✓ Test output saved to {output_file}")
return True
except Exception as e:
print(f"✗ Test failed with error: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run the test."""
print("PDF Parser Test Suite")
print("=" * 50)
success = test_basic_parsing()
if success:
print("\n✓ All tests passed!")
return 0
else:
print("\n✗ Tests failed!")
return 1
if __name__ == "__main__":
sys.exit(main())