visionattend-api / test_core_flows.py
Shevilll's picture
Implemented the first version of the application, handled the UI, face recognition, and database handling
5fde056
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()