""" Test script for OCR/OMR backend endpoints. Usage: python test_endpoints.py [base_url] Default base_url: http://localhost:5000 """ import sys import os import requests BASE_URL = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:5000" IMAGES_DIR = os.path.join(os.path.dirname(__file__), "Images") results = [] # Track prerequisite passes for dependent tests prereqs = { "question_paper": False, "omr_answer_key": False, "process_omr": False, } def report(name, passed, detail=""): status = "PASS" if passed else "FAIL" results.append((name, status)) msg = f"[{status}] {name}" if detail: msg += f" -- {detail}" print(msg) return passed def skip(name, reason): results.append((name, "SKIP")) print(f"[SKIP] {name} -- {reason}") # --------------------------------------------------------------------------- # 1. Health check # --------------------------------------------------------------------------- def test_health(): try: r = requests.get(f"{BASE_URL}/health", timeout=10) return report("/health", r.status_code == 200, f"status={r.status_code}") except Exception as e: return report("/health", False, str(e)) # --------------------------------------------------------------------------- # 2. Root endpoint # --------------------------------------------------------------------------- def test_root(): try: r = requests.get(f"{BASE_URL}/", timeout=10) return report("/ (root)", r.status_code == 200, f"status={r.status_code}") except Exception as e: return report("/ (root)", False, str(e)) # --------------------------------------------------------------------------- # 3. EasyOCR # --------------------------------------------------------------------------- def test_easyocr(): img = os.path.join(IMAGES_DIR, "test.jpg") if not os.path.exists(img): return report("/easyocr", False, "test.jpg not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/easyocr", files={"images": f}, timeout=60) ok = r.status_code == 200 detail = f"status={r.status_code}" if ok: data = r.json() detail += f", keys={list(data.keys()) if isinstance(data, dict) else 'list'}" return report("/easyocr", ok, detail) except Exception as e: return report("/easyocr", False, str(e)) # --------------------------------------------------------------------------- # 4. Tesseract # --------------------------------------------------------------------------- def test_tesseract(): img = os.path.join(IMAGES_DIR, "OCRSheet.jpg") if not os.path.exists(img): return report("/tesseract", False, "OCRSheet.jpg not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/tesseract", files={"images": f}, timeout=60) ok = r.status_code == 200 return report("/tesseract", ok, f"status={r.status_code}") except Exception as e: return report("/tesseract", False, str(e)) # --------------------------------------------------------------------------- # 5. Process question paper (PDF) # --------------------------------------------------------------------------- def test_question_paper_pdf(): img = os.path.join(IMAGES_DIR, "question_paper.pdf") if not os.path.exists(img): return report("/process_question_paper (pdf)", False, "question_paper.pdf not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/process_question_paper", files={"file": f}, timeout=120) ok = r.status_code == 200 detail = f"status={r.status_code}" if ok: data = r.json() detail += f", questions={len(data.get('questions', []))}" prereqs["question_paper"] = True return report("/process_question_paper (pdf)", ok, detail) except Exception as e: return report("/process_question_paper (pdf)", False, str(e)) # --------------------------------------------------------------------------- # 6. Process question paper (image) # --------------------------------------------------------------------------- def test_question_paper_png(): img = os.path.join(IMAGES_DIR, "question_paper.png") if not os.path.exists(img): return report("/process_question_paper (png)", False, "question_paper.png not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/process_question_paper", files={"file": f}, timeout=120) ok = r.status_code == 200 detail = f"status={r.status_code}" if ok: data = r.json() detail += f", questions={len(data.get('questions', []))}" prereqs["question_paper"] = True return report("/process_question_paper (png)", ok, detail) except Exception as e: return report("/process_question_paper (png)", False, str(e)) # --------------------------------------------------------------------------- # 7. Process OMR answer key # --------------------------------------------------------------------------- def test_omr_answer_key(): img = os.path.join(IMAGES_DIR, "omr_answer_key.pdf") if not os.path.exists(img): return report("/process_omr_answer_key", False, "omr_answer_key.pdf not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/process_omr_answer_key", files={"file": f}, timeout=120) ok = r.status_code == 200 detail = f"status={r.status_code}" if ok: data = r.json() detail += f", success={data.get('success')}" prereqs["omr_answer_key"] = True return report("/process_omr_answer_key", ok, detail) except Exception as e: return report("/process_omr_answer_key", False, str(e)) # --------------------------------------------------------------------------- # 8. Process OMR sheet # --------------------------------------------------------------------------- def test_process_omr(): img = os.path.join(IMAGES_DIR, "OMRSheet.jpg") if not os.path.exists(img): return report("/process_omr", False, "OMRSheet.jpg not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/process_omr", files={"images": f}, timeout=180) ok = r.status_code == 200 detail = f"status={r.status_code}" if ok: prereqs["process_omr"] = True return report("/process_omr", ok, detail) except Exception as e: return report("/process_omr", False, str(e)) # --------------------------------------------------------------------------- # 9. Evaluate OMR (depends on 7 + 8) # --------------------------------------------------------------------------- def test_evaluate_omr(): if not prereqs["omr_answer_key"] or not prereqs["process_omr"]: skip("/evaluate_omr", "requires /process_omr_answer_key and /process_omr to pass first") return try: r = requests.post(f"{BASE_URL}/evaluate_omr", timeout=120) ok = r.status_code == 200 return report("/evaluate_omr", ok, f"status={r.status_code}") except Exception as e: return report("/evaluate_omr", False, str(e)) # --------------------------------------------------------------------------- # 10. Evaluate answers (depends on 5/6) # --------------------------------------------------------------------------- def test_evaluate_answers(): if not prereqs["question_paper"]: skip("/evaluate_answers", "requires /process_question_paper to pass first") return img = os.path.join(IMAGES_DIR, "test.jpg") if not os.path.exists(img): return report("/evaluate_answers", False, "test.jpg not found") try: with open(img, "rb") as f: r = requests.post(f"{BASE_URL}/evaluate_answers", files={"student_answers": f}, timeout=120) ok = r.status_code == 200 return report("/evaluate_answers", ok, f"status={r.status_code}") except Exception as e: return report("/evaluate_answers", False, str(e)) # --------------------------------------------------------------------------- # Run all tests in order # --------------------------------------------------------------------------- if __name__ == "__main__": print(f"\nTesting backend at: {BASE_URL}\n" + "=" * 60) test_health() test_root() test_easyocr() test_tesseract() test_question_paper_pdf() test_question_paper_png() test_omr_answer_key() test_process_omr() test_evaluate_omr() test_evaluate_answers() # Summary print("\n" + "=" * 60) passed = sum(1 for _, s in results if s == "PASS") failed = sum(1 for _, s in results if s == "FAIL") skipped = sum(1 for _, s in results if s == "SKIP") total = len(results) print(f"Results: {passed}/{total} passed, {failed} failed, {skipped} skipped") sys.exit(0 if failed == 0 else 1)