# import requests # import json # # url = 'http://127.0.0.1:8000/show/information/' # 确保 URL 正确 # # data = { # 'order':1, # 'class_name': '软件工程', # 'sid':102201539 , # } # # response = requests.post(url, data=data) # data1 = response.json() # print(response.text) from django.test import TestCase from django.urls import reverse from app01.models import Student from django.db.models import Window, F from django.db.models.functions import Rank class ShowInformationTestCase(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_show_information(self): # 测试带有有效参数的POST请求 url = reverse('show_information') data = { 'class_name': '测试班级1', 'sid': '001', 'order': '1' } response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['sid'], '001') self.assertEqual(response.json()['name'], '学生1') self.assertEqual(response.json()['student_rank'], 1) def test_show_information_with_get_request(self): # 测试GET请求 url = reverse('show_information') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['message'], 0) self.assertEqual(response.json()['error'], '请求方法错误') def test_show_information_without_class_name(self): # 测试缺少班级的请求 url = reverse('show_information') data = { 'sid': '001', 'order': '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_show_information_without_sid(self): # 测试缺少学号的请求 url = reverse('show_information') data = { 'class_name':'测试班级1', 'order': '1' } response = self.client.post(url, data) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['message'], 0) self.assertEqual(response.json()['error'], '缺少必要的参数')