File size: 7,295 Bytes
5c36ec7 | 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 252 253 254 255 256 257 258 259 260 261 262 | #!/usr/bin/env python3
"""
Test script for DocGenie API.
Verifies all components are properly installed and configured.
"""
import sys
import os
from pathlib import Path
def test_imports():
"""Test that all required modules can be imported."""
print("Testing imports...")
try:
import fastapi
print(" β FastAPI")
except ImportError as e:
print(f" β FastAPI: {e}")
return False
try:
import uvicorn
print(" β Uvicorn")
except ImportError as e:
print(f" β Uvicorn: {e}")
return False
try:
import pydantic
print(" β Pydantic")
except ImportError as e:
print(f" β Pydantic: {e}")
return False
try:
import requests
print(" β Requests")
except ImportError as e:
print(f" β Requests: {e}")
return False
try:
from PIL import Image
print(" β Pillow")
except ImportError as e:
print(f" β Pillow: {e}")
return False
try:
from bs4 import BeautifulSoup
print(" β BeautifulSoup4")
except ImportError as e:
print(f" β BeautifulSoup4: {e}")
return False
try:
from playwright.async_api import async_playwright
print(" β Playwright")
except ImportError as e:
print(f" β Playwright: {e}")
return False
try:
import anthropic
print(" β Anthropic")
except ImportError as e:
print(f" β Anthropic: {e}")
return False
try:
from docgenie import ENV
print(" β DocGenie")
except ImportError as e:
print(f" β DocGenie: {e}")
return False
return True
def test_api_structure():
"""Test that API files are in place."""
print("\nTesting API structure...")
api_dir = Path(__file__).parent
files = {
"main.py": "Main API application",
"schemas.py": "Request/Response models",
"utils.py": "Processing utilities",
"README.md": "Documentation",
"__init__.py": "Package init"
}
all_present = True
for filename, description in files.items():
filepath = api_dir / filename
if filepath.exists():
print(f" β {filename}: {description}")
else:
print(f" β {filename}: Missing!")
all_present = False
return all_present
def test_docgenie_integration():
"""Test integration with DocGenie modules."""
print("\nTesting DocGenie integration...")
try:
from docgenie import ENV
prompt_template = ENV.PROMPT_TEMPLATES_DIR / "ClaudeRefined12" / "seed-based-json.txt"
if prompt_template.exists():
print(f" β Prompt template found: {prompt_template}")
else:
print(f" β Prompt template not found: {prompt_template}")
return False
# Test reading template
content = prompt_template.read_text(encoding='utf-8')
if "{language}" in content and "{doc_type}" in content:
print(" β Prompt template has required placeholders")
else:
print(" β Prompt template missing placeholders")
return False
return True
except Exception as e:
print(f" β Error: {e}")
return False
def test_environment():
"""Test environment configuration."""
print("\nTesting environment...")
api_key = os.getenv("ANTHROPIC_API_KEY")
if api_key:
print(f" β ANTHROPIC_API_KEY is set (length: {len(api_key)})")
else:
print(" β ANTHROPIC_API_KEY not set (optional for testing)")
python_version = sys.version_info
if python_version >= (3, 10):
print(f" β Python version: {python_version.major}.{python_version.minor}.{python_version.micro}")
else:
print(f" β Python version: {python_version.major}.{python_version.minor}.{python_version.micro} (3.10+ required)")
return False
return True
def test_playwright_browsers():
"""Test if Playwright browsers are installed."""
print("\nTesting Playwright browsers...")
try:
import subprocess
result = subprocess.run(
["playwright", "show-trace", "--help"],
capture_output=True,
timeout=5
)
if result.returncode == 0:
print(" β Playwright CLI is available")
else:
print(" β Playwright CLI might have issues")
# Check if chromium is installed
# This is a basic check - actual browser installation is verified at runtime
print(" βΉ Chromium will be verified when rendering PDFs")
return True
except FileNotFoundError:
print(" β Playwright CLI not found")
return False
except Exception as e:
print(f" β Could not verify Playwright: {e}")
return True # Non-critical for this test
def test_api_modules():
"""Test that API modules can be imported."""
print("\nTesting API modules...")
try:
# Add parent and current directory to path
api_dir = Path(__file__).parent
project_root = api_dir.parent
sys.path.insert(0, str(project_root))
sys.path.insert(0, str(api_dir))
import schemas
print(" β schemas module")
import utils
print(" β utils module")
# Test that schema models exist
schemas.GenerateDocumentRequest
schemas.GenerateDocumentResponse
schemas.DocumentResult
print(" β All schema models defined")
return True
except Exception as e:
print(f" β Error importing API modules: {e}")
return False
def main():
"""Run all tests."""
print("="*60)
print("DocGenie API - Test Suite")
print("="*60)
results = {
"Imports": test_imports(),
"API Structure": test_api_structure(),
"Environment": test_environment(),
"DocGenie Integration": test_docgenie_integration(),
"Playwright": test_playwright_browsers(),
"API Modules": test_api_modules()
}
print("\n" + "="*60)
print("Test Results Summary")
print("="*60)
for test_name, result in results.items():
status = "β PASS" if result else "β FAIL"
print(f"{status}: {test_name}")
all_passed = all(results.values())
print("\n" + "="*60)
if all_passed:
print("β
All tests passed! API is ready to use.")
print("\nTo start the API:")
print(" cd api")
print(" python main.py")
print("\nThen visit: http://localhost:8000/docs")
else:
print("β οΈ Some tests failed. Please fix issues before running the API.")
print("\nCommon fixes:")
print(" uv sync # or: pip install -e .")
print(" playwright install chromium")
print(" export ANTHROPIC_API_KEY='your-key'")
print("="*60)
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())
|