Compare commits
8 Commits
eb074e5dd5
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| dbeeb4d25a | |||
| 58c4b85e6a | |||
| b6fc41994e | |||
| 1a08ba460c | |||
| 692631e395 | |||
| 1174b225a7 | |||
| ded28f6a1e | |||
| 4e64fb2422 |
43
app/tests/ReceiveExcelTest.py
Normal file
43
app/tests/ReceiveExcelTest.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import unittest
|
||||||
|
import os
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
from werkzeug.datastructures import FileStorage # 用于创建测试文件
|
||||||
|
|
||||||
|
class ReceiveExcelTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
app.testing = True # 设置 Flask 应用为测试模式
|
||||||
|
self.client = app.test_client()
|
||||||
|
# 创建一个测试用的Excel文件或使用一个真实的Excel文件路径
|
||||||
|
self.test_file_path = 'template.xlsx'
|
||||||
|
|
||||||
|
def test_receive_excel_no_file(self):
|
||||||
|
# 测试没有文件时的情况
|
||||||
|
response = self.client.post('/api/receive-excel')
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
self.assertEqual(response.get_json(), {"error": "No file part"})
|
||||||
|
|
||||||
|
def test_receive_excel_with_file(self):
|
||||||
|
# 模拟已登录用户
|
||||||
|
with self.client.session_transaction() as sess:
|
||||||
|
sess['number'] = 'X202301000001'
|
||||||
|
|
||||||
|
# 使用 FileStorage 创建测试文件对象
|
||||||
|
data = {
|
||||||
|
'file': (open(self.test_file_path, 'rb'), self.test_file_path)
|
||||||
|
}
|
||||||
|
# 发送 POST 请求到 /api/receive-excel
|
||||||
|
response = self.client.post('/api/receive-excel', data=data, content_type='multipart/form-data')
|
||||||
|
|
||||||
|
# 验证返回的状态码和响应
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.get_json(), {"message": "File successfully processed"})
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
# 清理测试用的文件或数据
|
||||||
|
if os.path.exists(self.test_file_path):
|
||||||
|
os.remove(self.test_file_path)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
39
app/tests/StudentSignInTest.py
Normal file
39
app/tests/StudentSignInTest.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
from unittest.mock import patch
|
||||||
|
from flask import session
|
||||||
|
|
||||||
|
|
||||||
|
class StudentSignInTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# 设置 Flask 测试模式
|
||||||
|
app.testing = True
|
||||||
|
self.client = app.test_client()
|
||||||
|
|
||||||
|
@patch('db.database_manager.DatabaseManager.update_sign_in_info')
|
||||||
|
def test_student_sign_in(self, mock_update_sign_in_info):
|
||||||
|
# 模拟已登录用户
|
||||||
|
with self.client.session_transaction() as sess:
|
||||||
|
sess['number'] = 'X202301000001'
|
||||||
|
|
||||||
|
# 模拟数据库操作
|
||||||
|
mock_update_sign_in_info.return_value = 1 # 假设成功更新签到信息
|
||||||
|
|
||||||
|
# 发送 POST 请求到/api/student-sign-in
|
||||||
|
response = self.client.post('/api/student-sign-in', data={
|
||||||
|
'course_name': '计算机导论',
|
||||||
|
'course_id': '2',
|
||||||
|
'student_number': 'X202301000001',
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
# 验证返回的状态码和JSON数据
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
json_data = response.get_json()
|
||||||
|
self.assertEqual(json_data, {"msg": "ok", "data": "签到成功!"})
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
37
app/tests/TeacherSignInTest.py
Normal file
37
app/tests/TeacherSignInTest.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
from unittest.mock import patch
|
||||||
|
from flask import session
|
||||||
|
|
||||||
|
class TeacherSignInTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# 设置 Flask 测试模式
|
||||||
|
app.testing = True
|
||||||
|
self.client = app.test_client()
|
||||||
|
|
||||||
|
@patch('db.database_manager.DatabaseManager.update_sign_in_info')
|
||||||
|
def test_teacher_sign_in(self, mock_teacher_sign_in):
|
||||||
|
# 模拟已登录用户
|
||||||
|
with self.client.session_transaction() as sess:
|
||||||
|
sess['number'] = 'X202301000001'
|
||||||
|
|
||||||
|
# 模拟数据库操作的返回值
|
||||||
|
mock_teacher_sign_in.return_value = {"msg": "当前班级专业没有学生'"}
|
||||||
|
|
||||||
|
# 发送 POST 请求到/api/teacher-sign-in
|
||||||
|
response = self.client.post('/api/teacher-sign-in', data={
|
||||||
|
'course_id': '123',
|
||||||
|
'course_name': 'Test Course',
|
||||||
|
'class_name': 'TestClass',
|
||||||
|
'major_id': '456'
|
||||||
|
})
|
||||||
|
|
||||||
|
# 验证返回的状态码和数据
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
json_data = response.get_json()
|
||||||
|
self.assertEqual(json_data, {"msg": "当前班级专业没有学生"})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
73
app/tests/TestGetHtml.py
Normal file
73
app/tests/TestGetHtml.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
|
||||||
|
|
||||||
|
class HtmlTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
app.testing = True # 设置 Flask 应用为测试模式
|
||||||
|
self.client = app.test_client() # 创建一个测试客户端
|
||||||
|
|
||||||
|
def test_get_course_info(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/course-info')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
def test_get_attendance(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/attendance')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
def test_get_announcement(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/announcement')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
def test_get_attendance_reminder(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/attendance-reminder')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
def test_get_course_category(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/course-category')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
def test_get_attendance_teacher(self):
|
||||||
|
# 测试 GET 请求
|
||||||
|
response = self.client.get('/attendance-teacher')
|
||||||
|
|
||||||
|
# 验证响应状态码
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
|
# 验证响应的Content-Type头部值
|
||||||
|
self.assertIn('text/html', response.content_type)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
44
app/tests/TestGetMenu.py
Normal file
44
app/tests/TestGetMenu.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
from flask import session
|
||||||
|
from unittest.mock import patch # 如果需要模拟数据库方法
|
||||||
|
|
||||||
|
|
||||||
|
class MenuAPITestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
app.testing = True # 设置 Flask 应用为测试模式
|
||||||
|
self.client = app.test_client() # 创建一个测试客户端
|
||||||
|
self.expected_menu = [
|
||||||
|
{"name": "课程类别", "path": "/course-category"},
|
||||||
|
{"name": "课程信息", "path": "/course-info"},
|
||||||
|
{"name": "课程签到", "path": "/attendance-teacher"},
|
||||||
|
{"name": "签到提醒", "path": "/attendance-reminder"}
|
||||||
|
]
|
||||||
|
|
||||||
|
def test_menu_no_login(self):
|
||||||
|
# 测试未登录情况下请求API
|
||||||
|
response = self.client.get('/api/menu')
|
||||||
|
self.assertEqual(response.status_code, 401)
|
||||||
|
self.assertEqual(response.get_json(), [])
|
||||||
|
|
||||||
|
def test_login(self):
|
||||||
|
with self.client:
|
||||||
|
self.client.post('/login', data=dict(
|
||||||
|
number='G0001',
|
||||||
|
password='1'
|
||||||
|
))
|
||||||
|
|
||||||
|
# 确认登录后session中有数据
|
||||||
|
self.assertIn('role', session)
|
||||||
|
|
||||||
|
# 执行logout
|
||||||
|
response = self.client.get('/api/menu')
|
||||||
|
print(response.get_json())
|
||||||
|
# 确认响应是重定向到登录页面
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(self.expected_menu,response.get_json())
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
34
app/tests/TestLogout.py
Normal file
34
app/tests/TestLogout.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from app.views import *
|
||||||
|
|
||||||
|
class LogoutTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# 设置 Flask 测试模式
|
||||||
|
app.testing = True
|
||||||
|
self.client = app.test_client()
|
||||||
|
|
||||||
|
def test_logout(self):
|
||||||
|
# 先登录,确保 session 是设置的
|
||||||
|
with self.client:
|
||||||
|
self.client.post('/login', data=dict(
|
||||||
|
number='G0001',
|
||||||
|
password='1'
|
||||||
|
))
|
||||||
|
|
||||||
|
# 确认登录后session中有数据
|
||||||
|
self.assertIn('number', session)
|
||||||
|
|
||||||
|
# 执行logout
|
||||||
|
response = self.client.get('/logout')
|
||||||
|
|
||||||
|
# 确认 session 被清空
|
||||||
|
self.assertNotIn('number', session)
|
||||||
|
|
||||||
|
# 确认响应是重定向到登录页面
|
||||||
|
self.assertEqual(response.status_code, 302)
|
||||||
|
self.assertTrue(response.location.endswith('/login'))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
bcrypt==4.1.2
|
|
||||||
blinker==1.7.0
|
|
||||||
click==8.1.7
|
|
||||||
colorama==0.4.6
|
|
||||||
DBUtils==3.0.3
|
|
||||||
et-xmlfile==1.1.0
|
|
||||||
Flask==3.0.0
|
|
||||||
itsdangerous==2.1.2
|
|
||||||
Jinja2==3.1.2
|
|
||||||
MarkupSafe==2.1.3
|
|
||||||
openpyxl==3.1.2
|
|
||||||
PyMySQL==1.1.0
|
|
||||||
Werkzeug==3.0.1
|
|
||||||
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Reference in New Issue
Block a user