|
| 1 | +"""Test KPI integration with simulator-worker.""" |
| 2 | + |
| 3 | +import datetime |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import esdl |
| 9 | +import pytest |
| 10 | + |
| 11 | +# Check if full simulator worker can be imported |
| 12 | +SIMULATOR_AVAILABLE = False |
| 13 | +try: |
| 14 | + from simulator_worker.simulator_worker import simulator_worker_task |
| 15 | + from omotes_simulator_core.infrastructure.utils import pyesdl_from_string |
| 16 | + |
| 17 | + SIMULATOR_AVAILABLE = True |
| 18 | +except ImportError: |
| 19 | + simulator_worker_task = None # type: ignore[assignment, misc] |
| 20 | + pyesdl_from_string = None # type: ignore[assignment, misc] |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.skipif( |
| 24 | + not SIMULATOR_AVAILABLE or os.getenv("INFLUXDB_HOSTNAME") is None, |
| 25 | + reason="simulator-worker or InfluxDB not available" |
| 26 | +) |
| 27 | +class TestKPIEndToEndIntegration: |
| 28 | + """Integration tests for end-to-end KPI calculation in simulator workflow.""" |
| 29 | + |
| 30 | + def test_kpis_included_in_output_esdl(self) -> None: |
| 31 | + """Test that KPIs are calculated and included in output ESDL.""" |
| 32 | + # Load test ESDL |
| 33 | + test_esdl_path = Path(__file__).parent.parent / "testdata" / "test1.esdl" |
| 34 | + with open(test_esdl_path, "r") as f: |
| 35 | + input_esdl = f.read() |
| 36 | + |
| 37 | + # Configure workflow - use Unix timestamps (seconds since epoch) |
| 38 | + start_time = datetime.datetime(2019, 1, 1, 0, 0, tzinfo=datetime.timezone.utc) |
| 39 | + end_time = datetime.datetime(2019, 1, 1, 2, 0, tzinfo=datetime.timezone.utc) |
| 40 | + |
| 41 | + workflow_config: dict[str, list[float] | float | str | bool] = { |
| 42 | + "timestep": 3600.0, # 1 hour in seconds |
| 43 | + "start_time": start_time.timestamp(), # Unix timestamp |
| 44 | + "end_time": end_time.timestamp(), # Unix timestamp |
| 45 | + "system_lifetime": 30.0, |
| 46 | + } |
| 47 | + |
| 48 | + # Mock progress handler |
| 49 | + mock_progress = MagicMock() |
| 50 | + |
| 51 | + # Run simulation with KPI calculation |
| 52 | + output_esdl, _ = simulator_worker_task( |
| 53 | + input_esdl, workflow_config, mock_progress, "simulator" |
| 54 | + ) |
| 55 | + |
| 56 | + # Verify output is not None |
| 57 | + assert output_esdl is not None |
| 58 | + assert len(output_esdl) > 0 |
| 59 | + |
| 60 | + # Parse output ESDL |
| 61 | + esh = pyesdl_from_string(output_esdl) |
| 62 | + energy_system = esh.energy_system |
| 63 | + |
| 64 | + assert hasattr(energy_system, "KPIs"), "Energy system should have KPIs attribute" |
| 65 | + kpis = energy_system.KPIs |
| 66 | + assert kpis is not None, "KPIs should be calculated and present in output ESDL" |
| 67 | + |
| 68 | + assert hasattr(kpis, "kpi"), "KPIs should have kpi collection" |
| 69 | + kpi_list = list(kpis.kpi) |
| 70 | + assert len(kpi_list) > 0, "At least one KPI should be present in output ESDL" |
| 71 | + |
| 72 | + for kpi in kpi_list: |
| 73 | + assert isinstance(kpi, esdl.DistributionKPI), f"Expected DistributionKPI, got {type(kpi)}" |
| 74 | + assert kpi.name, "KPI should have a non-empty name" |
| 75 | + assert kpi.distribution is not None, f"KPI '{kpi.name}' has no distribution" |
| 76 | + items = list(kpi.distribution.stringItem) |
| 77 | + assert len(items) > 0, f"KPI '{kpi.name}' distribution has no items" |
| 78 | + for item in items: |
| 79 | + assert item.value is not None, f"KPI '{kpi.name}' item '{item.label}' has no value" |
| 80 | + |
| 81 | + # InfluxDB profile references must survive the KPI enrichment step |
| 82 | + all_ports = [ |
| 83 | + port |
| 84 | + for asset in energy_system.eAllContents() |
| 85 | + if isinstance(asset, esdl.Asset) |
| 86 | + for port in asset.port |
| 87 | + ] |
| 88 | + assert any( |
| 89 | + len(port.profile) > 0 for port in all_ports |
| 90 | + ), "Output ESDL should retain InfluxDB profile references from simulation results" |
0 commit comments