test_same_day_rule.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, _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_returns_empty(self, monkeypatch):
  35. assert await run(date(2026, 1, 1), date(2026, 1, 1), {}) == {}
  36. @pytest.mark.asyncio
  37. async def test_strips_same_day_dates_morning(self, monkeypatch):
  38. today = date(2026, 7, 10)
  39. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ) # before checkin_hour -> same_day = {today}
  40. _stub_config(monkeypatch)
  41. _stub_now(monkeypatch, now)
  42. data = {
  43. today: DiscountData(type=DiscountType.PERCENT, value=40),
  44. today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
  45. today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
  46. }
  47. result = await run(today, today + timedelta(days=2), data)
  48. assert today not in result
  49. assert today + timedelta(days=1) in result # tomorrow is NOT same-day in the morning window
  50. assert today + timedelta(days=2) in result
  51. @pytest.mark.asyncio
  52. async def test_strips_same_day_dates_afternoon(self, monkeypatch):
  53. today = date(2026, 7, 10)
  54. now = datetime(2026, 7, 10, 16, 0, tzinfo=_TZ) # after checkin_hour -> same_day = {today, +1}
  55. _stub_config(monkeypatch)
  56. _stub_now(monkeypatch, now)
  57. data = {
  58. today: DiscountData(type=DiscountType.PERCENT, value=40),
  59. today + timedelta(days=1): DiscountData(type=DiscountType.PERCENT, value=30),
  60. today + timedelta(days=2): DiscountData(type=DiscountType.PERCENT, value=20),
  61. }
  62. result = await run(today, today + timedelta(days=2), data)
  63. assert today not in result
  64. assert today + timedelta(days=1) not in result
  65. assert today + timedelta(days=2) in result
  66. assert result[today + timedelta(days=2)].value == 20
  67. @pytest.mark.asyncio
  68. async def test_untouched_dates_pass_through_unchanged(self, monkeypatch):
  69. today = date(2026, 7, 10)
  70. now = datetime(2026, 7, 10, 10, 0, tzinfo=_TZ)
  71. _stub_config(monkeypatch)
  72. _stub_now(monkeypatch, now)
  73. rec = DiscountData(type=DiscountType.FLAT, value=15)
  74. data = {today + timedelta(days=5): rec}
  75. result = await run(today, today + timedelta(days=5), data)
  76. assert result[today + timedelta(days=5)] is rec