-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_setup.py
More file actions
251 lines (197 loc) · 6.91 KB
/
test_setup.py
File metadata and controls
251 lines (197 loc) · 6.91 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
#!/usr/bin/env python3
"""
Test script to verify the text extraction tool setup.
This script checks:
1. All dependencies are installed
2. Configuration is valid
3. API keys are present
4. Processors can be initialized
"""
import sys
from pathlib import Path
def test_imports():
"""Test that all required modules can be imported."""
print("Testing imports...")
try:
# Core Python modules
import os
import tempfile
from pathlib import Path
from typing import List, Union, Dict, Any
print(" ✓ Core Python modules")
# Third-party dependencies
import PyPDF2
print(" ✓ PyPDF2")
import docx
print(" ✓ python-docx")
import openpyxl
print(" ✓ openpyxl")
import xlrd
print(" ✓ xlrd")
import magic
print(" ✓ python-magic")
import moviepy
print(" ✓ moviepy")
import pydub
print(" ✓ pydub")
import openai
print(" ✓ openai")
import google.generativeai as genai
print(" ✓ google-generativeai")
from dotenv import load_dotenv
print(" ✓ python-dotenv")
print("✓ All imports successful!")
return True
except ImportError as e:
print(f"✗ Import error: {e}")
print("\nTo fix this, run: pip install -r requirements.txt")
return False
def test_project_structure():
"""Test that the project structure is correct."""
print("\nTesting project structure...")
required_files = [
"src/__init__.py",
"src/config.py",
"src/text_extractor.py",
"src/file_processors/__init__.py",
"src/file_processors/base_processor.py",
"src/file_processors/openai_processor.py",
"src/file_processors/gemini_processor.py",
"main.py",
"requirements.txt",
"README.md"
]
missing_files = []
for file_path in required_files:
if not Path(file_path).exists():
missing_files.append(file_path)
else:
print(f" ✓ {file_path}")
if missing_files:
print(f"✗ Missing files: {missing_files}")
return False
else:
print("✓ All required files present!")
return True
def test_configuration():
"""Test configuration loading."""
print("\nTesting configuration...")
try:
from src.config import Config
print(f" OpenAI API Key: {'SET' if Config.OPENAI_API_KEY else 'NOT SET'}")
print(f" Google API Key: {'SET' if Config.GOOGLE_API_KEY else 'NOT SET'}")
print(f" OpenAI Model: {Config.OPENAI_MODEL}")
print(f" Gemini Model: {Config.GEMINI_MODEL}")
# Test validation
is_valid = Config.validate_config()
if is_valid:
print("✓ Configuration is valid!")
else:
print("⚠ Configuration incomplete - some API keys are missing")
print(" Create a .env file with your API keys (see env_example.txt)")
return True
except Exception as e:
print(f"✗ Configuration error: {e}")
return False
def test_processors():
"""Test processor initialization."""
print("\nTesting processors...")
try:
from src.config import Config
# Test OpenAI processor
if Config.OPENAI_API_KEY:
try:
from src.file_processors.openai_processor import OpenAIProcessor
openai_processor = OpenAIProcessor()
print(" ✓ OpenAI processor initialized")
except Exception as e:
print(f" ✗ OpenAI processor failed: {e}")
else:
print(" ⚠ OpenAI processor skipped (no API key)")
# Test Gemini processor
if Config.GOOGLE_API_KEY:
try:
from src.file_processors.gemini_processor import GeminiProcessor
gemini_processor = GeminiProcessor()
print(" ✓ Gemini processor initialized")
except Exception as e:
print(f" ✗ Gemini processor failed: {e}")
else:
print(" ⚠ Gemini processor skipped (no API key)")
return True
except Exception as e:
print(f"✗ Processor test error: {e}")
return False
def test_main_extractor():
"""Test main TextExtractor class."""
print("\nTesting main extractor...")
try:
from src.text_extractor import TextExtractor
# This will only work if API keys are configured
try:
extractor = TextExtractor()
# Test getting supported extensions
extensions = extractor.get_supported_extensions()
print(f" ✓ TextExtractor initialized")
print(f" ✓ Found {len(extensions)} processor types")
for processor, exts in extensions.items():
print(f" {processor}: {len(exts)} extensions")
return True
except ValueError as e:
if "API keys" in str(e):
print(" ⚠ TextExtractor needs API keys to initialize")
print(" This is expected if you haven't set up .env file yet")
return True
else:
raise e
except Exception as e:
print(f"✗ TextExtractor test error: {e}")
return False
def test_cli():
"""Test CLI interface."""
print("\nTesting CLI interface...")
try:
# Test that main.py can be imported
import main
print(" ✓ main.py can be imported")
# Test help functionality (doesn't require API keys)
print(" ✓ CLI module structure looks good")
return True
except Exception as e:
print(f"✗ CLI test error: {e}")
return False
def main():
"""Run all tests."""
print("Text Extraction Tool - Setup Test")
print("=" * 50)
tests = [
test_imports,
test_project_structure,
test_configuration,
test_processors,
test_main_extractor,
test_cli
]
passed = 0
total = len(tests)
for test_func in tests:
try:
if test_func():
passed += 1
except Exception as e:
print(f"✗ Test {test_func.__name__} crashed: {e}")
print("\n" + "=" * 50)
print(f"Test Results: {passed}/{total} passed")
if passed == total:
print("🎉 All tests passed! Your setup looks good.")
print("\nNext steps:")
print("1. Add your API keys to a .env file (see env_example.txt)")
print("2. Test with sample files: python main.py --info")
print("3. Try the examples: python examples/example_usage.py")
else:
print("⚠ Some tests failed. Please check the issues above.")
if passed >= 3: # Core functionality works
print("\nCore functionality appears to work.")
print("Missing API keys are the most common issue.")
if __name__ == "__main__":
main()