File size: 7,695 Bytes
e310cf5 | 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 | """Tests for API endpoints."""
from datetime import date, time
import pytest
class TestHealthEndpoints:
"""Test health check endpoints."""
def test_root_endpoint(self, client):
"""Root endpoint should return OK status."""
response = client.get("/")
assert response.status_code == 200
assert response.json()["status"] == "ok"
def test_health_endpoint(self, client):
"""Health endpoint should return healthy status."""
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
class TestLookupEndpoints:
"""Test lookup API endpoints."""
def test_get_rooms(self, client, seed_data):
"""Should return all rooms."""
response = client.get("/rooms")
assert response.status_code == 200
rooms = response.json()
assert len(rooms) == 3
assert all("name" in r and "capacity" in r for r in rooms)
def test_get_instructors(self, client, seed_data):
"""Should return all instructors."""
response = client.get("/instructors")
assert response.status_code == 200
instructors = response.json()
assert len(instructors) == 2
def test_get_students(self, client, seed_data):
"""Should return all students."""
response = client.get("/students")
assert response.status_code == 200
students = response.json()
assert len(students) == 10
class TestClassCRUD:
"""Test class CRUD operations."""
def test_create_class(self, client, seed_data):
"""Should create a new class."""
response = client.post("/classes", json={
"course_name": "Test Course",
"chapter_topic": "Test Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [1, 2, 3],
})
assert response.status_code == 201
data = response.json()
assert data["course_name"] == "Test Course"
assert len(data["students"]) == 3
def test_get_classes_by_date(self, client, seed_data):
"""Should return classes for a specific date."""
# First create a class
client.post("/classes", json={
"course_name": "Test Course",
"chapter_topic": "Test Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [],
})
response = client.get("/classes?date=2024-01-15")
assert response.status_code == 200
classes = response.json()
assert len(classes) == 1
def test_update_class(self, client, seed_data):
"""Should update an existing class."""
# Create
create_response = client.post("/classes", json={
"course_name": "Original Name",
"chapter_topic": "Original Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [],
})
class_id = create_response.json()["id"]
# Update
update_response = client.put(f"/classes/{class_id}", json={
"course_name": "Updated Name",
"chapter_topic": "Updated Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [1],
})
assert update_response.status_code == 200
assert update_response.json()["course_name"] == "Updated Name"
assert len(update_response.json()["students"]) == 1
def test_delete_class(self, client, seed_data):
"""Should delete a class."""
# Create
create_response = client.post("/classes", json={
"course_name": "To Delete",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [],
})
class_id = create_response.json()["id"]
# Delete
delete_response = client.delete(f"/classes/{class_id}")
assert delete_response.status_code == 204
# Verify deleted
get_response = client.get(f"/classes/{class_id}")
assert get_response.status_code == 404
class TestConflictAPI:
"""Test conflict detection via API."""
def test_room_conflict_returns_409(self, client, seed_data):
"""Creating a class with room conflict should return 409."""
# Create first class
client.post("/classes", json={
"course_name": "First Class",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [],
})
# Try to create conflicting class
response = client.post("/classes", json={
"course_name": "Conflicting Class",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "11:00:00", # Overlaps
"end_time": "12:30:00",
"room_id": 1, # Same room
"instructor_id": 2, # Different instructor
"student_ids": [],
})
assert response.status_code == 409
detail = response.json()["detail"]
assert detail["error"] == "conflict"
assert any(c["type"] == "room" for c in detail["conflicts"])
def test_adjacent_times_no_conflict(self, client, seed_data):
"""Adjacent times should not conflict."""
# Create first class
client.post("/classes", json={
"course_name": "First Class",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:00:00",
"room_id": 1,
"instructor_id": 1,
"student_ids": [],
})
# Create adjacent class (should succeed)
response = client.post("/classes", json={
"course_name": "Adjacent Class",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "11:00:00", # Starts exactly when first ends
"end_time": "12:00:00",
"room_id": 1, # Same room
"instructor_id": 1, # Same instructor
"student_ids": [],
})
assert response.status_code == 201
def test_capacity_conflict_returns_409(self, client, seed_data):
"""Creating a class with too many students should return 409."""
response = client.post("/classes", json={
"course_name": "Overcrowded Class",
"chapter_topic": "Topic",
"date": "2024-01-15",
"start_time": "10:00:00",
"end_time": "11:30:00",
"room_id": 3, # Small Room with capacity=5
"instructor_id": 1,
"student_ids": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], # 10 students
})
assert response.status_code == 409
detail = response.json()["detail"]
assert any(c["type"] == "capacity" for c in detail["conflicts"])
|