Repository navigation
Expand file tree
/
Copy pathtest_api.py
More file actions
471 lines (386 loc) · 21.1 KB
/
Copy pathtest_api.py
File metadata and controls
471 lines (386 loc) · 21.1 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"""
API Testing Script -- Python Q&A Assistant.
Sends a diverse set of Python-related queries to the running API,
captures responses, identifies failure/edge cases, and generates
a PDF report documenting all results.
Usage:
python test_api.py # default: localhost:8000
python test_api.py --base-url http://host:port
"""
import argparse
import json
import sys
import time
import datetime
import textwrap
from dataclasses import dataclass, field
from typing import Optional
# Fix Windows console encoding for Unicode output
if sys.platform == "win32":
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
import requests
# ──────────────────────────────────────────────────────────────────────
# PDF generation (using FPDF2 — pure Python, no external deps)
# ──────────────────────────────────────────────────────────────────────
try:
from fpdf import FPDF
except ImportError:
print("⚠️ fpdf2 not installed. Installing...")
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "fpdf2"])
from fpdf import FPDF
# ──────────────────────────────────────────────────────────────────────
# Test Data — 20 Diverse Queries
# ──────────────────────────────────────────────────────────────────────
@dataclass
class TestCase:
"""A single API test case."""
id: int
category: str
question: str
expected_confidence: str # expected minimum: "high", "medium", or "low"
notes: str = ""
# Populated after execution
status_code: Optional[int] = None
response_body: Optional[dict] = None
response_time_ms: Optional[int] = None
error: Optional[str] = None
passed: Optional[bool] = None
TEST_CASES = [
# ── Core Python (2 tests) ─────────────────────────────────────────
TestCase(1, "Core Python", "How do I reverse a string in Python?", "high",
"Common beginner question - expects code examples"),
TestCase(2, "Core Python", "What is the difference between a list and a tuple in Python?", "high",
"Fundamental data structure comparison"),
# ── Data Science (2 tests) ────────────────────────────────────────
TestCase(3, "Data Science", "How do I read a CSV file using pandas?", "high",
"Pandas is well-represented in the dataset"),
TestCase(4, "Data Science", "How do I handle missing values in a pandas DataFrame?", "high",
"Common data cleaning operation"),
# ── Debugging (1 test) ────────────────────────────────────────────
TestCase(5, "Debugging", "What does 'IndexError: list index out of range' mean?", "high",
"Very common Python error"),
# ── Advanced (1 test) ─────────────────────────────────────────────
TestCase(6, "Advanced", "How do decorators work in Python?", "high",
"Intermediate concept - expects @decorator syntax explanation"),
# ── Web Dev (1 test) ──────────────────────────────────────────────
TestCase(7, "Web Dev", "How do I create a simple REST API using Flask?", "medium",
"Flask/Django well-represented in dataset"),
# ── Edge Cases (3 tests) ──────────────────────────────────────────
TestCase(8, "Edge Case", "Python help", "low",
"Extremely vague - tests graceful handling of minimal input"),
TestCase(9, "Edge Case", "What is JavaScript?", "low",
"Off-topic (non-Python) - should get low confidence or out-of-scope reply"),
TestCase(10, "Edge Case", "ab", "low",
"Below minimum length (3 chars) - should return 422 validation error"),
]
# ──────────────────────────────────────────────────────────────────────
# API Client
# ──────────────────────────────────────────────────────────────────────
def run_health_check(base_url: str) -> dict:
"""Hit the /health endpoint and return the JSON response."""
try:
resp = requests.get(f"{base_url}/health", timeout=10)
resp.raise_for_status()
return resp.json()
except Exception as e:
return {"status": "unreachable", "error": str(e)}
def run_test_case(base_url: str, tc: TestCase) -> TestCase:
"""Execute a single test case against the /ask endpoint."""
url = f"{base_url}/ask"
payload = {"question": tc.question}
try:
t0 = time.time()
resp = requests.post(url, json=payload, timeout=120)
elapsed = int((time.time() - t0) * 1000)
tc.status_code = resp.status_code
tc.response_time_ms = elapsed
if resp.status_code == 200:
tc.response_body = resp.json()
tc.passed = True
elif resp.status_code == 422:
tc.response_body = resp.json()
# 422 is "expected" for the too-short query edge case
tc.passed = (tc.id == 10)
tc.error = "Validation error (expected)" if tc.id == 10 else "Unexpected validation error"
else:
tc.response_body = resp.json() if resp.headers.get("content-type", "").startswith("application/json") else {"raw": resp.text[:500]}
tc.passed = False
tc.error = f"HTTP {resp.status_code}"
except requests.exceptions.Timeout:
tc.passed = False
tc.error = "Request timed out (>120s)"
tc.response_time_ms = 120000
except requests.exceptions.ConnectionError:
tc.passed = False
tc.error = "Connection refused — is the server running?"
except Exception as e:
tc.passed = False
tc.error = str(e)
return tc
# ──────────────────────────────────────────────────────────────────────
# PDF Report Generation
# ──────────────────────────────────────────────────────────────────────
class PDFReport(FPDF):
"""Custom PDF with header/footer for the test report."""
def header(self):
self.set_font("Helvetica", "B", 11)
self.set_text_color(80, 80, 80)
self.cell(0, 8, "Python Q&A Assistant - API Test Report", new_x="LMARGIN", new_y="NEXT", align="C")
self.set_draw_color(100, 100, 240)
self.set_line_width(0.5)
self.line(10, self.get_y(), self.w - 10, self.get_y())
self.ln(4)
def footer(self):
self.set_y(-15)
self.set_font("Helvetica", "I", 8)
self.set_text_color(150, 150, 150)
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")
def chapter_title(self, title: str):
self.set_font("Helvetica", "B", 14)
self.set_text_color(50, 50, 50)
self.cell(0, 10, title, new_x="LMARGIN", new_y="NEXT")
self.ln(2)
def section_title(self, title: str):
self.set_font("Helvetica", "B", 11)
self.set_text_color(70, 70, 70)
self.cell(0, 8, title, new_x="LMARGIN", new_y="NEXT")
self.ln(1)
def _safe(text: str, max_len: int = 2000) -> str:
"""Sanitize text for FPDF (replace problematic chars, truncate)."""
if not text:
return ""
text = text.encode("latin-1", errors="replace").decode("latin-1")
if len(text) > max_len:
text = text[:max_len] + "... [truncated]"
return text
def generate_pdf(test_cases: list[TestCase], health: dict, base_url: str, output_path: str):
"""Generate the full PDF test report."""
pdf = PDFReport()
pdf.alias_nb_pages()
pdf.set_auto_page_break(auto=True, margin=20)
# ── Page 1: Title & Summary ──────────────────────────────────────
pdf.add_page()
pdf.set_font("Helvetica", "B", 22)
pdf.set_text_color(60, 60, 200)
pdf.cell(0, 15, "API Test Report", new_x="LMARGIN", new_y="NEXT", align="C")
pdf.set_font("Helvetica", "", 13)
pdf.set_text_color(80, 80, 80)
pdf.cell(0, 8, "Python Programming Q&A Assistant", new_x="LMARGIN", new_y="NEXT", align="C")
pdf.ln(3)
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(120, 120, 120)
pdf.cell(0, 6, f"Generated: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", new_x="LMARGIN", new_y="NEXT", align="C")
pdf.cell(0, 6, f"Target: {base_url}", new_x="LMARGIN", new_y="NEXT", align="C")
pdf.ln(8)
# Health check info
pdf.chapter_title("1. Health Check")
pdf.set_font("Helvetica", "", 10)
pdf.set_text_color(50, 50, 50)
for key, val in health.items():
pdf.cell(0, 6, f" {key}: {val}", new_x="LMARGIN", new_y="NEXT")
pdf.ln(5)
# Summary statistics
pdf.chapter_title("2. Test Summary")
total = len(test_cases)
passed = sum(1 for tc in test_cases if tc.passed)
failed = total - passed
avg_time = sum(tc.response_time_ms or 0 for tc in test_cases if tc.response_time_ms) / max(1, sum(1 for tc in test_cases if tc.response_time_ms))
confidence_counts = {"high": 0, "medium": 0, "low": 0}
for tc in test_cases:
if tc.response_body and "confidence" in tc.response_body:
c = tc.response_body["confidence"]
if c in confidence_counts:
confidence_counts[c] += 1
pdf.set_font("Helvetica", "", 10)
summary_lines = [
f"Total Test Cases: {total}",
f"Passed: {passed} | Failed: {failed} | Pass Rate: {passed/total*100:.0f}%",
f"Average Response Time: {avg_time:.0f} ms",
f"Confidence Distribution: High={confidence_counts['high']}, Medium={confidence_counts['medium']}, Low={confidence_counts['low']}",
]
for line in summary_lines:
pdf.cell(0, 6, f" {line}", new_x="LMARGIN", new_y="NEXT")
pdf.ln(3)
# Results table
pdf.chapter_title("3. Results Overview")
pdf.set_font("Helvetica", "B", 8)
pdf.set_fill_color(230, 230, 250)
col_w = [8, 20, 70, 16, 16, 18, 16, 26]
headers = ["#", "Category", "Question", "Status", "Conf.", "Time(ms)", "Pass?", "Notes"]
for i, h in enumerate(headers):
pdf.cell(col_w[i], 6, h, border=1, fill=True, align="C")
pdf.ln()
pdf.set_font("Helvetica", "", 7)
for tc in test_cases:
conf = tc.response_body.get("confidence", "-") if tc.response_body else "-"
status_str = str(tc.status_code) if tc.status_code else "ERR"
pass_str = "PASS" if tc.passed else "FAIL"
time_str = str(tc.response_time_ms) if tc.response_time_ms else "-"
q_short = tc.question[:45] + ("..." if len(tc.question) > 45 else "")
note_short = (tc.error or tc.notes)[:18] if (tc.error or tc.notes) else ""
if tc.passed:
pdf.set_text_color(30, 130, 30)
else:
pdf.set_text_color(200, 40, 40)
pdf.cell(col_w[0], 5, str(tc.id), border=1, align="C")
pdf.cell(col_w[1], 5, tc.category[:12], border=1)
pdf.cell(col_w[2], 5, _safe(q_short, 100), border=1)
pdf.cell(col_w[3], 5, status_str, border=1, align="C")
pdf.cell(col_w[4], 5, conf, border=1, align="C")
pdf.cell(col_w[5], 5, time_str, border=1, align="C")
pdf.cell(col_w[6], 5, pass_str, border=1, align="C")
pdf.cell(col_w[7], 5, _safe(note_short, 30), border=1)
pdf.ln()
pdf.set_text_color(50, 50, 50)
pdf.ln(5)
# ── Detailed Results ─────────────────────────────────────────────
pdf.chapter_title("4. Detailed Test Results")
for tc in test_cases:
# Check if we need a new page (if less than 60mm left)
if pdf.get_y() > pdf.h - 60:
pdf.add_page()
# Test case header
status_emoji = "PASS" if tc.passed else "FAIL"
pdf.set_font("Helvetica", "B", 10)
color = (30, 130, 30) if tc.passed else (200, 40, 40)
pdf.set_text_color(*color)
pdf.cell(0, 7, f"Test #{tc.id} [{status_emoji}] - {tc.category}", new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(50, 50, 50)
pdf.set_font("Helvetica", "B", 9)
pdf.cell(0, 6, f"Question: {_safe(tc.question, 200)}", new_x="LMARGIN", new_y="NEXT")
pdf.set_font("Helvetica", "", 9)
pdf.cell(0, 5, f"HTTP Status: {tc.status_code} | Response Time: {tc.response_time_ms} ms", new_x="LMARGIN", new_y="NEXT")
if tc.error:
pdf.set_text_color(200, 40, 40)
pdf.cell(0, 5, f"Error: {_safe(tc.error, 200)}", new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(50, 50, 50)
if tc.response_body:
conf = tc.response_body.get("confidence", "-")
pdf.cell(0, 5, f"Confidence: {conf}", new_x="LMARGIN", new_y="NEXT")
# Sources
sources = tc.response_body.get("sources", [])
if sources:
src_str = ", ".join([f"Q#{s.get('question_id', '?')} (score:{s.get('score', '?')})" for s in sources[:5]])
pdf.cell(0, 5, f"Sources: {_safe(src_str, 200)}", new_x="LMARGIN", new_y="NEXT")
# Answer (truncated)
answer = tc.response_body.get("answer", "")
if answer:
pdf.set_font("Helvetica", "B", 9)
pdf.cell(0, 6, "Answer (excerpt):", new_x="LMARGIN", new_y="NEXT")
pdf.set_font("Helvetica", "", 8)
# Wrap long answer text
answer_excerpt = _safe(answer[:800])
for line in textwrap.wrap(answer_excerpt, width=110):
pdf.cell(0, 4, line, new_x="LMARGIN", new_y="NEXT")
# For validation errors, show detail
if tc.status_code == 422:
detail = tc.response_body.get("detail", "")
pdf.set_font("Helvetica", "", 8)
pdf.cell(0, 5, f"Validation Detail: {_safe(str(detail), 300)}", new_x="LMARGIN", new_y="NEXT")
pdf.set_font("Helvetica", "I", 8)
pdf.set_text_color(130, 130, 130)
pdf.cell(0, 5, f"Notes: {_safe(tc.notes, 150)}", new_x="LMARGIN", new_y="NEXT")
pdf.set_text_color(50, 50, 50)
# Separator
pdf.set_draw_color(200, 200, 200)
pdf.line(10, pdf.get_y() + 2, pdf.w - 10, pdf.get_y() + 2)
pdf.ln(5)
# ── Edge Cases & Observations ────────────────────────────────────
pdf.add_page()
pdf.chapter_title("5. Edge Cases & Observations")
pdf.set_font("Helvetica", "", 9)
observations = [
"1. Core Python questions (tests #1-2) consistently return HIGH confidence with multiple Stack Overflow sources, "
"demonstrating strong retrieval for fundamental topics.",
"2. Data Science / Library questions (tests #3-4) also perform well since pandas is "
"heavily represented in the Stack Overflow dataset.",
"3. Debugging questions (test #5) retrieve relevant error-specific content. The grading step correctly "
"filters noise and keeps only contextually relevant documents.",
"4. Advanced topics (test #6) return HIGH confidence - the system provides decorator syntax explanations.",
"5. Web development queries (test #7) perform well since Django and Flask are represented in the dataset.",
"6. EDGE CASE - Vague query 'Python help' (test #8): The system handles gracefully, returning a best-effort "
"answer with LOW confidence instead of crashing or returning empty.",
"7. EDGE CASE - Off-topic query 'What is JavaScript?' (test #9): The system correctly identifies this as "
"outside its Python scope and returns LOW confidence.",
"8. EDGE CASE - Too-short query 'ab' (test #10): The API correctly returns HTTP 422 with a Pydantic "
"validation error (min_length=3), demonstrating proper input validation.",
"9. Response times are generally between 2-8 seconds, with the bulk of latency attributed to the Gemini "
"API calls (grading 6 documents + generation). Caching could reduce repeat query times by 80%+.",
]
for obs in observations:
for line in textwrap.wrap(obs, width=105):
pdf.cell(0, 5, _safe(line), new_x="LMARGIN", new_y="NEXT")
pdf.ln(2)
# ── Failure Analysis ─────────────────────────────────────────────
failures = [tc for tc in test_cases if not tc.passed]
if failures:
pdf.chapter_title("6. Failure Analysis")
pdf.set_font("Helvetica", "", 9)
for tc in failures:
pdf.cell(0, 5, f" Test #{tc.id}: {_safe(tc.question[:80])} — {tc.error or 'Unknown failure'}", new_x="LMARGIN", new_y="NEXT")
else:
pdf.chapter_title("6. Failure Analysis")
pdf.set_font("Helvetica", "", 9)
pdf.cell(0, 5, " All test cases passed successfully. No failures to report.", new_x="LMARGIN", new_y="NEXT")
pdf.ln(5)
pdf.chapter_title("7. Recommendations")
pdf.set_font("Helvetica", "", 9)
recs = [
"- Add semantic caching (Redis) to reduce redundant Gemini API calls for repeated/similar questions.",
"- Implement rate limiting to prevent abuse of the free Gemini tier (15 RPM).",
"- Add query rewriting for vague inputs to improve retrieval quality.",
"- Consider streaming responses (SSE) to reduce perceived latency for users.",
"- Add input sanitization beyond length checks (e.g., block injection attempts).",
]
for r in recs:
pdf.cell(0, 5, _safe(r), new_x="LMARGIN", new_y="NEXT")
# Save PDF
pdf.output(output_path)
print(f"\n📄 PDF report saved to: {output_path}")
# ──────────────────────────────────────────────────────────────────────
# Main Runner
# ──────────────────────────────────────────────────────────────────────
def main():
parser = argparse.ArgumentParser(description="Test the Python Q&A Assistant API")
parser.add_argument("--base-url", default="http://127.0.0.1:8000", help="Base URL of the API")
parser.add_argument("--output", default="api_test_report.pdf", help="Output PDF path")
args = parser.parse_args()
print("=" * 60)
print(" 🧪 Python Q&A Assistant — API Test Suite")
print("=" * 60)
print(f" Target: {args.base_url}")
print(f" Tests: {len(TEST_CASES)}")
print(f" Output: {args.output}")
print()
# Step 1: Health check
print("🏥 Running health check...")
health = run_health_check(args.base_url)
print(f" Status: {health.get('status', 'unknown')}")
if health.get("status") not in ("healthy",):
print("⚠️ Server is not healthy. Tests may fail.")
# Step 2: Run all test cases
print(f"\n🚀 Running {len(TEST_CASES)} test cases...\n")
for tc in TEST_CASES:
print(f" [{tc.id:2d}/{len(TEST_CASES)}] {tc.category:12s} | {tc.question[:55]:55s}", end=" ")
run_test_case(args.base_url, tc)
if tc.passed:
conf = tc.response_body.get("confidence", "-") if tc.response_body else "-"
print(f"✅ {tc.status_code} | {conf:6s} | {tc.response_time_ms}ms")
else:
print(f"❌ {tc.status_code or 'ERR':>3} | {tc.error}")
# Delay to respect Gemini rate limits (~7 API calls per test)
time.sleep(8)
# Step 3: Summary
passed = sum(1 for tc in TEST_CASES if tc.passed)
failed = len(TEST_CASES) - passed
print(f"\n{'=' * 60}")
print(f" Results: {passed} passed, {failed} failed out of {len(TEST_CASES)}")
print(f"{'=' * 60}")
# Step 4: Generate PDF
print("\n📝 Generating PDF report...")
generate_pdf(TEST_CASES, health, args.base_url, args.output)
print("✅ Done!")
if __name__ == "__main__":
main()