Files
shanxia2.0/backend/tests/test_pl_vehicle_fuel_month_km.py
T

62 lines
2.5 KiB
Python

"""加油汇总:月份偏移、公里与百公里油耗算法"""
from datetime import date
from decimal import Decimal
from app.services.pl_vehicle_fuel_service import (
period_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_period_uses_next_first_minus_current_first():
cur = [(date(2026, 10, 8), Decimal("250")), (date(2026, 10, 1), Decimal("100"))]
following = [(date(2026, 11, 8), Decimal("500")), (date(2026, 11, 2), Decimal("300"))]
assert period_run_km(cur, following) == (Decimal("200"), "finalized")
def test_period_without_next_reading_is_estimated():
cur = [(date(2026, 10, 1), Decimal("100")), (date(2026, 10, 8), Decimal("250"))]
assert period_run_km(cur, []) == (Decimal("150"), "estimated")
def test_period_insufficient_is_not_zero():
one = [(date(2026, 10, 1), Decimal("100"))]
assert period_run_km(one, []) == (None, "insufficient")
assert period_run_km([], one) == (None, "insufficient")
assert period_run_km([], []) == (None, "insufficient")
def test_period_ignores_missing_readings_and_accepts_real_zero():
cur = [(date(2026, 10, 1), None), (date(2026, 10, 8), Decimal("0"))]
following = [(date(2026, 11, 1), None), (date(2026, 11, 2), Decimal("300"))]
assert period_run_km(cur, following) == (Decimal("300"), "finalized")
assert period_run_km(cur, [(date(2026, 11, 2), Decimal("0"))]) == (Decimal("0"), "finalized")
def test_period_detects_odometer_rollback_including_internal_readings():
cur = [(date(2026, 10, 1), Decimal("100")), (date(2026, 10, 8), Decimal("90"))]
assert period_run_km(cur, []) == (None, "abnormal")
assert period_run_km(cur, [(date(2026, 11, 2), Decimal("300"))]) == (None, "abnormal")
assert period_run_km(cur[:1], [(date(2026, 11, 2), Decimal("80"))]) == (None, "abnormal")