Spaces:
Running
Running
| import requests | |
| import os | |
| import sys | |
| import uuid | |
| from datetime import date | |
| # Base URL for the FastAPI server | |
| BASE_URL = "http://localhost:8000" | |
| # Path to the test image | |
| IMAGE_PATH = "test_face.jpg" | |
| def download_test_image(): | |
| if not os.path.exists(IMAGE_PATH): | |
| print(f"Downloading test image to {IMAGE_PATH}...") | |
| url = "https://raw.githubusercontent.com/opencv/opencv/master/samples/data/lena.jpg" | |
| response = requests.get(url) | |
| if response.status_code == 200: | |
| with open(IMAGE_PATH, 'wb') as f: | |
| f.write(response.content) | |
| print("Downloaded successfully.") | |
| else: | |
| print("Failed to download test image.") | |
| sys.exit(1) | |
| def run_tests(): | |
| print("--- VisionAttend Integration Tests ---") | |
| # Generate unique roll number to avoid constraint errors | |
| roll_number = f"TEST-{uuid.uuid4().hex[:6].upper()}" | |
| student_name = "Test Student" | |
| student_id = None | |
| print("\n[1] Testing Registration...") | |
| try: | |
| with open(IMAGE_PATH, 'rb') as f: | |
| files = {'image': (IMAGE_PATH, f, 'image/jpeg')} | |
| data = {'name': student_name, 'roll_number': roll_number} | |
| response = requests.post(f"{BASE_URL}/api/register", data=data, files=files) | |
| if response.status_code == 200: | |
| result = response.json() | |
| student_id = result.get('student_id') | |
| print(f"β Registration successful. Student ID: {student_id}") | |
| else: | |
| print(f"β Registration failed: {response.text}") | |
| return | |
| except Exception as e: | |
| print(f"β Registration exception: {e}") | |
| return | |
| print("\n[2] Testing Recognition & Attendance Marking...") | |
| try: | |
| with open(IMAGE_PATH, 'rb') as f: | |
| files = {'image': (IMAGE_PATH, f, 'image/jpeg')} | |
| response = requests.post(f"{BASE_URL}/api/recognize", files=files) | |
| if response.status_code == 200: | |
| result = response.json() | |
| print(f"β Recognition successful: {result['message']} - {result['student_name']} ({result['roll_number']})") | |
| assert result['roll_number'] == roll_number | |
| else: | |
| print(f"β Recognition failed: {response.text}") | |
| except Exception as e: | |
| print(f"β Recognition exception: {e}") | |
| print("\n[3] Testing Attendance Logs Query...") | |
| try: | |
| today_str = date.today().strftime("%Y-%m-%d") | |
| response = requests.get(f"{BASE_URL}/api/attendance?date_filter={today_str}") | |
| if response.status_code == 200: | |
| logs = response.json().get('logs', []) | |
| found = False | |
| for log in logs: | |
| if log.get('student_id') == str(student_id): | |
| found = True | |
| print(f"β Found student in logs. Status: {log.get('status')}") | |
| assert log.get('status') == 'Present' | |
| break | |
| if not found: | |
| print("β Student not found in attendance logs.") | |
| else: | |
| print(f"β Attendance query failed: {response.text}") | |
| except Exception as e: | |
| print(f"β Attendance query exception: {e}") | |
| print("\n[4] Testing Manual Attendance Update...") | |
| try: | |
| data = { | |
| "student_id": str(student_id), | |
| "date": date.today().strftime("%Y-%m-%d"), | |
| "status": "Absent" | |
| } | |
| response = requests.post(f"{BASE_URL}/api/attendance/manual", json=data) | |
| if response.status_code == 200: | |
| print("β Manual attendance marked successfully.") | |
| else: | |
| print(f"β Manual attendance failed: {response.text}") | |
| except Exception as e: | |
| print(f"β Manual attendance exception: {e}") | |
| print("\n[5] Testing Student Details Update...") | |
| try: | |
| new_name = "Updated Test Student" | |
| # We'll use the same roll_number as checking existing logic | |
| data = { | |
| "name": new_name, | |
| "roll_number": roll_number | |
| } | |
| response = requests.put(f"{BASE_URL}/api/students/{student_id}", json=data) | |
| if response.status_code == 200: | |
| print("β Student details updated successfully.") | |
| else: | |
| print(f"β Student detail update failed: {response.text}") | |
| except Exception as e: | |
| print(f"β Student detail update exception: {e}") | |
| print("\n[6] Testing Student Deletion (Cleanup)...") | |
| if student_id: | |
| try: | |
| response = requests.delete(f"{BASE_URL}/api/students/{student_id}") | |
| if response.status_code == 200: | |
| print("β Student deleted successfully.") | |
| else: | |
| print(f"β Student deletion failed: {response.text}") | |
| except Exception as e: | |
| print(f"β Student deletion exception: {e}") | |
| if __name__ == "__main__": | |
| download_test_image() | |
| run_tests() | |