from datetime import datetime, timedelta from decimal import Decimal from gateway.model import MachineState def _machine() -> MachineState: return MachineState(1, "1#空压机", 0) def test_instant_ratio(): m = _machine() t = datetime(2026, 1, 1, 10, 0, 0) m.apply(t, instant_flow=Decimal("10"), total_flow=Decimal("1000")) m.apply(t, instant_power=Decimal("50"), total_energy=Decimal("5000")) assert m.ratios["instant"] == Decimal("5") # 周期基准刚建立,小时/天/月比值应为 0 assert m.ratios["hour"] == 0 assert m.ratios["day"] == 0 assert m.ratios["month"] == 0 def test_instant_ratio_zero_flow_is_safe(): m = _machine() t = datetime(2026, 1, 1, 10, 0, 0) m.apply(t, instant_flow=Decimal("0"), instant_power=Decimal("50")) assert m.ratios["instant"] == 0 def test_hour_ratio_and_hour_rollover(): m = _machine() t0 = datetime(2026, 1, 1, 10, 0, 0) m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000")) m.apply(t0 + timedelta(minutes=30), total_flow=Decimal("1100"), total_energy=Decimal("5500")) assert m.ratios["hour"] == Decimal("5") assert m.ratios["day"] == Decimal("5") # 11:00:01 整点清零:之前累计值成为新基准 t1 = datetime(2026, 1, 1, 11, 0, 1) m.apply(t1, total_flow=Decimal("1120"), total_energy=Decimal("5600")) assert m.ratios["hour"] == 0 # 天累计不受整点影响:600/120 = 5 assert m.ratios["day"] == Decimal("5") # 本小时内有新产出后才开始累计 m.apply(t1 + timedelta(minutes=10), total_flow=Decimal("1170"), total_energy=Decimal("5850")) assert m.ratios["hour"] == Decimal("5") def test_day_and_month_rollover(): m = _machine() start = datetime(2026, 1, 31, 23, 50, 0) m.apply(start, total_flow=Decimal("1000"), total_energy=Decimal("3000")) m.apply(start + timedelta(minutes=5), total_flow=Decimal("1100"), total_energy=Decimal("3300")) assert m.ratios["day"] == Decimal("3") assert m.ratios["month"] == Decimal("3") # 跨零点、跨月 m.apply(datetime(2026, 2, 1, 0, 0, 5), total_flow=Decimal("1110"), total_energy=Decimal("3330")) assert m.ratios["day"] == 0 assert m.ratios["month"] == 0 assert m.ratios["hour"] == 0 m.apply(datetime(2026, 2, 1, 0, 0, 35), total_flow=Decimal("1130"), total_energy=Decimal("3390")) assert m.ratios["day"] == Decimal("3") assert m.ratios["month"] == Decimal("3") def test_zero_consumption_in_period_is_zero(): m = _machine() t = datetime(2026, 1, 1, 10, 0, 0) m.apply(t, total_flow=Decimal("1000"), total_energy=Decimal("5000")) m.apply(t + timedelta(minutes=5), total_flow=Decimal("1000"), total_energy=Decimal("5000")) assert m.ratios["hour"] == 0 def test_restore_same_period_keeps_accumulation(): m = _machine() t0 = datetime(2026, 1, 1, 10, 0, 0) m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000")) m.apply(t0 + timedelta(minutes=10), total_flow=Decimal("1100"), total_energy=Decimal("5500")) payload = m.to_payload() restored = MachineState(1, "1#空压机", 0) restored.restore(payload, t0 + timedelta(minutes=20)) assert restored.ratios["day"] == Decimal("5") # 恢复后再累加,仍以原始基准计算:700/140 = 5 restored.apply(t0 + timedelta(minutes=30), total_flow=Decimal("1140"), total_energy=Decimal("5700")) assert restored.ratios["day"] == Decimal("5") def test_restore_after_period_change_uses_last_totals(): m = _machine() t0 = datetime(2026, 1, 1, 10, 0, 0) m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000")) m.apply(t0 + timedelta(minutes=10), total_flow=Decimal("1100"), total_energy=Decimal("5500")) payload = m.to_payload() restored = MachineState(1, "1#空压机", 0) restored.restore(payload, datetime(2026, 1, 2, 8, 0, 0)) assert restored.ratios["day"] == 0 # 仍在同一个月内,月累计保持连续 assert restored.ratios["month"] == Decimal("5") # 恢复后的新数据以“最后一次累计值”为起点 restored.apply(datetime(2026, 1, 2, 8, 1, 0), total_flow=Decimal("1110"), total_energy=Decimal("5530")) assert restored.ratios["day"] == Decimal("3") def test_restore_payload_roundtrip_of_ratios(): m = _machine() t0 = datetime(2026, 1, 1, 10, 0, 0) m.apply(t0, instant_flow=Decimal("10"), total_flow=Decimal("1000")) m.apply(t0, instant_power=Decimal("25"), total_energy=Decimal("5000")) m.apply(t0 + timedelta(minutes=1), total_flow=Decimal("1010"), total_energy=Decimal("5050")) payload = m.to_payload() restored = MachineState(1, "1#空压机", 0) restored.restore(payload, t0 + timedelta(minutes=2)) for key in ("instant", "hour", "day", "month"): assert restored.ratios[key] == m.ratios[key]