""" Tests for Pydantic models and schemas """ from datetime import datetime import pytest from models.schemas import EventData, FrigateEvent, HAAttributes, HAEntity, HealthResponse, RootResponse, ServiceStatus class TestServiceStatus: """Test ServiceStatus model""" def test_service_status_creation(self): """Test creating a ServiceStatus instance""" service = ServiceStatus(enabled=True, url="http://example.com", status="healthy") assert service.enabled is True assert service.url == "http://example.com" assert service.status == "healthy" def test_service_status_validation(self): """Test ServiceStatus validation""" # Valid data service = ServiceStatus(enabled=False, url="https://api.example.com", status="unhealthy") assert service.enabled is False def test_service_status_required_fields(self): """Test that required fields are enforced""" with pytest.raises(ValueError): ServiceStatus() # Missing required fields class TestHAAttributes: """Test HAAttributes model""" def test_ha_attributes_creation(self): """Test creating HAAttributes instance""" attrs = HAAttributes(unit_of_measurement="°C", friendly_name="Living Room Temperature") assert attrs.unit_of_measurement == "°C" assert attrs.friendly_name == "Living Room Temperature" def test_ha_attributes_optional_fields(self): """Test that fields are optional""" attrs = HAAttributes() assert attrs.unit_of_measurement is None assert attrs.friendly_name is None class TestHAEntity: """Test HAEntity model""" def test_ha_entity_creation(self): """Test creating HAEntity instance""" attributes = HAAttributes(unit_of_measurement="°C", friendly_name="Temperature") entity = HAEntity(entity_id="sensor.temperature", state="22.5", attributes=attributes) assert entity.entity_id == "sensor.temperature" assert entity.state == "22.5" assert entity.attributes.unit_of_measurement == "°C" class TestFrigateEvent: """Test FrigateEvent model""" def test_frigate_event_creation(self): """Test creating FrigateEvent instance""" event = FrigateEvent( id="event_123", timestamp="2024-01-01T12:00:00Z", camera="front_door", label="person", confidence=0.95, ) assert event.id == "event_123" assert event.camera == "front_door" assert event.label == "person" assert event.confidence == 0.95 def test_frigate_event_confidence_validation(self): """Test confidence validation (0-1 range)""" # Valid confidence event = FrigateEvent( id="event_123", timestamp="2024-01-01T12:00:00Z", camera="front_door", label="person", confidence=0.5, ) assert event.confidence == 0.5 # Invalid confidence (too high) with pytest.raises(ValueError): FrigateEvent( id="event_123", timestamp="2024-01-01T12:00:00Z", camera="front_door", label="person", confidence=1.5, ) # Invalid confidence (negative) with pytest.raises(ValueError): FrigateEvent( id="event_123", timestamp="2024-01-01T12:00:00Z", camera="front_door", label="person", confidence=-0.1, ) class TestEventData: """Test EventData model""" def test_event_data_creation(self): """Test creating EventData instance""" event = EventData( service="home_assistant", event_type="state_changed", metadata={"entity_id": "sensor.temperature", "new_state": "22.5"}, ) assert event.service == "home_assistant" assert event.event_type == "state_changed" assert event.metadata["entity_id"] == "sensor.temperature" def test_event_data_default_metadata(self): """Test default metadata is empty dict""" event = EventData(service="test_service", event_type="test_event") assert event.metadata == {} class TestHealthResponse: """Test HealthResponse model""" def test_health_response_creation(self): """Test creating HealthResponse instance""" timestamp = datetime.now().isoformat() health = HealthResponse(status="healthy", timestamp=timestamp) assert health.status == "healthy" assert health.timestamp == timestamp class TestRootResponse: """Test RootResponse model""" def test_root_response_creation(self): """Test creating RootResponse instance""" response = RootResponse(message="Test API", version="1.0.0") assert response.message == "Test API" assert response.version == "1.0.0"