|
|
|
|
# import requests
|
|
|
|
|
# url = 'http://127.0.0.1:8000/call/students/' # 确保 URL 正确
|
|
|
|
|
#
|
|
|
|
|
# data = {
|
|
|
|
|
# 'class_name': '7班',
|
|
|
|
|
# 'num': 2,
|
|
|
|
|
# 'mode': 0
|
|
|
|
|
# }
|
|
|
|
|
# response = requests.post(url, data=data)
|
|
|
|
|
# print(response.text)
|
|
|
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
from app01.models import Student
|
|
|
|
|
class CallStudentsTestCase(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
# 创建一些测试数据
|
|
|
|
|
Student.objects.create(name='学生1', sid='001', credit=0.0, class_name='测试班级1', probability=1, max=0.0, min=0.0)
|
|
|
|
|
Student.objects.create(name='学生2', sid='002', credit=0.0, class_name='测试班级1', probability=1, max=0.0, min=0.0)
|
|
|
|
|
|
|
|
|
|
def test_call_students_with_valid_post_request(self):
|
|
|
|
|
# 测试带有有效参数的POST请求
|
|
|
|
|
url = reverse('call_students')
|
|
|
|
|
data = {
|
|
|
|
|
'class_name': '测试班级1',
|
|
|
|
|
'num': 2,
|
|
|
|
|
'mode': 1
|
|
|
|
|
}
|
|
|
|
|
response = self.client.post(url, data)
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
|
def test_call_students_with_get_request(self):
|
|
|
|
|
# 测试GET请求
|
|
|
|
|
url = reverse('call_students')
|
|
|
|
|
response = self.client.get(url)
|
|
|
|
|
self.assertEqual(response.status_code, 200) # 或者是200,视图处理决定
|
|
|
|
|
self.assertIn('error', response.json())
|
|
|
|
|
|
|
|
|
|
def test_call_students_without_class_name(self):
|
|
|
|
|
# 测试参数缺少班级名称的POST请求
|
|
|
|
|
url = reverse('call_students')
|
|
|
|
|
data = {
|
|
|
|
|
'num': 2,
|
|
|
|
|
'mode': 1
|
|
|
|
|
}
|
|
|
|
|
response = self.client.post(url, data)
|
|
|
|
|
# 检查响应状态码和内容
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(response.json()['message'], 0)
|
|
|
|
|
self.assertEqual(response.json()['error'], '缺少必要的参数')
|
|
|
|
|
|
|
|
|
|
def test_call_students_without_num(self):
|
|
|
|
|
# 测试参数缺少数量的POST请求
|
|
|
|
|
url = reverse('call_students')
|
|
|
|
|
data = {
|
|
|
|
|
'class_name': '测试班级1',
|
|
|
|
|
'mode': 1
|
|
|
|
|
}
|
|
|
|
|
response = self.client.post(url, data)
|
|
|
|
|
# 检查响应状态码和内容
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(response.json()['message'], 0)
|
|
|
|
|
self.assertEqual(response.json()['error'], '缺少必要的参数')
|
|
|
|
|
|
|
|
|
|
def test_call_students_without_mode(self):
|
|
|
|
|
# 测试参数缺少模式的POST请求
|
|
|
|
|
url = reverse('call_students')
|
|
|
|
|
data = {
|
|
|
|
|
'class_name': '测试班级1',
|
|
|
|
|
'num': 2
|
|
|
|
|
}
|
|
|
|
|
response = self.client.post(url, data)
|
|
|
|
|
# 检查响应状态码和内容
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
self.assertEqual(response.json()['message'], 0)
|
|
|
|
|
self.assertEqual(response.json()['error'], '缺少必要的参数')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|