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