File size: 9,264 Bytes
b8548e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

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)