Files
hunjing/backend/tests/modules/time_sense/test_lunar.py
T
2026-08-21 16:00:13 +08:00

102 lines
2.5 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.
"""农历/节气/节日引擎测试。"""
from datetime import date
from app.modules.time_sense.lunar import (
Shichen,
get_day_period,
get_festivals,
get_shichen,
get_solar_term,
solar_to_lunar,
)
def test_solar_to_lunar_spring_festival_2026():
# 2026-02-17 是农历丙午年正月初一(春节)
result = solar_to_lunar(date(2026, 2, 17))
assert result.year_ganzhi == "丙午"
assert result.month == 1
assert result.day == 1
assert result.is_leap_month is False
def test_solar_to_lunar_leap_month_recognized():
# 农历闰月标记:取一个已知闰月日期(如 2025 农历闰六月初一 ≈ 2025-07-25)
result = solar_to_lunar(date(2025, 7, 25))
assert result.month == 6
assert result.is_leap_month is True
def test_get_shichen_zi():
# 子时:23 点与 0 点都属子
assert get_shichen(23).name == "子"
assert get_shichen(0).name == "子"
def test_get_shichen_wu():
assert get_shichen(12).name == "午"
def test_get_day_period_deep_night():
assert get_day_period(2) == "凌晨"
assert get_day_period(23) == "深夜"
def test_get_solar_term_returns_current_or_nearest():
# 立春通常在 2/3-2/5;2026 年立春为 2/4
info = get_solar_term(date(2026, 2, 4))
assert info.current_name == "立春"
def test_get_solar_term_cross_year_next_in_chinese():
# 2026-12-25 跨年带入项,下一节气小寒在 getJieQiTable() 中以 XIAO_HAN 出现
info = get_solar_term(date(2026, 12, 25))
assert info.next_name == "小寒"
assert info.current_name is None
def test_get_solar_term_non_term_day_current_none():
_ALL_JIEQI_CN = {
"立春",
"雨水",
"惊蛰",
"春分",
"清明",
"谷雨",
"立夏",
"小满",
"芒种",
"夏至",
"小暑",
"大暑",
"立秋",
"处暑",
"白露",
"秋分",
"寒露",
"霜降",
"立冬",
"小雪",
"大雪",
"冬至",
"小寒",
"大寒",
}
info = get_solar_term(date(2026, 7, 15))
assert info.current_name is None
assert info.next_name in _ALL_JIEQI_CN
assert info.days_to_next > 0
def test_get_festivals_spring_festival():
festivals = get_festivals(date(2026, 2, 17))
names = [f.name for f in festivals]
assert "春节" in names
def test_shichen_model_has_range():
s = Shichen(name="子", start_hour=23, end_hour=1)
assert s.name == "子"
assert s.start_hour == 23