| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import pytest
- from datetime import date, timedelta
- from room_cal.discount import Discount, DiscountData, DiscountType, _build_title, _build_description
- def test_build_title_flat():
- assert _build_title('flat', 20) == 'Discount: $20'
- def test_build_title_percent():
- assert _build_title('percent', 15) == 'Discount: 15%'
- def test_build_description_flat():
- assert _build_description('flat', 20, {'name': 'Test => ', 'id': 'abc'}) == 'Discount: $20\nSource: Test => abc'
- def test_build_description_percent():
- assert _build_description('percent', 15, {'name': 'Test => ', 'id': 'abc'}) == 'Discount: 15%\nSource: Test => abc'
- def test_build_description_empty_source():
- assert _build_description('flat', 20, {}) == 'Discount: $20'
- @pytest.mark.asyncio
- async def test_set_and_get_flat_round_trip():
- """Find a free day in the past, set a flat discount, read back, then delete."""
- discount = Discount()
- scan_start = date.today() - timedelta(days=50)
- scan_end = date.today() - timedelta(days=40)
- existing = await discount.list(scan_start, scan_end)
- already_taken = {e.start.date() for e in existing}
- target = None
- d = scan_start
- while d <= scan_end:
- if d not in already_taken:
- target = d
- break
- d += timedelta(days=1)
- assert target is not None, "No free date found in the past 40–50 days"
- source = {'domain': 'test', 'name': 'Test => ', 'id': 'test-flat'}
- try:
- await discount.set(target, DiscountData(type=DiscountType.FLAT, value=round(20.6), source=source))
- stored = await discount.get(target)
- assert stored is not None, f"No Discount event found after set on {target}"
- assert stored.type == DiscountType.FLAT
- assert stored.value == 21
- assert stored.source == source
- event = await discount._find(target)
- assert event is not None
- assert event.title == 'Discount: $21'
- assert event.description == 'Discount: $21\nSource: Test => test-flat'
- finally:
- await discount.delete_on(target)
- assert await discount.get(target) is None, f"Discount event still present after delete on {target}"
- @pytest.mark.asyncio
- async def test_set_and_get_percent_round_trip():
- """Find a free day in the past, set a percent discount, read back, then delete."""
- discount = Discount()
- scan_start = date.today() - timedelta(days=40)
- scan_end = date.today() - timedelta(days=30)
- existing = await discount.list(scan_start, scan_end)
- already_taken = {e.start.date() for e in existing}
- target = None
- d = scan_start
- while d <= scan_end:
- if d not in already_taken:
- target = d
- break
- d += timedelta(days=1)
- assert target is not None, "No free date found in the past 30–40 days"
- source = {'domain': 'test', 'name': 'Test => ', 'id': 'test-pct'}
- try:
- await discount.set(target, DiscountData(type=DiscountType.PERCENT, value=round(14.7), source=source))
- stored = await discount.get(target)
- assert stored is not None, f"No Discount event found after set on {target}"
- assert stored.type == DiscountType.PERCENT
- assert stored.value == 15
- assert stored.source == source
- event = await discount._find(target)
- assert event is not None
- assert event.title == 'Discount: 15%'
- assert event.description == 'Discount: 15%\nSource: Test => test-pct'
- finally:
- await discount.delete_on(target)
- assert await discount.get(target) is None, f"Discount event still present after delete on {target}"
|