Files
37duAutomation/tests/unit/test_xq0033_rejoin_gap.py
puddy 6c1161fd54 test(ci): 新增 pytest 单元测试与最小 CI 门禁
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 的纯逻辑部分。
2026-11-11 12:48:37 +08:00

170 lines
6.7 KiB
Python
Raw Permalink 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.
# -*- coding: utf-8 -*-
"""XQ0033 离职当月再入职「不操作段」与日期滚动口径单测。
口径来源(插件 docstring / 项目记忆):
- 台账工号(同工号再入职):[本月1日, 再入职日) 不导入、不删除;
- 历史工号(换工号再入职):整月不操作,且必须「工号+姓名」同时匹配;
- 所有月份窗口锚定积分日期 T-2 所在月;
- 跳天补漏只补 T-2 所在月、且早于 T-2 的日期。
"""
from datetime import date, timedelta
import pytest
@pytest.fixture(scope="module")
def mod(plugins):
return plugins.module("xq0033", "logic.rejoin_gap")
@pytest.fixture(scope="module")
def pkg(plugins):
return plugins.package("xq0033")
@pytest.fixture
def keys(mod):
cfg = mod.config
return {
"code": "hr_staff_base.base_code",
"name": "hr_staff_base.staff_name",
"leave": f"{cfg.REJOIN_FIELD_PREFIX}.leave_date",
"join": f"{cfg.REJOIN_FIELD_PREFIX}.join_date",
}
def _row(keys, code, name, leave, join):
return {keys["code"]: code, keys["name"]: name,
keys["leave"]: leave, keys["join"]: join}
def _emp(code, name):
"""「员工信息9列」元组(索引 0=工号、8=姓名),与报表行结构一致。"""
return (code, "", "", "", "", "", "", "", name)
# ---------------- RejoinGuard:本月再入职 ----------------
def test_this_month_backfills_noop_before_join(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-10-01", "2026-10-18")],
anchor=date(2026, 10, 20))
assert g.invalid_until("37037921", "何蕾") == 18
assert g.is_invalid("37037921", "何蕾", 1) is True
assert g.is_invalid("37037921", "何蕾", 17) is True
# 再入职日当天起恢复正常(属于新周期)
assert g.is_invalid("37037921", "何蕾", 18) is False
def test_join_next_month_marks_whole_month(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-10-30", "2026-11-18")],
anchor=date(2026, 10, 20))
assert g.invalid_until("37037921", "何蕾") == mod._WHOLE_MONTH
assert g.is_invalid("37037921", "何蕾", 17) is True
assert g.is_invalid("37037921", "何蕾", 31) is True
def test_join_before_this_month_has_no_gap(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-08-01", "2026-09-10")],
anchor=date(2026, 10, 20))
assert g.invalid_until("37037921", "何蕾") == 1
assert g.is_invalid("37037921", "何蕾", 1) is False
def test_anchor_month_follows_t_minus_2_not_run_day(mod, keys):
"""月初运行(T-2 落在上月)时,不操作段按 T-2 所在月计算。"""
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-09-30", "2026-10-18")],
anchor=date(2026, 10, 31))
assert g.invalid_until("37037921", "何蕾") == 18
# ---------------- RejoinGuard:历史工号(换工号再入职) ----------------
def test_old_code_requires_same_name(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-09-30", "2026-10-18")],
old_codes_map={"37037921": ["37031111"]},
anchor=date(2026, 10, 20))
assert g.invalid_until("37031111", "何蕾") == mod._WHOLE_MONTH
# 工号可能被复用:姓名不一致时不跳过,避免误伤他人积分
assert g.invalid_until("37031111", "张三") is None
assert g.is_invalid("37031111", "张三", 1) is False
def test_missing_fields_are_ignored(mod, keys):
g = mod.RejoinGuard([
_row(keys, "", "何蕾", "2026-10-01", "2026-10-18"), # 无工号
_row(keys, "37037921", "何蕾", "", "2026-10-18"), # 无离职日
_row(keys, "37037922", "王五", "2026-10-01", "bad-date"), # 再入职日异常
], anchor=date(2026, 10, 20))
assert bool(g) is False
assert g.describe() == []
# ---------------- RejoinGuard:allow / describe ----------------
def test_allow_filters_import_and_delete_rows(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-10-01", "2026-10-18")],
anchor=date(2026, 10, 20))
rows = [
(_emp("37037921", "何蕾"), date(2026, 10, 3)), # 不操作段:剔除
(_emp("37037921", "何蕾"), date(2026, 10, 18)), # 新周期:保留
(_emp("37037922", "王五"), date(2026, 10, 3)), # 不在名单:保留
]
kept = g.allow(rows)
assert [r[1] for r in kept] == [date(2026, 10, 18), date(2026, 10, 3)]
def test_allow_returns_input_when_no_rejoin(mod, keys):
g = mod.RejoinGuard([], anchor=date(2026, 10, 20))
rows = [(_emp("37037921", "何蕾"), date(2026, 10, 3))]
assert g.allow(rows) is rows
def test_describe_text(mod, keys):
g = mod.RejoinGuard([_row(keys, "37037921", "何蕾", "2026-10-01", "2026-10-18")],
old_codes_map={"37037921": ["37031111"]},
anchor=date(2026, 10, 20))
text = "\n".join(g.describe())
assert "何蕾(37037921) 再入职 10-18 → 1~17 日不导入/不删除" in text
assert "37031111" in text and "整月" in text
# ---------------- 日期滚动:跳天补漏 ----------------
def test_gap_days_none_or_covered(pkg):
t2 = date(2026, 10, 20)
assert pkg._gap_days(t2, None) == []
assert pkg._gap_days(t2, t2) == []
assert pkg._gap_days(t2, date(2026, 10, 19)) == []
def test_gap_days_fills_skipped_days(pkg):
assert pkg._gap_days(date(2026, 10, 20), date(2026, 10, 10)) == [
date(2026, 10, 11) + timedelta(days=i) for i in range(9)
]
def test_gap_days_clamped_to_t2_month(pkg):
"""跨月时不补上月日期:月初 T-2 属上月,窗口取 T-2 所在月。"""
assert pkg._gap_days(date(2026, 10, 5), date(2026, 9, 28)) == [
date(2026, 10, 1), date(2026, 10, 2), date(2026, 10, 3), date(2026, 10, 4)
]
# T-2 已进入新月份、上轮在上月末 → 无同月可补日期
assert pkg._gap_days(date(2026, 11, 1), date(2026, 10, 30)) == []
# ---------------- 日期滚动:跨月首轮识别 ----------------
def test_is_new_month_prefers_file_month(pkg):
assert pkg._is_new_month((2026, 10), None, date(2026, 11, 5)) is True
assert pkg._is_new_month((2026, 11), date(2026, 10, 30), date(2026, 11, 5)) is False
def test_is_new_month_falls_back_to_covered_until(pkg):
assert pkg._is_new_month(None, date(2026, 10, 30), date(2026, 11, 5)) is True
assert pkg._is_new_month(None, date(2026, 11, 1), date(2026, 11, 5)) is False
def test_is_new_month_unknown_keeps_rolling(pkg):
"""两处都拿不到时按「未跨月」处理,不做无依据的清空。"""
assert pkg._is_new_month(None, None, date(2026, 11, 5)) is False