-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick_test_classrooms.py
More file actions
233 lines (196 loc) · 8.46 KB
/
quick_test_classrooms.py
File metadata and controls
233 lines (196 loc) · 8.46 KB
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
#!/usr/bin/env python3
"""
Quick test script for Classroom Management APIs
Simple script to test basic classroom operations.
Run this after starting the server: python main.py
"""
import requests
import json
import time
# Configuration
BASE_URL = "http://localhost:8000"
ROBOT_ID = "robot_001"
def test_add_classroom_form(classroom_name, robot_id, device_id):
"""Test adding classroom with form data"""
print(f"📝 Adding classroom '{classroom_name}' with robot '{robot_id}' and device '{device_id}' (form)...")
try:
data = {
"classroom_name": classroom_name,
"robot_id": robot_id,
"device_id": device_id
}
response = requests.post(f"{BASE_URL}/classrooms/add/form", data=data)
if response.status_code == 200:
result = response.json()
print(f"✅ Added classroom: {result.get('message', 'N/A')}")
return True
else:
print(f"❌ Failed to add classroom: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error adding classroom: {e}")
return False
def test_add_classroom_json(classroom_name, robot_id, device_id):
"""Test adding classroom with JSON"""
print(f"📝 Adding classroom '{classroom_name}' with robot '{robot_id}' and device '{device_id}' (JSON)...")
try:
json_data = {
"classroom_name": classroom_name,
"robot_id": robot_id,
"device_id": device_id
}
response = requests.post(f"{BASE_URL}/classrooms/add", json=json_data)
if response.status_code == 200:
result = response.json()
print(f"✅ Added classroom: {result.get('message', 'N/A')}")
return True
else:
print(f"❌ Failed to add classroom: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error adding classroom: {e}")
return False
def test_get_classrooms():
"""Test getting all classrooms"""
print(f"📋 Getting all classrooms...")
try:
response = requests.get(f"{BASE_URL}/classrooms/list")
if response.status_code == 200:
result = response.json()
classrooms = result.get('classrooms', [])
print(f"✅ Found {len(classrooms)} classrooms:")
for classroom in classrooms:
print(f" - Classroom: {classroom.get('classroom_name')}, Robot: {classroom.get('robot_id')}, Device: {classroom.get('device_id')}")
return True
else:
print(f"❌ Failed to get classrooms: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error getting classrooms: {e}")
return False
def test_get_classrooms_by_robot(robot_id):
"""Test getting classrooms for a specific robot"""
print(f"📋 Getting classrooms for robot '{robot_id}'...")
try:
params = {"robot_id": robot_id}
response = requests.get(f"{BASE_URL}/classrooms/list/by-robot", params=params)
if response.status_code == 200:
result = response.json()
classrooms = result.get('classrooms', [])
print(f"✅ Found {len(classrooms)} classrooms for robot '{robot_id}':")
for classroom in classrooms:
print(f" - Classroom: {classroom.get('classroom_name')}, Device: {classroom.get('device_id')}")
return True
else:
print(f"❌ Failed to get classrooms: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error getting classrooms: {e}")
return False
def test_get_all_robots():
"""Test getting all robots (legacy endpoint)"""
print(f"📋 Getting all robots...")
try:
response = requests.get(f"{BASE_URL}/classrooms/all")
if response.status_code == 200:
result = response.json()
robots = result.get('robots', [])
print(f"✅ Found {len(robots)} robots:")
for robot in robots:
robot_id = robot.get('robot_id', 'Unknown')
devices = robot.get('device', [])
print(f" - Robot: {robot_id} ({len(devices)} classrooms)")
for device in devices:
print(f" * Device: {device.get('device_id')}, Classroom: {device.get('room_name')}")
return True
else:
print(f"❌ Failed to get robots: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error getting robots: {e}")
return False
def test_update_classroom(classroom_name, robot_id, device_id):
"""Test updating classroom"""
print(f"✏️ Updating classroom '{classroom_name}' with robot '{robot_id}' and device '{device_id}'...")
try:
data = {
"classroom_name": classroom_name,
"robot_id": robot_id,
"device_id": device_id
}
response = requests.put(f"{BASE_URL}/classrooms/update/form", data=data)
if response.status_code == 200:
result = response.json()
print(f"✅ Updated classroom: {result.get('message', 'N/A')}")
return True
else:
print(f"❌ Failed to update classroom: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error updating classroom: {e}")
return False
def test_delete_classroom(classroom_name):
"""Test deleting classroom"""
print(f"🗑️ Deleting classroom '{classroom_name}'...")
try:
params = {"classroom_name": classroom_name}
response = requests.delete(f"{BASE_URL}/classrooms/delete", params=params)
if response.status_code == 200:
result = response.json()
print(f"✅ Deleted classroom: {result.get('message', 'N/A')}")
return True
else:
print(f"❌ Failed to delete classroom: {response.status_code} - {response.text}")
return False
except Exception as e:
print(f"❌ Error deleting classroom: {e}")
return False
def test_validation_errors():
"""Test validation error handling"""
print("🔍 Testing validation errors...")
# Test empty robot_id
print(" Testing empty robot_id...")
data = {"classroom_name": "test", "robot_id": "", "device_id": "test"}
response = requests.post(f"{BASE_URL}/classrooms/add", json=data)
print(f" Status: {response.status_code} - Expected: 400")
# Test empty device_id
print(" Testing empty device_id...")
data = {"classroom_name": "test", "robot_id": "test", "device_id": ""}
response = requests.post(f"{BASE_URL}/classrooms/add", json=data)
print(f" Status: {response.status_code} - Expected: 400")
# Test empty classroom_name
print(" Testing empty classroom_name...")
data = {"classroom_name": "", "robot_id": "test", "device_id": "test"}
response = requests.post(f"{BASE_URL}/classrooms/add", json=data)
print(f" Status: {response.status_code} - Expected: 400")
def main():
print("🚀 Starting Quick Classroom API Tests")
print("=" * 50)
# Test data
test_classrooms = [
("Classroom A", "robot_001", "tv_001"),
("Classroom B", "robot_002", "tv_002"),
("Classroom C", "robot_003", "tv_003")
]
# Test adding classrooms
print("\n📝 Testing Add Classroom Operations:")
for classroom_name, robot_id, device_id in test_classrooms:
test_add_classroom_form(classroom_name, robot_id, device_id)
test_add_classroom_json(classroom_name, robot_id, device_id)
# Test getting classrooms
print("\n📋 Testing Get Classroom Operations:")
test_get_classrooms()
test_get_classrooms_by_robot("robot_001")
# Test updating classrooms
print("\n✏️ Testing Update Classroom Operations:")
test_update_classroom("Classroom A", "robot_001", "tv_updated_001")
# Test validation
print("\n🔍 Testing Validation:")
test_validation_errors()
# Test deleting classrooms
print("\n🗑️ Testing Delete Classroom Operations:")
for classroom_name, robot_id, device_id in test_classrooms:
test_delete_classroom(classroom_name)
print("\n✅ Quick tests completed!")
if __name__ == "__main__":
main()