"""Tests for accommodation functionality.""" import tempfile from datetime import date, datetime from typing import Any import pytest import yaml from agenda.accommodation import get_events from agenda.event import Event class TestGetEvents: """Test the get_events function.""" def test_get_events_airbnb(self) -> None: """Test getting accommodation events for Airbnb.""" accommodation_data = [ { "from": date(2024, 6, 1), "to": date(2024, 6, 5), "location": "Paris", "operator": "airbnb", "url": "https://airbnb.com/rooms/123" } ] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert len(events) == 1 event = events[0] assert event.date == date(2024, 6, 1) assert event.end_date == date(2024, 6, 5) assert event.name == "accommodation" assert event.title == "Paris Airbnb" assert event.url == "https://airbnb.com/rooms/123" finally: import os os.unlink(filepath) def test_get_events_hotel(self) -> None: """Test getting accommodation events for hotel.""" accommodation_data = [ { "from": date(2024, 6, 1), "to": date(2024, 6, 5), "name": "Hilton Hotel", "url": "https://hilton.com" } ] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert len(events) == 1 event = events[0] assert event.date == date(2024, 6, 1) assert event.end_date == date(2024, 6, 5) assert event.name == "accommodation" assert event.title == "Hilton Hotel" assert event.url == "https://hilton.com" finally: import os os.unlink(filepath) def test_get_events_no_url(self) -> None: """Test getting accommodation events without URL.""" accommodation_data = [ { "from": date(2024, 6, 1), "to": date(2024, 6, 5), "name": "Local B&B" } ] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert len(events) == 1 event = events[0] assert event.url is None assert event.title == "Local B&B" finally: import os os.unlink(filepath) def test_get_events_multiple_accommodations(self) -> None: """Test getting multiple accommodation events.""" accommodation_data = [ { "from": date(2024, 6, 1), "to": date(2024, 6, 5), "location": "London", "operator": "airbnb" }, { "from": date(2024, 6, 10), "to": date(2024, 6, 15), "name": "Royal Hotel" } ] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert len(events) == 2 # Check first accommodation (Airbnb) assert events[0].title == "London Airbnb" assert events[0].date == date(2024, 6, 1) # Check second accommodation (Hotel) assert events[1].title == "Royal Hotel" assert events[1].date == date(2024, 6, 10) finally: import os os.unlink(filepath) def test_get_events_empty_file(self) -> None: """Test getting events from empty file.""" accommodation_data: list[dict[str, Any]] = [] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert events == [] finally: import os os.unlink(filepath) def test_get_events_file_not_found(self) -> None: """Test error handling when file doesn't exist.""" with pytest.raises(FileNotFoundError): get_events("/nonexistent/file.yaml") def test_get_events_datetime_objects(self) -> None: """Test with datetime objects instead of dates.""" accommodation_data = [ { "from": datetime(2024, 6, 1, 15, 0), "to": datetime(2024, 6, 5, 11, 0), "name": "Hotel Example" } ] with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: yaml.dump(accommodation_data, f) filepath = f.name try: events = get_events(filepath) assert len(events) == 1 event = events[0] assert event.date == datetime(2024, 6, 1, 15, 0) assert event.end_date == datetime(2024, 6, 5, 11, 0) finally: import os os.unlink(filepath)