"""月份校验单元测试。 守的口径:报表、月结、调整单入口都必须拦住 `2026-13`、`0000-01` 这类 非法月份。只校验长度会让 ValueError 一路传到 `calendar.monthrange`, 最终变成 500 而不是参数错误 400。 """ import pytest from pydantic import ValidationError from app.core.month import parse_month, validate_month from app.schemas.report import DepartmentSummaryQuery, PersonalDetailQuery from app.schemas.settlement import AdjustmentCreateIn, SettlementGenerateIn class Test月份校验: """合法月份通过,非法月份在入口即拒绝。""" @pytest.mark.parametrize("month", ["2026-01", "2026-12", "2000-06"]) def test_合法月份_通过(self, month: str): assert validate_month(month) == month year, mon = parse_month(month) assert (year, mon) == (int(month[:4]), int(month[5:7])) @pytest.mark.parametrize( "month", ["2026-13", "2026-00", "2026-1", "2026-1a", "0000-01", "abc", "2026/10"], ) def test_非法月份_抛ValueError(self, month: str): with pytest.raises(ValueError): validate_month(month) @pytest.mark.parametrize("month", ["2026-13", "2026-00", "0000-01"]) def test_月结与调整单_schema拒绝非法月份(self, month: str): with pytest.raises(ValidationError): SettlementGenerateIn(month=month) with pytest.raises(ValidationError): AdjustmentCreateIn( settlement_month=month, personnel_id=1, direction=1, amount="100.00", reason="测试", ) @pytest.mark.parametrize("month", ["2026-13", "2026-00", "0000-01"]) def test_报表查询_schema拒绝非法月份(self, month: str): with pytest.raises(ValidationError): DepartmentSummaryQuery(settlement_month=month) with pytest.raises(ValidationError): PersonalDetailQuery(settlement_month=month) def test_报表可选月份_空串规范为None(self): """axios 未剔除空串时不能误报格式错误。""" assert PersonalDetailQuery(settlement_month="").settlement_month is None