Files
shanxia2.0/backend/tests/test_pl_vehicle_fuel_month_km.py
T
2026-10-15 08:50:25 +08:00

66 lines
2.4 KiB
Python

"""加油汇总:月份偏移、公里与百公里油耗算法"""
from datetime import date
from decimal import Decimal
from app.services.pl_vehicle_fuel_service import (
month_run_km,
quarter_bounds,
quarter_label,
rate_l100,
ym_shift,
)
def test_ym_shift_across_year():
assert ym_shift("2026-01", -1) == "2025-12"
assert ym_shift("2025-12", 1) == "2026-01"
def test_quarter_bounds_and_label():
assert quarter_bounds(date(2026, 10, 13)) == (date(2026, 10, 1), date(2027, 1, 1))
assert quarter_label(date(2026, 10, 13)) == "2026-Q4"
assert quarter_bounds(date(2026, 3, 1)) == (date(2026, 1, 1), date(2026, 4, 1))
def test_rate_l100():
assert rate_l100(Decimal("33"), Decimal("100")) == Decimal("33.00")
assert rate_l100(Decimal("66"), Decimal("200")) == Decimal("33.00")
assert rate_l100(Decimal("10"), Decimal("0")) is None
assert rate_l100(Decimal("10"), Decimal("-1")) is None
def test_month_run_km_prefers_prev_month_last():
# 当月末日里程 - 上月末日里程
cur = [(date(2026, 10, 1), Decimal("100")), (date(2026, 10, 8), Decimal("250"))]
prev = [(date(2026, 9, 20), Decimal("80")), (date(2026, 9, 28), Decimal("100"))]
assert month_run_km(cur, prev) == Decimal("150")
def test_month_run_km_falls_back_to_intra_month():
cur = [(date(2026, 10, 1), Decimal("100")), (date(2026, 10, 8), Decimal("250"))]
assert month_run_km(cur, []) == Decimal("150")
def test_month_run_km_zero_when_insufficient():
assert month_run_km([(date(2026, 10, 1), Decimal("100"))], []) == Decimal("0")
assert month_run_km([], []) == Decimal("0")
def test_month_run_km_ignores_none_km():
cur = [(date(2026, 10, 1), None), (date(2026, 10, 8), Decimal("250")), (date(2026, 10, 9), None)]
prev = [(date(2026, 9, 28), Decimal("200"))]
assert month_run_km(cur, prev) == Decimal("50")
def test_prev_month_rate_l100_from_prev_and_before():
"""上个月百公里油耗:上月升数 / 上月行驶公里 * 100(上月里程 − 上上月末里程)。"""
prev_rows = [
(date(2026, 9, 5), Decimal("1000")),
(date(2026, 9, 28), Decimal("1300")),
]
before_prev = [(date(2026, 8, 30), Decimal("1000"))]
prev_km = month_run_km(prev_rows, before_prev)
assert prev_km == Decimal("300")
# 上月加油合计 99 L → 99/300*100 = 33.00
assert rate_l100(Decimal("99"), prev_km) == Decimal("33.00")