Files
zkxw/backend/tests/test_oa_leave_hours.py
T
2026-07-30 17:54:48 +08:00

97 lines
2.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.
"""oa_leave_hours unit tests"""
from app.services.oa_leave_hours import (
apply_leave_kit_hours,
calc_leave_hours,
validate_leave_kit_form_values,
)
def test_calc_same_day_sample():
assert calc_leave_hours("2026-07-30 09:00", "2026-07-30 15:00") == 5.0
def test_calc_morning_only():
assert calc_leave_hours("2026-07-30 08:30", "2026-07-30 12:00") == 3.5
def test_calc_lunch_gap():
assert calc_leave_hours("2026-07-30 11:00", "2026-07-30 14:00") == 2.0
def test_calc_invalid():
assert calc_leave_hours("2026-07-30 15:00", "2026-07-30 09:00") is None
assert calc_leave_hours("", "2026-07-30 09:00") is None
def test_apply_overwrites_when_not_manual():
schema = {
"rule": [
{
"type": "leaveKit",
"props": {
"morningStart": "08:30",
"morningEnd": "12:00",
"afternoonStart": "13:00",
"afternoonEnd": "17:30",
},
"children": [
{"type": "select", "field": "leaveType", "title": "请假类型"},
{"type": "datePicker", "field": "leaveStart", "title": "开始时间"},
{"type": "datePicker", "field": "leaveEnd", "title": "结束时间"},
{"type": "inputNumber", "field": "leaveDuration", "title": "时长(h)"},
],
}
]
}
data = {
"leaveType": "事假",
"leaveStart": "2026-07-30 09:00",
"leaveEnd": "2026-07-30 15:00",
"leaveDuration": 1,
}
out = apply_leave_kit_hours(schema, data)
assert out["leaveDuration"] == 5.0
def test_apply_respects_manual():
schema = {
"rule": [
{
"type": "leaveKit",
"props": {},
"children": [
{"type": "datePicker", "field": "leaveStart"},
{"type": "datePicker", "field": "leaveEnd"},
{"type": "inputNumber", "field": "leaveDuration"},
],
}
]
}
data = {
"leaveStart": "2026-07-30 09:00",
"leaveEnd": "2026-07-30 15:00",
"leaveDuration": 8,
"__manual_leaveDuration": True,
}
out = apply_leave_kit_hours(schema, data)
assert out["leaveDuration"] == 8.0
def test_validate_end_before_start():
schema = {
"rule": [
{
"type": "leaveKit",
"children": [
{"type": "datePicker", "field": "s"},
{"type": "datePicker", "field": "e"},
{"type": "inputNumber", "field": "d"},
],
}
]
}
errs = validate_leave_kit_form_values(
schema, {"s": "2026-07-30 15:00", "e": "2026-07-30 09:00", "d": 1}
)
assert any("结束" in e for e in errs)