-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_app.py
More file actions
224 lines (196 loc) · 8.71 KB
/
test_app.py
File metadata and controls
224 lines (196 loc) · 8.71 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
#!/usr/bin/env python3
"""
Test script for BookBytes application
Demonstrates basic functionality and API usage
"""
import requests
import json
import time
import os
from pathlib import Path
class BookBytesTest:
def __init__(self, base_url="http://localhost:5000"):
self.base_url = base_url
self.session = requests.Session()
def test_health_check(self):
"""Test the health check endpoint"""
print("\n🔍 Testing health check...")
try:
response = self.session.get(f"{self.base_url}/health")
if response.status_code == 200:
print("✅ Health check passed")
print(f" Response: {response.json()}")
return True
else:
print(f"❌ Health check failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Health check error: {e}")
return False
def test_process_book(self, isbn):
"""Test processing a book by ISBN"""
print(f"\n📚 Testing book processing for ISBN: {isbn}")
try:
payload = {"isbn": isbn}
response = self.session.post(
f"{self.base_url}/api/process",
json=payload,
headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
result = response.json()
print("✅ Book processing successful")
print(f" Title: {result.get('book', {}).get('title', 'Unknown')}")
print(f" Author: {result.get('book', {}).get('author', 'Unknown')}")
print(f" Chapters processed: {result.get('chapters_processed', 0)}")
print(f" Message: {result.get('message', '')}")
return True, result
else:
print(f"❌ Book processing failed: {response.status_code}")
try:
error_data = response.json()
print(f" Error: {error_data.get('message', 'Unknown error')}")
except:
print(f" Error: {response.text}")
return False, None
except Exception as e:
print(f"❌ Book processing error: {e}")
return False, None
def test_list_books(self):
"""Test listing all books"""
print("\n📖 Testing book listing...")
try:
response = self.session.get(f"{self.base_url}/api/books")
if response.status_code == 200:
result = response.json()
books = result.get('books', [])
print(f"✅ Found {len(books)} books")
for book in books:
print(f" - {book.get('title', 'Unknown')} by {book.get('author', 'Unknown')}")
print(f" ISBN: {book.get('isbn', 'Unknown')}, Chapters: {book.get('chapter_count', 0)}")
return True, books
else:
print(f"❌ Book listing failed: {response.status_code}")
return False, []
except Exception as e:
print(f"❌ Book listing error: {e}")
return False, []
def test_get_chapters(self, isbn):
"""Test getting chapters for a specific book"""
print(f"\n📑 Testing chapter retrieval for ISBN: {isbn}")
try:
response = self.session.get(f"{self.base_url}/api/books/{isbn}/chapters")
if response.status_code == 200:
result = response.json()
chapters = result.get('chapters', [])
print(f"✅ Found {len(chapters)} chapters")
for chapter in chapters[:3]: # Show first 3 chapters
print(f" Chapter {chapter.get('chapter_number', 0)}: {chapter.get('title', 'Unknown')}")
print(f" Words: {chapter.get('word_count', 0)}, Audio: {bool(chapter.get('audio_file_path'))}")
if len(chapters) > 3:
print(f" ... and {len(chapters) - 3} more chapters")
return True, chapters
else:
print(f"❌ Chapter retrieval failed: {response.status_code}")
return False, []
except Exception as e:
print(f"❌ Chapter retrieval error: {e}")
return False, []
def test_download_audio(self, isbn, chapter_number, output_dir="test_audio"):
"""Test downloading audio for a specific chapter"""
print(f"\n🎵 Testing audio download for ISBN: {isbn}, Chapter: {chapter_number}")
try:
# Create output directory
Path(output_dir).mkdir(exist_ok=True)
response = self.session.get(f"{self.base_url}/api/audio/{isbn}/{chapter_number}")
if response.status_code == 200:
filename = f"{output_dir}/{isbn}_chapter_{chapter_number:02d}.mp3"
with open(filename, 'wb') as f:
f.write(response.content)
file_size = os.path.getsize(filename)
print(f"✅ Audio downloaded successfully")
print(f" File: {filename}")
print(f" Size: {file_size:,} bytes")
return True, filename
else:
print(f"❌ Audio download failed: {response.status_code}")
return False, None
except Exception as e:
print(f"❌ Audio download error: {e}")
return False, None
def run_full_test(self, test_isbn="9780307887894"):
"""Run a complete test suite"""
print("🚀 Starting BookBytes Test Suite")
print("=" * 50)
# Test 1: Health check
if not self.test_health_check():
print("\n❌ Health check failed. Make sure the server is running.")
return False
# Test 2: Process a book
success, book_data = self.test_process_book(test_isbn)
if not success:
print("\n❌ Book processing failed. Check your OpenAI API key and internet connection.")
return False
# Wait a moment for processing to complete
time.sleep(2)
# Test 3: List books
success, books = self.test_list_books()
if not success:
print("\n❌ Book listing failed.")
return False
# Test 4: Get chapters
success, chapters = self.test_get_chapters(test_isbn)
if not success or not chapters:
print("\n❌ Chapter retrieval failed.")
return False
# Test 5: Download audio for first chapter
success, audio_file = self.test_download_audio(test_isbn, 1)
if not success:
print("\n❌ Audio download failed.")
return False
print("\n" + "=" * 50)
print("🎉 All tests passed successfully!")
print("\n📊 Test Summary:")
print(f" ✅ Health check: OK")
print(f" ✅ Book processing: OK")
print(f" ✅ Book listing: OK")
print(f" ✅ Chapter retrieval: OK")
print(f" ✅ Audio download: OK")
if audio_file:
print(f"\n🎵 Sample audio file created: {audio_file}")
print(" You can play this file to test the audio quality.")
return True
def main():
"""Main test function"""
print("BookBytes Test Script")
print("====================\n")
# Check if server is likely running
tester = BookBytesTest()
# Test with a well-known book
test_isbns = [
"9780307887894", # The Power of Habit
"9780735211292", # Atomic Habits
"9780374533557", # Thinking, Fast and Slow
]
print("Available test ISBNs:")
for i, isbn in enumerate(test_isbns, 1):
print(f" {i}. {isbn}")
# Use the first ISBN for testing
test_isbn = test_isbns[0]
print(f"\nUsing ISBN: {test_isbn} for testing")
# Run the full test suite
success = tester.run_full_test(test_isbn)
if success:
print("\n🎯 Next steps:")
print(" 1. Try processing other books with different ISBNs")
print(" 2. Listen to the generated audio files")
print(" 3. Integrate the API into your own applications")
print(" 4. Explore the database to see stored data")
else:
print("\n🔧 Troubleshooting:")
print(" 1. Make sure the server is running: python app.py")
print(" 2. Check your OpenAI API key is set: export OPENAI_API_KEY='your-key'")
print(" 3. Ensure you have internet connectivity")
print(" 4. Check the server logs for detailed error messages")
if __name__ == "__main__":
main()