test_same_day_rule.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import pytest
  2. from datetime import date, datetime, timedelta
  3. from types import SimpleNamespace
  4. from zoneinfo import ZoneInfo
  5. from room_cal.discount import DiscountData, DiscountType
  6. from room_cal.rules.discount.same_day import RULE_NAME, SURCHARGE_PERCENT, _same_day_dates, run
  7. _TZ = ZoneInfo('America/Los_Angeles')
  8. def _stub_config(monkeypatch, *, checkin_hour: int = 14):
  9. cfg = SimpleNamespace(global_=SimpleNamespace(property=SimpleNamespace(checkin_hour=checkin_hour)))
  10. monkeypatch.setattr('room_cal.rules.discount.same_day.get_config', lambda: cfg)
  11. def _stub_now(monkeypatch, dt: datetime):
  12. monkeypatch.setattr('room_cal.rules.discount.same_day.local_now', lambda: dt)
  13. def test_rule_registered():
  14. assert RULE_NAME == 'same_day'
  15. class TestSameDayDates:
  16. def test_morning_before_checkin_is_today_only(self):
  17. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # 10 AM, checkin_hour=14 -> rollover=2
  18. assert _same_day_dates(now) == {date(2026, 7, 10)}
  19. def test_right_at_rollover_is_today_only(self):
  20. now = datetime(2026, 7, 10, 2, 0, tzinfo=_TZ)
  21. assert _same_day_dates(now) == {date(2026, 7, 10)}
  22. def test_afternoon_after_checkin_is_today_and_tomorrow(self):
  23. now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # 4 PM
  24. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  25. def test_right_at_checkin_hour_is_today_and_tomorrow(self):
  26. now = datetime(2026, 7, 10, 14, 0, tzinfo=_TZ)
  27. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  28. def test_after_midnight_before_rollover_is_today_and_tomorrow(self):
  29. # 1 AM on the 10th: still the prior evening's "today + tomorrow" window
  30. now = datetime(2026, 7, 10, 1, 0, tzinfo=_TZ)
  31. assert _same_day_dates(now) == {date(2026, 7, 10), date(2026, 7, 11)}
  32. class TestRun:
  33. @pytest.mark.asyncio
  34. async def test_empty_input_adds_surcharge_dates(self, monkeypatch):
  35. today = date(2026, 1, 1)
  36. now = datetime(2026, 1, 1, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
  37. _stub_config(monkeypatch)
  38. _stub_now(monkeypatch, now)
  39. result = await run(today, today, {})
  40. assert result == {today: DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)}
  41. @pytest.mark.asyncio
  42. async def test_overrides_same_day_dates_morning(self, monkeypatch):
  43. today = date(2026, 7, 10)
  44. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
  45. _stub_config(monkeypatch)
  46. _stub_now(monkeypatch, now)
  47. data = {
  48. today: DiscountData(type=DiscountType.PERCENT, value=40),
  49. today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
  50. today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
  51. }
  52. result = await run(today, today + timedelta(days=2), data)
  53. assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
  54. assert result[today + timedelta(days=1)].value == 30 # tomorrow is NOT same-day in the morning window
  55. assert result[today + timedelta(days=2)].value == 20
  56. @pytest.mark.asyncio
  57. async def test_overrides_same_day_dates_afternoon(self, monkeypatch):
  58. today = date(2026, 7, 10)
  59. now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # after checkin_hour -> same_day = {today, +1}
  60. _stub_config(monkeypatch)
  61. _stub_now(monkeypatch, now)
  62. data = {
  63. today: DiscountData(type=DiscountType.PERCENT, value=40),
  64. today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
  65. today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
  66. }
  67. result = await run(today, today + timedelta(days=2), data)
  68. assert result[today] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
  69. assert result[today + timedelta(days=1)] == DiscountData(type=DiscountType.PERCENT, value=SURCHARGE_PERCENT)
  70. assert result[today + timedelta(days=2)].value == 20
  71. @pytest.mark.asyncio
  72. async def test_same_day_outside_range_not_added(self, monkeypatch):
  73. # start_date/end_date excludes today -> the surcharge must not be written outside the window
  74. today = date(2026, 7, 10)
  75. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
  76. _stub_config(monkeypatch)
  77. _stub_now(monkeypatch, now)
  78. result = await run(today + timedelta(days=1), today + timedelta(days=5), {})
  79. assert today not in result
  80. @pytest.mark.asyncio
  81. async def test_untouched_dates_pass_through_unchanged(self, monkeypatch):
  82. today = date(2026, 7, 10)
  83. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
  84. _stub_config(monkeypatch)
  85. _stub_now(monkeypatch, now)
  86. rec = DiscountData(type=DiscountType.FLAT, value=15)
  87. data = {today + timedelta(days=5): rec}
  88. result = await run(today, today + timedelta(days=5), data)
  89. assert result[today + timedelta(days=5)] is rec