import pytest from datetime import date, timedelta from room_cal.reservations import Reservation async def test_add_reservation_round_trip(): res = Reservation() checkin = date.today() - timedelta(days=30) checkout = checkin + timedelta(days=1) data = { 'guest_name': 'Bob Test', 'service': 'Test Service', 'reservation_id': 'A1-B2-C3', 'checkin_date': checkin, 'checkout_date': checkout, } try: await res.set(data) stored = await res.get('A1-B2-C3', checkin, checkout) assert stored is not None, "Reservation not found after set" assert stored.guest_name == 'Bob Test' assert stored.reservation_id == 'A1-B2-C3' finally: event = await res._find('A1-B2-C3', checkin, checkout) if event: await res.delete(event) async def _make_reservation(res: Reservation, reservation_id: str, checkin: date, checkout: date): await res.set({ 'guest_name': 'Patch Test', 'service': 'Test Service', 'reservation_id': reservation_id, 'checkin_date': checkin, 'checkout_date': checkout, }) return await res._find(reservation_id, checkin, checkout) async def test_patch_generic_fields(): res = Reservation() checkin = date.today() - timedelta(days=31) checkout = checkin + timedelta(days=1) reservation_id = 'PATCH-FIELDS-1' try: event = await _make_reservation(res, reservation_id, checkin, checkout) result = await res.patch(event.uid, checkin, fields={'door_code': '4242', 'note': 'left a spare key'}) assert result['door_code'] == '4242' assert result['note'] == 'left a spare key' assert result['reservation_id'] == reservation_id # untouched identity field survives stored = await res._find(reservation_id, checkin, checkout) from room_cal._utils import safe_json_loads xd = safe_json_loads(stored.x_data) assert xd['door_code'] == '4242' assert xd['note'] == 'left a spare key' finally: event = await res._find(reservation_id, checkin, checkout) if event: await res.delete(event) async def test_patch_rejects_protected_fields(): res = Reservation() checkin = date.today() - timedelta(days=32) checkout = checkin + timedelta(days=1) reservation_id = 'PATCH-PROTECTED-1' try: event = await _make_reservation(res, reservation_id, checkin, checkout) with pytest.raises(ValueError): await res.patch(event.uid, checkin, fields={'reservation_id': 'HIJACKED'}) finally: event = await res._find(reservation_id, checkin, checkout) if event: await res.delete(event) async def test_patch_checkin_checkout_time(): res = Reservation() checkin = date.today() - timedelta(days=33) checkout = checkin + timedelta(days=1) reservation_id = 'PATCH-TIME-1' try: event = await _make_reservation(res, reservation_id, checkin, checkout) result = await res.patch(event.uid, checkin, checkin_time='17:00', checkout_time='09:30') assert result['checkin_time'] == '17:00' assert result['checkout_time'] == '09:30' assert result['checkin_date'] == checkin.isoformat() # date untouched assert result['checkout_date'] == checkout.isoformat() stored = await res._find(reservation_id, checkin, checkout) assert stored.start.date() == checkin assert stored.start.hour == 17 and stored.start.minute == 0 assert stored.end.date() == checkout assert stored.end.hour == 9 and stored.end.minute == 30 finally: event = await res._find(reservation_id, checkin, checkout) if event: await res.delete(event)