247 lines
12 KiB
Python
247 lines
12 KiB
Python
"""
|
|
测试数据初始化脚本
|
|
使用纯SQL直接向数据库插入测试数据,不修改任何原有项目文件
|
|
用法: python init_test_data.py
|
|
"""
|
|
from datetime import date
|
|
from databases import engine_all, Session
|
|
from sqlalchemy import text
|
|
|
|
Session_ = Session()
|
|
|
|
|
|
def seed():
|
|
print("=== 开始插入测试数据 ===\n")
|
|
|
|
# ── 1. 科目 ──────────────────────────────────────────
|
|
subjects = ["Python程序设计", "数据结构", "数据库原理", "人工智能导论", "计算机网络", "操作系统"]
|
|
sub_ids = {}
|
|
for name in subjects:
|
|
r = Session_.execute(text("SELECT id FROM subject WHERE name = :name"), {"name": name}).fetchone()
|
|
if r:
|
|
sub_ids[name] = r[0]
|
|
else:
|
|
result = Session_.execute(text("INSERT INTO subject (name) VALUES (:name)"), {"name": name})
|
|
sub_ids[name] = result.lastrowid
|
|
print(f" [科目] {name}")
|
|
Session_.commit()
|
|
|
|
# ── 2. 教师 ──────────────────────────────────────────
|
|
teachers = [
|
|
("张伟", "m", sub_ids["Python程序设计"], 1, "北京", "Python", "博士", "清华", date(2018, 3, 1), "6年"),
|
|
("李芳", "f", sub_ids["数据结构"], 1, "上海", "算法", "博士", "北大", date(2019, 9, 1), "4年"),
|
|
("王强", "m", sub_ids["数据库原理"], 2, "广州", "MySQL", "硕士", "中山大学", date(2020, 2, 1), "3年"),
|
|
("陈静", "f", sub_ids["人工智能导论"], 2, "深圳", "机器学习", "博士", "华南理工", date(2017, 7, 1), "7年"),
|
|
("刘洋", "m", sub_ids["计算机网络"], 3, "杭州", "网络", "硕士", "浙江大学", date(2021, 1, 1), "2年"),
|
|
("赵敏", "f", sub_ids["操作系统"], 3, "南京", "Linux", "博士", "南京大学", date(2019, 3, 1), "5年"),
|
|
]
|
|
teacher_ids = []
|
|
for t in teachers:
|
|
phone = f"1380000{len(teacher_ids):04d}"
|
|
r = Session_.execute(
|
|
text("SELECT id FROM teachers WHERE phone = :phone AND is_delete = 0"),
|
|
{"phone": phone}
|
|
).fetchone()
|
|
if r:
|
|
teacher_ids.append(r[0])
|
|
else:
|
|
result = Session_.execute(text("""
|
|
INSERT INTO teachers (name, sex, class_id, phone, subject_id, home_place, specialty,
|
|
education, collage, start_time, work_experience, is_delete)
|
|
VALUES (:name, :sex, :class_id, :phone, :subject_id, :home_place, :specialty,
|
|
:education, :collage, :start_time, :work_experience, 0)
|
|
"""), {
|
|
"name": t[0], "sex": t[1], "class_id": t[3], "phone": phone,
|
|
"subject_id": t[2], "home_place": t[4], "specialty": t[5],
|
|
"education": t[6], "collage": t[7], "start_time": t[8], "work_experience": t[9],
|
|
})
|
|
teacher_ids.append(result.lastrowid)
|
|
print(f" [教师] {t[0]} ({t[1]}) - {t[5]}")
|
|
Session_.commit()
|
|
|
|
# ── 3. 班级 ──────────────────────────────────────────
|
|
classes = [
|
|
("CS202401", "计算机科学2401班", teacher_ids[0], teacher_ids[1], teacher_ids[2], date(2024, 9, 1), date(2028, 6, 30)),
|
|
("CS202402", "计算机科学2402班", teacher_ids[3], teacher_ids[4], teacher_ids[5], date(2024, 9, 1), date(2028, 6, 30)),
|
|
("AI202501", "人工智能2501班", teacher_ids[0], teacher_ids[3], teacher_ids[5], date(2025, 9, 1), date(2029, 6, 30)),
|
|
]
|
|
class_ids = []
|
|
for c in classes:
|
|
r = Session_.execute(
|
|
text("SELECT id FROM classes WHERE num = :num AND is_delete = 0"),
|
|
{"num": c[0]}
|
|
).fetchone()
|
|
if r:
|
|
class_ids.append(r[0])
|
|
else:
|
|
result = Session_.execute(text("""
|
|
INSERT INTO classes (num, name, head_teacher_id, coach_teacher_id, tutor_teacher_id,
|
|
class_start_time, class_end_time, is_delete)
|
|
VALUES (:num, :name, :head_teacher_id, :coach_teacher_id, :tutor_teacher_id,
|
|
:class_start_time, :class_end_time, 0)
|
|
"""), {
|
|
"num": c[0], "name": c[1], "head_teacher_id": c[2],
|
|
"coach_teacher_id": c[3], "tutor_teacher_id": c[4],
|
|
"class_start_time": c[5], "class_end_time": c[6],
|
|
})
|
|
class_ids.append(result.lastrowid)
|
|
print(f" [班级] {c[1]}")
|
|
Session_.commit()
|
|
|
|
# ── 4. 学生 ──────────────────────────────────────────
|
|
first_names = ["志强", "丽华", "建国", "美玲", "文博", "晓燕", "浩然", "雨桐", "子轩", "思涵",
|
|
"俊杰", "雅琪", "宇航", "欣怡", "天翊", "诗涵", "一诺", "浩宇", "梓萱", "博文"]
|
|
home_places = ["北京", "上海", "广州", "深圳", "杭州", "成都", "武汉", "西安"]
|
|
colleges = ["清华大学", "北京大学", "复旦大学", "上海交通大学", "浙江大学", "南京大学", "武汉大学", "华中科技大学"]
|
|
specialties = ["计算机科学", "软件工程", "数据科学", "人工智能"]
|
|
student_count = 0
|
|
for i in range(20):
|
|
class_idx = i % 3
|
|
enrollment = date(2024, 9, 1) if class_idx != 2 else date(2025, 9, 1)
|
|
graduate = date(2028, 6, 30) if class_idx != 2 else date(2029, 6, 30)
|
|
phone = f"1390000{1000+i}"
|
|
r = Session_.execute(
|
|
text("SELECT id FROM students WHERE phone = :phone AND is_deleted = 0"),
|
|
{"phone": phone}
|
|
).fetchone()
|
|
if r:
|
|
continue
|
|
Session_.execute(text("""
|
|
INSERT INTO students (num, name, age, sex, home_Place, college, specialty,
|
|
enrollment_time, graduate_time, education,
|
|
class_id, advisor_id, phone, is_deleted)
|
|
VALUES (:num, :name, :age, :sex, :home_Place, :college, :specialty,
|
|
:enrollment_time, :graduate_time, '本科',
|
|
:class_id, :advisor_id, :phone, 0)
|
|
"""), {
|
|
"num": f"2024{1001+i}",
|
|
"name": first_names[i],
|
|
"age": 18 + (i % 4),
|
|
"sex": "男" if i % 2 == 0 else "女",
|
|
"home_Place": home_places[i % 8],
|
|
"college": colleges[i % 8],
|
|
"specialty": specialties[i % 4],
|
|
"enrollment_time": enrollment,
|
|
"graduate_time": graduate,
|
|
"class_id": class_ids[class_idx],
|
|
"advisor_id": teacher_ids[i % 6],
|
|
"phone": phone,
|
|
})
|
|
student_count += 1
|
|
print(f" [学生] {first_names[i]} (2024{1001+i}) - {'男' if i%2==0 else '女'} - 班级{class_idx+1}")
|
|
Session_.commit()
|
|
|
|
# ── 5. 成绩 ──────────────────────────────────────────
|
|
all_students = Session_.execute(
|
|
text("SELECT id, class_id FROM students WHERE is_deleted = 0")
|
|
).fetchall()
|
|
all_subjects = Session_.execute(text("SELECT id FROM subject")).fetchall()
|
|
score_count = 0
|
|
for idx, st in enumerate(all_students):
|
|
for subj_idx, sub in enumerate(all_subjects):
|
|
score_val = 60 + (idx * 7 + subj_idx * 13) % 41
|
|
# 让前3名学生部分课程不及格(用于统计接口测试)
|
|
if idx < 3 and subj_idx < 2:
|
|
score_val = 30 + (idx * 5) % 25
|
|
existing = Session_.execute(
|
|
text("SELECT id FROM scores WHERE sid = :sid AND cid = :cid AND num = 1 AND t_subject = :t_subject AND is_deleted = 0"),
|
|
{"sid": st[0], "cid": st[1], "t_subject": sub[0]}
|
|
).fetchone()
|
|
if not existing:
|
|
Session_.execute(text("""
|
|
INSERT INTO scores (sid, cid, num, score, t_subject, is_deleted)
|
|
VALUES (:sid, :cid, 1, :score, :t_subject, 0)
|
|
"""), {"sid": st[0], "cid": st[1], "score": score_val, "t_subject": sub[0]})
|
|
score_count += 1
|
|
print(f" [成绩] 为 {len(all_students)} 名学生插入了 {score_count} 条成绩记录")
|
|
Session_.commit()
|
|
|
|
# ── 6. 公司地址 ────────────────────────────────────────
|
|
addresses = ["北京市海淀区", "上海市浦东新区", "深圳市南山区", "杭州市西湖区", "广州市天河区"]
|
|
address_ids = []
|
|
for addr in addresses:
|
|
r = Session_.execute(
|
|
text("SELECT id FROM Address WHERE somewhere = :where"),
|
|
{"where": addr}
|
|
).fetchone()
|
|
if r:
|
|
address_ids.append(r[0])
|
|
else:
|
|
result = Session_.execute(
|
|
text("INSERT INTO Address (somewhere) VALUES (:where)"),
|
|
{"where": addr}
|
|
)
|
|
address_ids.append(result.lastrowid)
|
|
print(f" [地址] {addr}")
|
|
Session_.commit()
|
|
|
|
# ── 7. 公司 ────────────────────────────────────────────
|
|
companies = [
|
|
("字节跳动", address_ids[2]),
|
|
("阿里巴巴", address_ids[3]),
|
|
("腾讯", address_ids[2]),
|
|
("华为", address_ids[4]),
|
|
("百度", address_ids[0]),
|
|
]
|
|
company_ids = []
|
|
for comp in companies:
|
|
r = Session_.execute(
|
|
text("SELECT id FROM Company WHERE employment_company = :name"),
|
|
{"name": comp[0]}
|
|
).fetchone()
|
|
if r:
|
|
company_ids.append(r[0])
|
|
else:
|
|
result = Session_.execute(
|
|
text("INSERT INTO Company (employment_company, address_id) VALUES (:name, :addr_id)"),
|
|
{"name": comp[0], "addr_id": comp[1]}
|
|
)
|
|
company_ids.append(result.lastrowid)
|
|
print(f" [公司] {comp[0]}")
|
|
Session_.commit()
|
|
|
|
# ── 8. 就业信息 ────────────────────────────────────────
|
|
employment_data = [
|
|
(all_students[0][0], all_students[0][1], company_ids[0], 35000, date(2028, 3, 1), date(2028, 4, 15)),
|
|
(all_students[1][0], all_students[1][1], company_ids[1], 32000, date(2028, 3, 1), date(2028, 5, 1)),
|
|
(all_students[2][0], all_students[2][1], company_ids[2], 30000, date(2028, 3, 1), date(2028, 4, 20)),
|
|
(all_students[3][0], all_students[3][1], company_ids[3], 28000, date(2028, 3, 1), date(2028, 5, 10)),
|
|
(all_students[4][0], all_students[4][1], company_ids[4], 25000, date(2028, 3, 1), date(2028, 6, 1)),
|
|
(all_students[5][0], all_students[5][1], company_ids[0], 33000, date(2028, 3, 1), date(2028, 4, 25)),
|
|
(all_students[6][0], all_students[6][1], company_ids[1], 29000, date(2028, 3, 1), None),
|
|
(all_students[7][0], all_students[7][1], company_ids[2], 31000, date(2028, 3, 1), date(2028, 5, 5)),
|
|
]
|
|
emp_count = 0
|
|
for emp in employment_data:
|
|
r = Session_.execute(
|
|
text("SELECT id FROM Employments WHERE stuid = :stuid AND is_deleted = 0"),
|
|
{"stuid": emp[0]}
|
|
).fetchone()
|
|
if not r:
|
|
Session_.execute(text("""
|
|
INSERT INTO Employments (stuid, class_id, company_id, employment_salary,
|
|
employment_open_time, offer_recived_time, is_deleted)
|
|
VALUES (:stuid, :class_id, :company_id, :salary, :open_time, :offer_time, 0)
|
|
"""), {
|
|
"stuid": emp[0], "class_id": emp[1], "company_id": emp[2],
|
|
"salary": emp[3], "open_time": emp[4], "offer_time": emp[5],
|
|
})
|
|
emp_count += 1
|
|
print(f" [就业] 插入 {emp_count} 条就业记录")
|
|
Session_.commit()
|
|
|
|
# ── 完成 ───────────────────────────────────────────────
|
|
Session_.close()
|
|
print("\n=== 测试数据初始化完成!共插入:")
|
|
print(f" • 科目: {len(subjects)} 门")
|
|
print(f" • 教师: {len(teachers)} 位")
|
|
print(f" • 班级: {len(classes)} 个")
|
|
print(f" • 学生: {student_count} 名")
|
|
print(f" • 成绩: {score_count} 条")
|
|
print(f" • 公司: {len(companies)} 家")
|
|
print(f" • 就业: {emp_count} 条")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
seed()
|