Files
kaoyan/tests/test_data.py
T

125 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pathlib import Path
from studio.domain import normalize_catalog, parse_quota, select_score_lines
from studio.database import Database
def raw_catalog():
return {
"dwdm": "10001", "dwmc": "北京大学", "szss": "北京",
"zydm": "081200", "zymc": "计算机科学与技术", "xwlxmc": "学术学位",
"yxsdm": "001", "yxsmc": "计算机学院", "yjfxdm": "01", "yjfxmc": "人工智能",
"xxfs": "1", "ksfsmc": "全国统考", "nzsrsstr": "专业:6(不含推免)",
"kskmz": [
{"km1Vo": {"kskmmc": "101思想政治理论"}, "km2Vo": {"kskmmc": "201英语(一)"}, "km3Vo": {"kskmmc": "301数学(一)"}, "km4Vo": {"kskmmc": "408计算机学科专业基础"}},
{"km1Vo": {"kskmmc": "101思想政治理论"}, "km2Vo": {"kskmmc": "203日语"}, "km3Vo": {"kskmmc": "301数学(一)"}, "km4Vo": {"kskmmc": "408计算机学科专业基础"}},
],
}
def test_distinct_exam_combinations_are_preserved():
records = normalize_catalog(raw_catalog(), 2027)
assert len(records) == 2
assert records[0]["subjects"][1] == "201英语(一)"
assert records[1]["subjects"][1] == "203日语"
assert records[0]["record_key"] != records[1]["record_key"]
def test_ambiguous_quota_is_not_converted_to_major_total():
assert parse_quota("专业:6(不含推免)") == (6, "major")
assert parse_quota("学院:100(含推免80)") == (None, "college")
assert parse_quota("研究方向:6") == (None, "direction")
assert parse_quota("拟招人数待定") == (None, "unknown")
assert parse_quota("专业:0(不含推免)") == (0, "major")
def test_history_is_preserved_across_years_and_reruns(tmp_path: Path):
db = Database(tmp_path / "studio.db")
db.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
db.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
db.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
assert db.query_catalog({})["total"] == 4
result = db.query_catalog({"year": 2027, "subject": "203"})
assert result["total"] == 1
rid = result["items"][0]["id"]
db.favorite(rid, True)
assert db.query_catalog({"favorites": True})["total"] == 1
def test_score_line_match_does_not_promote_category_to_major():
rows = [{"code": "08", "total": "300"}, {"code": "0812", "total": "310"}, {"code": "081200", "total": "320"}]
scores = select_score_lines(rows, "081200", "10001", "北京大学", 2026)
assert len(scores) == 1
assert scores[0]["total"] == 320
assert scores[0]["scope"] == "major"
fallback = select_score_lines(rows[:1], "081200", "10001", "北京大学", 2025)[0]
assert fallback["scope"] == "category"
assert fallback["raw_code"] == "08"
def test_same_year_different_college_lines_are_not_attached_arbitrarily(tmp_path: Path):
db = Database(tmp_path / "studio.db")
db.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
rows = [{"code": "081200", "total": "350", "depart_id": "999", "depart_name": "其他学院"}]
db.upsert_scores(select_score_lines(rows, "081200", "10001", "北京大学", 2026))
assert db.query_catalog({"year": 2026})["items"][0]["score_total"] is None
def test_special_plan_score_does_not_overwrite_regular_admission_line(tmp_path):
db = Database(tmp_path / "studio.db")
db.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
rows = [{"code": "081200", "total": "320", "depart_id": "001", "note": ""}, {"code": "081200", "total": "260", "depart_id": "001", "note": "少干专项计划"}]
db.upsert_scores(select_score_lines(rows, "081200", "10001", "北京大学", 2026))
assert db.overview()["scores"] == 2
assert db.query_catalog({})["items"][0]["score_total"] == 320
def test_special_plan_full_names_are_not_used_as_regular_reference_line(tmp_path):
db = Database(tmp_path / 'studio.db')
db.upsert_catalog(normalize_catalog(raw_catalog(), 2026))
rows = [{'code': '081200', 'total': 320, 'depart_id': '001', 'note': ''}, {'code': '081200', 'total': 260, 'depart_id': '001', 'note': '少数民族骨干计划'}, {'code': '081200', 'total': 250, 'depart_id': '001', 'note': '退役大学生士兵计划'}]
db.upsert_scores(select_score_lines(rows, '081200', '10001', '北京大学', 2026))
assert db.query_catalog({})['items'][0]['score_total'] == 320
def test_spreadsheet_roundtrip_preserves_record_identity(tmp_path):
from studio.imports import export_sheet, import_file
db = Database(tmp_path / 'studio.db')
records = normalize_catalog(raw_catalog(), 2027)
db.upsert_catalog(records)
for kind in ('csv', 'xlsx'):
path = tmp_path / ('data.' + kind)
path.write_bytes(export_sheet(records, kind))
imported, _ = import_file(path)
db.upsert_catalog(imported)
assert db.query_catalog({})['total'] == 2
def test_known_historical_school_levels_are_available_in_filters(tmp_path):
db = Database(tmp_path / 'studio.db')
db.upsert_catalog(normalize_catalog(raw_catalog(), 2027))
assert db.query_catalog({'level': '985'})['total'] == 2
assert db.query_catalog({'level': '211'})['total'] == 2
def test_imported_college_quota_cannot_be_promoted_by_numeric_column():
from studio.imports import row_record
row = row_record({'school_name':'Test','major_code':'081200','enrollment_text':'学院:100','enrollment_count':100},2026)
assert row['quota'] is None
assert row['quota_scope'] == 'college'
def test_job_school_keyword_does_not_match_department_names(tmp_path):
db = Database(tmp_path / 'studio.db')
item = raw_catalog()
item['yxsmc'] = '信息学院'
db.upsert_catalog(normalize_catalog(item, 2027))
assert db.query_catalog({'school_keyword': '信息'})['total'] == 0
def test_yam_reference_books_are_arrays_for_the_detail_view():
from studio.imports import row_record
row = row_record({'school_name':'Test','major_code':'081200','reference_books':'[]'},2026)
assert row['reference_books'] == []