Compare commits
No commits in common. 'master2' and 'master' have entirely different histories.
@ -1,4 +1,3 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
/.idea
|
||||
@ -0,0 +1 @@
|
||||
__init__.cpython-311.pyc
|
||||
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
||||
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.11" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/新建文件夹.iml" filepath="$PROJECT_DIR$/.idea/新建文件夹.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="jdk" jdkName="Python 3.12" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@ -1,36 +1,31 @@
|
||||
# stumis/bll/auth_bll.py
|
||||
import json
|
||||
import bcrypt
|
||||
from stumis.model.user import User
|
||||
|
||||
class AuthBLL:
|
||||
def __init__(self, users_file: str):
|
||||
self.users_file = users_file
|
||||
self.users = self._load_users()
|
||||
|
||||
def _load_users(self) -> dict[str, User]:
|
||||
def _load_users(self) -> Dict[str, User]:
|
||||
try:
|
||||
with open(self.users_file, 'r') as f:
|
||||
data = json.load(f)
|
||||
return {user['username']: User(user['username'], user['password'].encode(), user['role']) for user in data}
|
||||
return {user['username']: User(**user) for user in data}
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
def authenticate(self, username: str, password: str) -> User | None:
|
||||
def authenticate(self, username: str, password: str) -> Optional[User]:
|
||||
user = self.users.get(username)
|
||||
if user and bcrypt.checkpw(password.encode(), user.password):
|
||||
if user and user.password == password: # 实际应比较哈希值
|
||||
return user
|
||||
return None
|
||||
|
||||
def add_user(self, user: User) -> bool:
|
||||
if user.username in self.users:
|
||||
return False
|
||||
hashed = bcrypt.hashpw(user.password.encode(), bcrypt.gensalt())
|
||||
user.password = hashed.decode()
|
||||
self.users[user.username] = user
|
||||
self._save_users()
|
||||
return True
|
||||
|
||||
def _save_users(self):
|
||||
with open(self.users_file, 'w') as f:
|
||||
json.dump([vars(user) for user in self.users.values()], f)
|
||||
json.dump([vars(user) for user in self.users.values()], f)
|
||||
@ -1 +0,0 @@
|
||||
print(123)
|
||||
@ -1,48 +1,39 @@
|
||||
# test_student_bll.py
|
||||
# test_bll.py
|
||||
from stumis.bll.studentBLL import StudentBLL
|
||||
from stumis.model.student import Student
|
||||
import os
|
||||
import pytest
|
||||
from datetime import date
|
||||
import uuid
|
||||
|
||||
@pytest.fixture
|
||||
def student_bll(tmp_path):
|
||||
file_path = tmp_path / "test_students.json"
|
||||
bll = StudentBLL(str(file_path))
|
||||
yield bll
|
||||
# 清理资源(如果需要)
|
||||
|
||||
@pytest.fixture
|
||||
def test_student():
|
||||
unique_suffix = uuid.uuid4().hex[:6]
|
||||
return Student(
|
||||
name=f"测试学生_{unique_suffix}",
|
||||
id_card=f"110101{date.today().year}{date.today().month:02d}{date.today().day:02d}{unique_suffix}",
|
||||
stu_id=f"{date.today().year}{int(date.today().strftime('%m%d'))}{unique_suffix}",
|
||||
def test_student_bll():
|
||||
file_path = "test_students.json"
|
||||
bll = StudentBLL(file_path)
|
||||
|
||||
# 使用合法的身份证号码
|
||||
test_student = Student(
|
||||
name="测试学生",
|
||||
id_card="110101200001011234", # 2000年1月1日出生
|
||||
stu_id="2025001",
|
||||
gender=True,
|
||||
height=175,
|
||||
weight=70.5,
|
||||
enrollment_date=f"{date.today().year}-09-01",
|
||||
class_name=f"软件工程1班_{unique_suffix}",
|
||||
enrollment_date="2025-09-01",
|
||||
class_name="软件工程1班",
|
||||
major="计算机科学与技术"
|
||||
)
|
||||
|
||||
def test_add_student(student_bll, test_student):
|
||||
# 测试添加学生
|
||||
print("测试添加学生...")
|
||||
result = student_bll.add(test_student)
|
||||
assert result is True
|
||||
result = bll.add(test_student)
|
||||
print("添加结果:", result)
|
||||
|
||||
# 测试查询学生
|
||||
print("测试查询学生...")
|
||||
retrieved_student = student_bll.get_by_id(test_student.id_card)
|
||||
assert retrieved_student is not None
|
||||
assert retrieved_student.name == test_student.name
|
||||
print("查询成功:", retrieved_student.name)
|
||||
retrieved_student = bll.get_by_id("110101200001011234")
|
||||
if retrieved_student:
|
||||
print("查询成功:", retrieved_student.name)
|
||||
else:
|
||||
print("查询失败")
|
||||
|
||||
print("所有测试完成!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__])
|
||||
test_student_bll()
|
||||
@ -1 +1,13 @@
|
||||
[]
|
||||
[
|
||||
{
|
||||
"name": "陈浩",
|
||||
"id_card": "522132200412245917",
|
||||
"stu_id": "110309230907019",
|
||||
"gender": true,
|
||||
"height": 169,
|
||||
"weight": 55.0,
|
||||
"enrollment_date": "2023-09-01",
|
||||
"class_name": "4",
|
||||
"major": "语文"
|
||||
}
|
||||
]
|
||||
Loading…
Reference in new issue