CoolFace
Apppublic

Propelis/QC_Rules

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
test_pdf_requirements.py74 linesDownload Raw Back to root
1#!/usr/bin/env python3
2"""
3Test script for PDF requirements functionality
4"""
5
6import os
7import tempfile
8from src.extract_text.ingest import RequirementsIngest
9
10def test_pdf_requirements():
11    """Test PDF requirements ingestion"""
12    print("Testing PDF requirements functionality...")
13    
14    # Create a simple test PDF (we'll use an existing one if available)
15    test_pdf_path = None
16    
17    # Look for any PDF file in the requirements_library
18    for root, dirs, files in os.walk("requirements_library"):
19        for file in files:
20            if file.lower().endswith('.pdf'):
21                test_pdf_path = os.path.join(root, file)
22                break
23        if test_pdf_path:
24            break
25    
26    if not test_pdf_path:
27        print("No PDF files found for testing. Creating a simple test...")
28        # Create a simple test with text file
29        with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
30            f.write("Test requirement: All products must have allergen information.")
31            test_file_path = f.name
32        
33        print(f"Created test text file: {test_file_path}")
34    else:
35        print(f"Using existing PDF for testing: {test_pdf_path}")
36        test_file_path = test_pdf_path
37    
38    try:
39        # Test the ingestion
40        ingest = RequirementsIngest()
41        
42        # Open the file and test ingestion
43        with open(test_file_path, 'rb') as f:
44            result = ingest.ingest_requirements_document(f)
45        
46        print("✅ Ingestion successful!")
47        print(f"Result type: {type(result)}")
48        
49        if isinstance(result, dict):
50            print(f"File type: {result.get('type', 'unknown')}")
51            print(f"Filename: {result.get('filename', 'unknown')}")
52            print(f"File size: {result.get('file_size', 0)} bytes")
53            print(f"Text content preview: {result.get('text_content', '')[:200]}...")
54        else:
55            print(f"Text content: {result[:200]}...")
56        
57        print("\n✅ PDF requirements functionality is working!")
58        
59    except Exception as e:
60        print(f"❌ Error during testing: {e}")
61        import traceback
62        traceback.print_exc()
63    
64    finally:
65        # Clean up test file if we created one
66        if test_pdf_path is None and 'test_file_path' in locals():
67            try:
68                os.unlink(test_file_path)
69                print(f"Cleaned up test file: {test_file_path}")
70            except:
71                pass
72
73if __name__ == "__main__":
74    test_pdf_requirements()