| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import pytest
- from datetime import date, datetime, timedelta
- from types import SimpleNamespace
- from zoneinfo import ZoneInfo
- from room_cal.discount import DiscountData, DiscountType
- from room_cal.rules.discount.same_day import RULE_NAME, SURCHARGE_PERCENT, _same_day_dates, run
- _TZ = ZoneInfo('America/Los_Angeles')
- def _stub_config(monkeypatch, *, checkin_hour: int = 14):
- cfg = SimpleNamespace(global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)))
- monkeypatch.setattr('room_cal.rules.discount.same_day.get_config', lambda: cfg)
- def _stub_now(monkeypatch, dt: datetime):
- monkeypatch.setattr('room_cal.rules.discount.same_day.local_now', lambda: dt)
- def test_rule_registered():
- assert RULE_NAME == 'same_day'
- class TestSameDayDates:
- def test_morning_before_checkin_is_today_only(self):
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # 10 AM, checkin_hour=14 -> rollover=2
- assert _same_day_dates(now) == {date(2026, 7, 10)}
- def test_right_at_rollover_is_today_only(self):
- now = datetime(2026, 7, 10, 2, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10)}
- def test_afternoon_after_checkin_is_today_and_tomorrow(self):
- now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # 4 PM
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- def test_right_at_checkin_hour_is_today_and_tomorrow(self):
- now = datetime(2026, 7, 10, 14, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- def test_after_midnight_before_rollover_is_today_and_tomorrow(self):
- # 1 AM on the 10th: still the prior evening's "today + tomorrow" window
- now = datetime(2026, 7, 10, 1, 0, tzinfo=_TZ)
- assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
- class TestRun:
- @pytest.mark.asyncio
- async def test_empty_input_adds_surcharge_dates(self, monkeypatch):
- today = date(2026, 1, 1)
- now = datetime(2026, 1, 1, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- result = await run(today, today, {})
- assert result == {today: DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)}
- @pytest.mark.asyncio
- async def test_overrides_same_day_dates_morning(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- data = {
- today: DiscountData(type=DiscountType.PERCENT, value=40),
- today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
- today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
- }
- result = await run(today, today + timedelta(days=2), data)
- assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
- assert result[today + timedelta(days=1)].value == 30 # tomorrow is NOT same-day in the morning window
- assert result[today + timedelta(days=2)].value == 20
- @pytest.mark.asyncio
- async def test_overrides_same_day_dates_afternoon(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # after checkin_hour -> same_day = {today, +1}
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- data = {
- today: DiscountData(type=DiscountType.PERCENT, value=40),
- today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
- today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
- }
- result = await run(today, today + timedelta(days=2), data)
- assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
- assert result[today + timedelta(days=1)] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
- assert result[today + timedelta(days=2)].value == 20
- @pytest.mark.asyncio
- async def test_same_day_outside_range_not_added(self, monkeypatch):
- # start_date/end_date excludes today -> the surcharge must not be written outside the window
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- result = await run(today + timedelta(days=1), today + timedelta(days=5), {})
- assert today not in result
- @pytest.mark.asyncio
- async def test_untouched_dates_pass_through_unchanged(self, monkeypatch):
- today = date(2026, 7, 10)
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
- _stub_config(monkeypatch)
- _stub_now(monkeypatch, now)
- rec = DiscountData(type=DiscountType.FLAT, value=15)
- data = {today + timedelta(days=5): rec}
- result = await run(today, today + timedelta(days=5), data)
- assert result[today + timedelta(days=5)] is rec
|