|
@@ -1,23 +1,17 @@
|
|
|
import pytest
|
|
import pytest
|
|
|
-from datetime import date, datetime, timedelta
|
|
|
|
|
|
|
+from datetime import date, timedelta
|
|
|
from types import SimpleNamespace
|
|
from types import SimpleNamespace
|
|
|
-from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
|
|
-from room_cal.rules.discount.short_notice import RULE_NAME, _same_day_dates, run
|
|
|
|
|
|
|
+from room_cal.rules.discount.short_notice import RULE_NAME, run
|
|
|
|
|
|
|
|
-_TZ = ZoneInfo('America/Los_Angeles')
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-def _stub_config(monkeypatch, *, max_days: int = 14, ramp_days: int = 7, checkin_hour: int = 14):
|
|
|
|
|
- cfg = SimpleNamespace(
|
|
|
|
|
- rules=SimpleNamespace(short_notice=SimpleNamespace(max_days=max_days, ramp_days=ramp_days)),
|
|
|
|
|
- global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)),
|
|
|
|
|
- )
|
|
|
|
|
|
|
+def _stub_config(monkeypatch, *, max_days: int = 14, ramp_days: int = 7):
|
|
|
|
|
+ cfg = SimpleNamespace(rules=SimpleNamespace(short_notice=SimpleNamespace(max_days=max_days, ramp_days=ramp_days)))
|
|
|
monkeypatch.setattr('room_cal.rules.discount.short_notice.get_config', lambda: cfg)
|
|
monkeypatch.setattr('room_cal.rules.discount.short_notice.get_config', lambda: cfg)
|
|
|
|
|
|
|
|
|
|
|
|
|
-def _stub_now(monkeypatch, dt: datetime):
|
|
|
|
|
- monkeypatch.setattr('room_cal.rules.discount.short_notice.local_now', lambda: dt)
|
|
|
|
|
|
|
+def _stub_today(monkeypatch, today: date):
|
|
|
|
|
+ monkeypatch.setattr('room_cal.rules.discount.short_notice.local_today', lambda: today)
|
|
|
|
|
|
|
|
|
|
|
|
|
def _stub_open(monkeypatch, open_set: set[date]):
|
|
def _stub_open(monkeypatch, open_set: set[date]):
|
|
@@ -30,85 +24,65 @@ def test_rule_registered():
|
|
|
assert RULE_NAME == 'short_notice'
|
|
assert RULE_NAME == 'short_notice'
|
|
|
|
|
|
|
|
|
|
|
|
|
-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)}
|
|
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_both_neighbors_open_gives_40_percent(monkeypatch):
|
|
|
|
|
+ today = date(2026, 7, 10)
|
|
|
|
|
+ _stub_config(monkeypatch, max_days=14, ramp_days=0)
|
|
|
|
|
+ _stub_today(monkeypatch, today)
|
|
|
|
|
+ d = today + timedelta(days=3)
|
|
|
|
|
+ _stub_open(monkeypatch, {d - timedelta(days=1), d, d + timedelta(days=1)})
|
|
|
|
|
|
|
|
- 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)}
|
|
|
|
|
|
|
+ result = await run(today, today + timedelta(days=5), {})
|
|
|
|
|
|
|
|
- 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)}
|
|
|
|
|
|
|
+ assert result[d].value == 40
|
|
|
|
|
|
|
|
- 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)}
|
|
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_only_day_before_open_gives_30_percent(monkeypatch):
|
|
|
|
|
+ today = date(2026, 7, 10)
|
|
|
|
|
+ _stub_config(monkeypatch, max_days=14, ramp_days=0)
|
|
|
|
|
+ _stub_today(monkeypatch, today)
|
|
|
|
|
+ d = today + timedelta(days=3)
|
|
|
|
|
+ _stub_open(monkeypatch, {d - timedelta(days=1), d})
|
|
|
|
|
|
|
|
|
|
+ result = await run(today, today + timedelta(days=5), {})
|
|
|
|
|
|
|
|
-class TestRun:
|
|
|
|
|
|
|
+ assert result[d].value == 30
|
|
|
|
|
|
|
|
- @pytest.mark.asyncio
|
|
|
|
|
- async def test_same_day_dates_never_discounted_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)
|
|
|
|
|
- # today, tomorrow, and the day after are all open with open neighbors on both sides
|
|
|
|
|
- open_set = {today + timedelta(days=i) for i in range(-1, 5)}
|
|
|
|
|
- _stub_open(monkeypatch, open_set)
|
|
|
|
|
|
|
|
|
|
- result = await run(today, today + timedelta(days=3), {})
|
|
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_only_day_after_open_gives_20_percent(monkeypatch):
|
|
|
|
|
+ today = date(2026, 7, 10)
|
|
|
|
|
+ _stub_config(monkeypatch, max_days=14, ramp_days=0)
|
|
|
|
|
+ _stub_today(monkeypatch, today)
|
|
|
|
|
+ d = today + timedelta(days=3)
|
|
|
|
|
+ _stub_open(monkeypatch, {d, d + timedelta(days=1)})
|
|
|
|
|
|
|
|
- assert today not in result
|
|
|
|
|
- assert today + timedelta(days=1) in result # tomorrow is NOT same-day in the morning window
|
|
|
|
|
|
|
+ result = await run(today, today + timedelta(days=5), {})
|
|
|
|
|
|
|
|
- @pytest.mark.asyncio
|
|
|
|
|
- async def test_same_day_dates_never_discounted_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)
|
|
|
|
|
- open_set = {today + timedelta(days=i) for i in range(-1, 5)}
|
|
|
|
|
- _stub_open(monkeypatch, open_set)
|
|
|
|
|
|
|
+ assert result[d].value == 20
|
|
|
|
|
|
|
|
- result = await run(today, today + timedelta(days=3), {})
|
|
|
|
|
|
|
|
|
|
- assert today not in result
|
|
|
|
|
- assert today + timedelta(days=1) not in result
|
|
|
|
|
- assert today + timedelta(days=2) in result
|
|
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_neither_neighbor_open_gives_no_discount(monkeypatch):
|
|
|
|
|
+ today = date(2026, 7, 10)
|
|
|
|
|
+ _stub_config(monkeypatch, max_days=14, ramp_days=0)
|
|
|
|
|
+ _stub_today(monkeypatch, today)
|
|
|
|
|
+ d = today + timedelta(days=3)
|
|
|
|
|
+ _stub_open(monkeypatch, {d})
|
|
|
|
|
|
|
|
- @pytest.mark.asyncio
|
|
|
|
|
- async def test_normal_tiers_unaffected_outside_same_day(self, monkeypatch):
|
|
|
|
|
- today = date(2026, 7, 10)
|
|
|
|
|
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
|
|
|
|
|
- _stub_config(monkeypatch, max_days=14, ramp_days=0)
|
|
|
|
|
- _stub_now(monkeypatch, now)
|
|
|
|
|
- d = today + timedelta(days=3)
|
|
|
|
|
- # d has both neighbors open -> 40%; well outside the same-day window
|
|
|
|
|
- open_set = {d - timedelta(days=1), d, d + timedelta(days=1)}
|
|
|
|
|
- _stub_open(monkeypatch, open_set)
|
|
|
|
|
|
|
+ result = await run(today, today + timedelta(days=5), {})
|
|
|
|
|
|
|
|
- result = await run(today, today + timedelta(days=5), {})
|
|
|
|
|
|
|
+ assert d not in result
|
|
|
|
|
|
|
|
- assert result[d].value == 40
|
|
|
|
|
|
|
|
|
|
- @pytest.mark.asyncio
|
|
|
|
|
- async def test_empty_window_returns_empty(self, monkeypatch):
|
|
|
|
|
- today = date(2026, 7, 10)
|
|
|
|
|
- now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
|
|
|
|
|
- _stub_config(monkeypatch, max_days=1, ramp_days=0)
|
|
|
|
|
- _stub_now(monkeypatch, now)
|
|
|
|
|
- _stub_open(monkeypatch, set())
|
|
|
|
|
|
|
+@pytest.mark.asyncio
|
|
|
|
|
+async def test_empty_window_returns_empty(monkeypatch):
|
|
|
|
|
+ today = date(2026, 7, 10)
|
|
|
|
|
+ _stub_config(monkeypatch, max_days=1, ramp_days=0)
|
|
|
|
|
+ _stub_today(monkeypatch, today)
|
|
|
|
|
+ _stub_open(monkeypatch, set())
|
|
|
|
|
|
|
|
- result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
|
|
|
|
|
|
|
+ result = await run(today + timedelta(days=5), today + timedelta(days=5), {})
|
|
|
|
|
|
|
|
- assert result == {}
|
|
|
|
|
|
|
+ assert result == {}
|