|
| 1 | +"""Test Ridwell calendar platform.""" |
| 2 | + |
| 3 | +from datetime import date |
| 4 | + |
| 5 | +from aioridwell.model import EventState, RidwellPickup, RidwellPickupEvent |
| 6 | +import pytest |
| 7 | + |
| 8 | +from homeassistant.components.calendar import CalendarEvent |
| 9 | +from homeassistant.components.ridwell.calendar import ( |
| 10 | + async_get_calendar_event_from_pickup_event, |
| 11 | +) |
| 12 | +from homeassistant.components.ridwell.const import ( |
| 13 | + CALENDAR_TITLE_NONE, |
| 14 | + CALENDAR_TITLE_ROTATING, |
| 15 | + CALENDAR_TITLE_STATUS, |
| 16 | + CONF_CALENDAR_TITLE, |
| 17 | +) |
| 18 | +from homeassistant.core import HomeAssistant |
| 19 | + |
| 20 | +START_DATE = date(2025, 10, 4) |
| 21 | +END_DATE = date(2025, 10, 5) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +@pytest.mark.parametrize( |
| 26 | + ( |
| 27 | + "pickup_name", |
| 28 | + "event_state", |
| 29 | + "summary_style", |
| 30 | + "expected_description", |
| 31 | + "expected_summary", |
| 32 | + ), |
| 33 | + [ |
| 34 | + # Valid events that should be converted. |
| 35 | + # Pickup name of "Cork" is used to produce PickupCategory.ROTATING, |
| 36 | + # and "Plastic Film" is used to generate a PickupCategory.STANDARD pickup. |
| 37 | + ( |
| 38 | + "Cork", |
| 39 | + EventState.SCHEDULED, |
| 40 | + CALENDAR_TITLE_ROTATING, # Display Rotating Category in summary. |
| 41 | + "Pickup types: Cork (quantity: 1)", |
| 42 | + "Ridwell Pickup (Cork)", |
| 43 | + ), |
| 44 | + ( |
| 45 | + "Cork", |
| 46 | + EventState.SCHEDULED, |
| 47 | + CALENDAR_TITLE_NONE, # Display no extra info in summary. |
| 48 | + "Pickup types: Cork (quantity: 1)", |
| 49 | + "Ridwell Pickup", |
| 50 | + ), |
| 51 | + ( |
| 52 | + "Cork", |
| 53 | + EventState.INITIALIZED, |
| 54 | + CALENDAR_TITLE_ROTATING, # Display Rotating Category in summary. |
| 55 | + "Pickup types: Cork (quantity: 1)", |
| 56 | + "Ridwell Pickup (Cork)", |
| 57 | + ), |
| 58 | + ( |
| 59 | + "Cork", |
| 60 | + EventState.SKIPPED, |
| 61 | + CALENDAR_TITLE_STATUS, # Display pickup state in summary. |
| 62 | + "Pickup types: Cork (quantity: 1)", |
| 63 | + "Ridwell Pickup (skipped)", |
| 64 | + ), |
| 65 | + ( |
| 66 | + "Cork", |
| 67 | + EventState.INITIALIZED, |
| 68 | + CALENDAR_TITLE_STATUS, # Display pickup state in summary. |
| 69 | + "Pickup types: Cork (quantity: 1)", |
| 70 | + "Ridwell Pickup (initialized)", |
| 71 | + ), |
| 72 | + ( |
| 73 | + "Cork", |
| 74 | + EventState.UNKNOWN, |
| 75 | + CALENDAR_TITLE_STATUS, # Display pickup state in summary. |
| 76 | + "Pickup types: Cork (quantity: 1)", |
| 77 | + "Ridwell Pickup (unknown)", |
| 78 | + ), |
| 79 | + ], |
| 80 | +) |
| 81 | +async def test_calendar_event_varied_states_and_types( |
| 82 | + hass: HomeAssistant, |
| 83 | + config_entry, |
| 84 | + pickup_name: str, |
| 85 | + event_state: EventState, |
| 86 | + expected_description: str, |
| 87 | + expected_summary: str, |
| 88 | + summary_style: str, |
| 89 | +) -> None: |
| 90 | + """Test CalendarEvent output based on pickup type and event state.""" |
| 91 | + |
| 92 | + # Set calendar config to default |
| 93 | + hass.config_entries.async_update_entry( |
| 94 | + config_entry, |
| 95 | + options={CONF_CALENDAR_TITLE: summary_style}, |
| 96 | + ) |
| 97 | + await hass.async_block_till_done() |
| 98 | + |
| 99 | + # Create test pickup |
| 100 | + pickup = RidwellPickup( |
| 101 | + name=pickup_name, |
| 102 | + offer_id=f"offer_{pickup_name.lower()}", |
| 103 | + quantity=1, |
| 104 | + product_id=f"product_{pickup_name.lower()}", |
| 105 | + priority=1, |
| 106 | + ) |
| 107 | + |
| 108 | + # Create test pickup event with the given state |
| 109 | + pickup_event = RidwellPickupEvent( |
| 110 | + _async_request=None, |
| 111 | + event_id=f"event_{pickup_name.lower()}_{event_state.name.lower()}", |
| 112 | + pickup_date=START_DATE, |
| 113 | + pickups=[pickup], |
| 114 | + state=event_state, |
| 115 | + ) |
| 116 | + |
| 117 | + # Call the function under test |
| 118 | + event = async_get_calendar_event_from_pickup_event(pickup_event, config_entry) |
| 119 | + |
| 120 | + assert isinstance(event, CalendarEvent) |
| 121 | + assert event.summary == expected_summary |
| 122 | + assert event.description == expected_description |
| 123 | + assert event.start == START_DATE |
| 124 | + assert event.end == END_DATE |
| 125 | + |
| 126 | + |
| 127 | +async def test_calendar_event_with_no_pickups( |
| 128 | + hass: HomeAssistant, |
| 129 | + config_entry, |
| 130 | +) -> None: |
| 131 | + """Test empty pickups.""" |
| 132 | + pickup_event = RidwellPickupEvent( |
| 133 | + _async_request=None, |
| 134 | + event_id="event_empty", |
| 135 | + pickup_date=START_DATE, |
| 136 | + pickups=[], |
| 137 | + state=EventState.SCHEDULED, |
| 138 | + ) |
| 139 | + |
| 140 | + event = async_get_calendar_event_from_pickup_event(pickup_event, config_entry) |
| 141 | + assert event.description == "Pickup types: " |
0 commit comments