Create test_app.py
Browse files- test_app.py +127 -0
test_app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Simple test script for the Smart Document Analysis Platform
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import unittest
|
| 7 |
+
import os
|
| 8 |
+
import tempfile
|
| 9 |
+
import json
|
| 10 |
+
from app import app
|
| 11 |
+
|
| 12 |
+
class TestSmartDocumentAnalysis(unittest.TestCase):
|
| 13 |
+
|
| 14 |
+
def setUp(self):
|
| 15 |
+
"""Set up test client"""
|
| 16 |
+
self.app = app.test_client()
|
| 17 |
+
self.app.testing = True
|
| 18 |
+
|
| 19 |
+
def test_index_page(self):
|
| 20 |
+
"""Test that the main page loads"""
|
| 21 |
+
response = self.app.get('/')
|
| 22 |
+
self.assertEqual(response.status_code, 200)
|
| 23 |
+
self.assertIn(b'Smart Document Analysis Platform', response.data)
|
| 24 |
+
|
| 25 |
+
def test_upload_no_file(self):
|
| 26 |
+
"""Test upload endpoint with no file"""
|
| 27 |
+
response = self.app.post('/upload')
|
| 28 |
+
data = json.loads(response.data)
|
| 29 |
+
self.assertFalse(data['success'])
|
| 30 |
+
self.assertIn('No file provided', data['error'])
|
| 31 |
+
|
| 32 |
+
def test_upload_url_no_url(self):
|
| 33 |
+
"""Test upload from URL with no URL"""
|
| 34 |
+
response = self.app.post('/upload-url',
|
| 35 |
+
json={},
|
| 36 |
+
content_type='application/json')
|
| 37 |
+
data = json.loads(response.data)
|
| 38 |
+
self.assertFalse(data['success'])
|
| 39 |
+
self.assertIn('No URL provided', data['error'])
|
| 40 |
+
|
| 41 |
+
def test_ask_no_question(self):
|
| 42 |
+
"""Test ask endpoint with no question"""
|
| 43 |
+
response = self.app.post('/ask',
|
| 44 |
+
json={'cache_id': 'test'},
|
| 45 |
+
content_type='application/json')
|
| 46 |
+
data = json.loads(response.data)
|
| 47 |
+
self.assertFalse(data['success'])
|
| 48 |
+
self.assertIn('Missing question or cache_id', data['error'])
|
| 49 |
+
|
| 50 |
+
def test_ask_no_cache_id(self):
|
| 51 |
+
"""Test ask endpoint with no cache_id"""
|
| 52 |
+
response = self.app.post('/ask',
|
| 53 |
+
json={'question': 'test'},
|
| 54 |
+
content_type='application/json')
|
| 55 |
+
data = json.loads(response.data)
|
| 56 |
+
self.assertFalse(data['success'])
|
| 57 |
+
self.assertIn('Missing question or cache_id', data['error'])
|
| 58 |
+
|
| 59 |
+
def test_list_caches(self):
|
| 60 |
+
"""Test listing caches"""
|
| 61 |
+
response = self.app.get('/caches')
|
| 62 |
+
data = json.loads(response.data)
|
| 63 |
+
self.assertTrue(data['success'])
|
| 64 |
+
self.assertIn('caches', data)
|
| 65 |
+
|
| 66 |
+
def test_delete_nonexistent_cache(self):
|
| 67 |
+
"""Test deleting a cache that doesn't exist"""
|
| 68 |
+
response = self.app.delete('/cache/nonexistent')
|
| 69 |
+
data = json.loads(response.data)
|
| 70 |
+
self.assertFalse(data['success'])
|
| 71 |
+
self.assertIn('Cache not found', data['error'])
|
| 72 |
+
|
| 73 |
+
def test_environment_setup():
|
| 74 |
+
"""Test that environment is properly configured"""
|
| 75 |
+
print("π§ Testing environment setup...")
|
| 76 |
+
|
| 77 |
+
# Check if .env file exists
|
| 78 |
+
if os.path.exists('.env'):
|
| 79 |
+
print("β
.env file found")
|
| 80 |
+
else:
|
| 81 |
+
print("β οΈ .env file not found - you'll need to create one")
|
| 82 |
+
|
| 83 |
+
# Check if API key is set
|
| 84 |
+
api_key = os.getenv('GOOGLE_API_KEY')
|
| 85 |
+
if api_key and api_key != 'your_gemini_api_key_here':
|
| 86 |
+
print("β
GOOGLE_API_KEY is set")
|
| 87 |
+
else:
|
| 88 |
+
print("β οΈ GOOGLE_API_KEY not set - please add it to .env file")
|
| 89 |
+
|
| 90 |
+
# Check if required packages are installed
|
| 91 |
+
try:
|
| 92 |
+
import flask
|
| 93 |
+
print("β
Flask is installed")
|
| 94 |
+
except ImportError:
|
| 95 |
+
print("β Flask is not installed")
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
import google.genai
|
| 99 |
+
print("β
Google GenAI is installed")
|
| 100 |
+
except ImportError:
|
| 101 |
+
print("β Google GenAI is not installed")
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
import httpx
|
| 105 |
+
print("β
httpx is installed")
|
| 106 |
+
except ImportError:
|
| 107 |
+
print("β httpx is not installed")
|
| 108 |
+
|
| 109 |
+
if __name__ == '__main__':
|
| 110 |
+
print("π§ͺ Running Smart Document Analysis Platform Tests")
|
| 111 |
+
print("=" * 60)
|
| 112 |
+
|
| 113 |
+
# Run environment tests
|
| 114 |
+
test_environment_setup()
|
| 115 |
+
|
| 116 |
+
print("\n" + "=" * 60)
|
| 117 |
+
print("Running unit tests...")
|
| 118 |
+
|
| 119 |
+
# Run unit tests
|
| 120 |
+
unittest.main(argv=[''], exit=False, verbosity=2)
|
| 121 |
+
|
| 122 |
+
print("\nπ Testing completed!")
|
| 123 |
+
print("\nπ Next steps:")
|
| 124 |
+
print(" 1. Set up your GOOGLE_API_KEY in .env file")
|
| 125 |
+
print(" 2. Run 'python app.py' to start the application")
|
| 126 |
+
print(" 3. Open http://localhost:5000 in your browser")
|
| 127 |
+
print(" 4. Try uploading a PDF and asking questions!")
|