From 1a08ba460c6422dbc8780bc7c0c594dc9cbcfda4 Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Fri, 29 Dec 2023 17:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=20StudentSignInTest.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/tests/StudentSignInTest.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 app/tests/StudentSignInTest.py diff --git a/app/tests/StudentSignInTest.py b/app/tests/StudentSignInTest.py new file mode 100644 index 0000000..0dfb64f --- /dev/null +++ b/app/tests/StudentSignInTest.py @@ -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()