tools/ci_check.py 统一本地与流水线检查:全量 Python 语法编译(py_compile 不落 __pycache__)+ tests/unit 纯逻辑单测,可选 --frontend 追加壳前端构建;.gitlab-ci.yml 在 Windows runner 上复用同一脚本;requirements-dev.txt / pytest.ini 固化测试依赖与用例路径。单测覆盖 XQ0033/0034/0035/0037/0045/0048 的纯逻辑部分。
189 lines
7.4 KiB
Python
189 lines
7.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""XQ0037 合同信息计算规则单测(不动浏览器/HR 的真实纯逻辑)。
|
||
|
||
覆盖 _parse_idcard / _retire_date / _defer_months / _month_span / _labor_template_id
|
||
以及 calc_plan 的期限、退休封顶、签署单位、异常分支。
|
||
"""
|
||
from datetime import date
|
||
|
||
import pytest
|
||
|
||
|
||
@pytest.fixture(scope="module")
|
||
def mod(plugins):
|
||
return plugins.module("xq0037", "data.step3_transfer_todo")
|
||
|
||
|
||
# ---------------- 身份证解析 ----------------
|
||
|
||
@pytest.mark.parametrize("idcard, birth, sex", [
|
||
("110101199001011234", date(1990, 1, 1), "1"), # 第17位 3=奇 -> 男
|
||
("110101199001011244", date(1990, 1, 1), "2"), # 第17位 4=偶 -> 女
|
||
("11010119900101123X", date(1990, 1, 1), "1"), # 末位 X 合法
|
||
("110101900101123", date(1990, 1, 1), "1"), # 15 位,第15位 3=奇 -> 男
|
||
])
|
||
def test_parse_idcard_valid(mod, idcard, birth, sex):
|
||
assert mod._parse_idcard(idcard) == (birth, sex)
|
||
|
||
|
||
@pytest.mark.parametrize("idcard", ["", None, "abc", "11010119900101123",
|
||
"110101199002311234"]) # 末一种:2-31 非法日期
|
||
def test_parse_idcard_invalid(mod, idcard):
|
||
assert mod._parse_idcard(idcard) == (None, None)
|
||
|
||
|
||
# ---------------- 退休日 / 月份工具 ----------------
|
||
|
||
def test_retire_date_male_female(mod):
|
||
assert mod._retire_date("1", date(2000, 1, 1)) == date(2060, 1, 1)
|
||
assert mod._retire_date("2", date(2000, 1, 1)) == date(2050, 1, 1)
|
||
|
||
|
||
def test_retire_date_leap_day_falls_back_to_feb28(mod):
|
||
"""2-29 生日遇平年:退到 2-28,不能抛 ValueError。"""
|
||
assert mod._retire_date("2", date(2000, 2, 29)) == date(2050, 2, 28)
|
||
|
||
|
||
def test_defer_months_clamps_to_month_end(mod):
|
||
assert mod._defer_months(date(2026, 1, 31), 1) == date(2026, 2, 28)
|
||
assert mod._defer_months(date(2026, 11, 1), 12) == date(2027, 11, 1)
|
||
|
||
|
||
def test_month_span_ignores_day_remainder_and_minimum_one(mod):
|
||
assert mod._month_span(date(2026, 11, 1), date(2027, 6, 1)) == 7
|
||
assert mod._month_span(date(2026, 1, 31), date(2026, 2, 28)) == 1 # 零头不计,最小 1
|
||
|
||
|
||
def test_labor_template_by_sequence(mod):
|
||
assert mod._labor_template_id("中国技师") == mod.TPL_LABOR_TECH
|
||
assert mod._labor_template_id("服务序列") == mod.TPL_LABOR_SERV
|
||
assert mod._labor_template_id("职能专业序列") == mod.TPL_LABOR_FUNC
|
||
assert mod._labor_template_id("") == mod.TPL_LABOR_FUNC
|
||
|
||
|
||
# ---------------- calc_plan ----------------
|
||
|
||
@pytest.fixture
|
||
def env(monkeypatch, mod):
|
||
"""隔离所有网络查询:模板、单位 code、单位详情固定返回。"""
|
||
monkeypatch.setattr(mod, "_new_contract_template_ids", lambda session, base_id: "")
|
||
monkeypatch.setattr(mod, "_unit_set_detail", lambda session, si_area: ("执照名称", "印章编号"))
|
||
monkeypatch.setattr(mod, "_unit_set_id_by_name",
|
||
lambda session, name: "SID" if name else "")
|
||
return mod
|
||
|
||
|
||
def _roster(**kw):
|
||
base = {"现职位序列": "服务序列", "现单位": "石桥铺会馆",
|
||
"现四级部门": "石桥铺会馆", "现岗位": "服务员"}
|
||
base.update(kw)
|
||
return base
|
||
|
||
|
||
def _biz(**kw):
|
||
base = {"idcard": "110101199501011234", "staff_id": "S1",
|
||
"leaveoffice_time": "2026-10-31"}
|
||
base.update(kw)
|
||
return base
|
||
|
||
|
||
def test_service_sequence_signs_12_months(mod, env):
|
||
plan, err = mod.calc_plan(object(), _biz(), _roster(现职位序列="服务序列"))
|
||
assert err is None
|
||
assert plan["months"] == 12
|
||
assert plan["treaty_type"] == "1"
|
||
assert plan["start"] == "2026-11-01"
|
||
assert plan["end_time"] == "2027-11-01"
|
||
assert plan["end"] == "2027-10-31"
|
||
assert plan["si_area"] == "SID"
|
||
assert mod.TPL_LABOR_SERV in plan["templates"]
|
||
assert mod.TPL_INTEGRITY in plan["templates"]
|
||
assert mod.TPL_SOCIAL in plan["templates"]
|
||
|
||
|
||
def test_other_sequence_signs_36_months(mod, env):
|
||
plan, err = mod.calc_plan(object(), _biz(), _roster(现职位序列="职能专业序列"))
|
||
assert err is None
|
||
assert plan["months"] == 36
|
||
assert plan["end"] == "2029-10-31"
|
||
assert mod.TPL_LABOR_FUNC in plan["templates"]
|
||
|
||
|
||
def test_overage_signs_12_months_overage_agreement(mod, env):
|
||
"""已达法定退休年龄:不分序列统一 12 个月 + 超龄人员用工协议,且不算异常。"""
|
||
plan, err = mod.calc_plan(object(), _biz(idcard="110101193501011234"),
|
||
_roster(现职位序列="职能专业序列"))
|
||
assert err is None
|
||
assert plan["months"] == 12
|
||
assert plan["treaty_type"] == mod.TREATY_TYPE_OVERAGE
|
||
assert mod.TPL_OVERAGE_AGREEMENT in plan["templates"]
|
||
|
||
|
||
def test_retirement_cap_before_reaching_age(mod, env):
|
||
"""未达龄但按期限签会越过退休日 -> 终止日封顶到退休日、期限按实际月数登记。"""
|
||
# 1969-06-01 男:退休日 2029-06-01;36 个月会签到期 2029-10-31 -> 应封顶
|
||
plan, err = mod.calc_plan(object(), _biz(idcard="110101196906011234"),
|
||
_roster(现职位序列="职能专业序列"))
|
||
assert err is None
|
||
assert plan["end"] == "2029-06-01"
|
||
assert plan["end_time"] == "2029-06-02"
|
||
assert plan["months"] == 31
|
||
assert plan["treaty_type"] == "1" # 未达龄仍走劳动合同
|
||
|
||
|
||
def test_lakeside_business_unit_split_by_sequence(mod, env):
|
||
plan, err = mod.calc_plan(
|
||
object(), _biz(), _roster(现单位=mod.UNIT_GOLDEN_LAKESIDE, 现职位序列="职能专业序列"))
|
||
assert err is None
|
||
assert plan["unit_name"] == mod.UNIT_LAKESIDE_COMPANY
|
||
|
||
plan2, err2 = mod.calc_plan(
|
||
object(), _biz(), _roster(现单位=mod.UNIT_GOLDEN_LAKESIDE, 现职位序列="服务序列"))
|
||
assert err2 is None
|
||
assert plan2["unit_name"] == mod.UNIT_SHIQIAOPU
|
||
|
||
|
||
def test_houlang_unit_split_uses_position(mod, env, monkeypatch):
|
||
"""后浪按岗位细分:calc_plan 必须把「现岗位」透传给细分函数。"""
|
||
captured = {}
|
||
|
||
def fake_split(session, unit_name, position):
|
||
captured["unit_name"] = unit_name
|
||
captured["position"] = position
|
||
return f"{mod.UNIT_TRAINING_VENUE}(餐饮)"
|
||
|
||
monkeypatch.setattr(mod, "houlang_unit_name", fake_split)
|
||
plan, err = mod.calc_plan(
|
||
object(), _biz(),
|
||
_roster(现四级部门="后浪新天地LIFE+主题会馆", 现岗位="吧员"))
|
||
assert err is None
|
||
assert captured == {"unit_name": "后浪新天地LIFE+主题会馆", "position": "吧员"}
|
||
assert plan["unit_name"] == f"{mod.UNIT_TRAINING_VENUE}(餐饮)"
|
||
|
||
|
||
def test_houlang_unit_missing_abnormal(mod, env, monkeypatch):
|
||
monkeypatch.setattr(mod, "houlang_unit_name", lambda s, u, p: None)
|
||
plan, err = mod.calc_plan(
|
||
object(), _biz(), _roster(现四级部门="后浪新天地LIFE+主题会馆", 现岗位="吧员"))
|
||
assert plan is None
|
||
assert "单位字典未找到键值" in err
|
||
|
||
|
||
def test_missing_idcard_abnormal(mod, env):
|
||
plan, err = mod.calc_plan(object(), _biz(idcard=""), _roster())
|
||
assert plan is None
|
||
assert "身份证" in err
|
||
|
||
|
||
def test_bad_leave_date_abnormal(mod, env):
|
||
plan, err = mod.calc_plan(object(), _biz(leaveoffice_time="bad"), _roster())
|
||
assert plan is None
|
||
assert "实际离职日期异常" in err
|
||
|
||
|
||
def test_unit_code_missing_abnormal(mod, env, monkeypatch):
|
||
monkeypatch.setattr(mod, "_unit_set_id_by_name", lambda session, name: "")
|
||
plan, err = mod.calc_plan(object(), _biz(), _roster())
|
||
assert plan is None
|
||
assert "单位字典未找到键值" in err
|