from datetime import UTC, datetime, timedelta from uuid import uuid4 import pytest from pydantic import ValidationError from app.domain.schemas.timeseries_history import ElementHistoryQuery def _query(**overrides): start = datetime(2026, 1, 1, tzinfo=UTC) values = { "start_time": start, "end_time": start + timedelta(hours=1), "mode": "observed", "elements": [{"element_id": " P1 ", "element_type": "pipe"}], } values.update(overrides) return ElementHistoryQuery(**values) def test_history_query_normalizes_targets_and_device_ids(): query = _query( elements=[ { "element_id": " P1 ", "element_type": "pipe", "device_ids": [" D1 ", "D1", "D2"], } ] ) assert query.elements[0].element_id == "P1" assert query.elements[0].device_ids == ["D1", "D2"] def test_analysis_comparison_requires_run_id(): with pytest.raises(ValidationError, match="run_id is required"): _query(mode="analysis_comparison") def test_other_modes_reject_run_id(): with pytest.raises(ValidationError, match="only valid"): _query(run_id=uuid4()) def test_history_query_rejects_reversed_time_range(): start = datetime(2026, 1, 1, tzinfo=UTC) with pytest.raises(ValidationError, match="start_time must be earlier"): _query(start_time=start, end_time=start)