You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# import requests
# import json
# url = 'http://127.0.0.1:8000/get/information/'
# class_name = '5班'
# file_path = 'C:/Users/Lenovo/Desktop/5班.xlsx'
#
# with open(file_path, 'rb') as f:
# files = {'file': f}
# data = {'class_name': class_name}
# response = requests.post(url, files=files, data=data)
# print(response.text)
from django.test import TestCase
from django.urls import reverse
from django.core.files.uploadedfile import SimpleUploadedFile
from app01.models import Student
class GetInformationTestCase(TestCase):
def setUp(self):
# 创建测试用的班级
Student.objects.create(
name='测试学生1',
sid='001',
credit=3.5,
class_name='测试班级1',
probability=0.9,
max=0.0,
min=0.0
)
def test_getinformation_with_wrong_file(self):
# 测试带有不同格式文件的POST请求
url = reverse('get_information')
class_name = '测试班级2'
file_content = b'some,data' # CSV文件内容
file = SimpleUploadedFile('test_file.csv', file_content, content_type='text/csv')
data = {'class_name': class_name, 'file': file}
response = self.client.post(url, data)
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['message'], 0)
def test_getinformation_without_file(self):
url = reverse('get_information')
class_name = '测试班级'
response = self.client.post(url, {'class_name': class_name})
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['message'], 0)
self.assertIn('缺少必要的参数', response.json()['error'])
def test_getinformation_class_name(self):
# 测试没有班级名称的POST请求
url = reverse('get_information')
file_content = b'some,data' # excel文件内容
file = SimpleUploadedFile('test_file.xlsx', file_content, content_type='text/xlsx')
response = self.client.post(url, {'file': file})
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['message'], 0)
self.assertIn('缺少必要的参数', response.json()['error'])
def test_getinformation_with_empty_file(self):
# 测试上传空文件的POST请求
url = reverse('get_information')
class_name = '测试班级'
file = SimpleUploadedFile('test_file.xlsx', b'', content_type='text/xlsx')
response = self.client.post(url, {'class_name': class_name}, FILES={'file': file})
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['message'], 0)
self.assertIn('缺少必要的参数', response.json()['error'])
def test_getinformation_not_post(self):
url = reverse('get_information')
response = self.client.get(url)
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertIn('error', response.json())
def test_getinformation_with_put_request(self):
# 测试PUT请求
url = reverse('get_information')
response = self.client.put(url)
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200)
self.assertIn('error', response.json())
def test_getinformation_with_delete_request(self):
# 测试DELETE请求
url = reverse('get_information')
response = self.client.delete(url)
# 检查响应状态码和内容
self.assertEqual(response.status_code, 200) # 或者是200视图处理决定
self.assertIn('error', response.json())