|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for ProblematicComponent components.""" |
| 5 | + |
| 6 | +import pytest |
| 7 | +from frequenz.client.common.microgrid import MicrogridId |
| 8 | +from frequenz.client.common.microgrid.components import ComponentId |
| 9 | + |
| 10 | +from frequenz.client.microgrid.component import ( |
| 11 | + ComponentCategory, |
| 12 | + ComponentStatus, |
| 13 | + MismatchedCategoryComponent, |
| 14 | + ProblematicComponent, |
| 15 | + UnrecognizedComponent, |
| 16 | + UnspecifiedComponent, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def component_id() -> ComponentId: |
| 22 | + """Provide a test component ID.""" |
| 23 | + return ComponentId(42) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def microgrid_id() -> MicrogridId: |
| 28 | + """Provide a test microgrid ID.""" |
| 29 | + return MicrogridId(1) |
| 30 | + |
| 31 | + |
| 32 | +def test_abstract_problematic_component_cannot_be_instantiated( |
| 33 | + component_id: ComponentId, microgrid_id: MicrogridId |
| 34 | +) -> None: |
| 35 | + """Test that ProblematicComponent base class cannot be instantiated.""" |
| 36 | + with pytest.raises( |
| 37 | + TypeError, match="Cannot instantiate ProblematicComponent directly" |
| 38 | + ): |
| 39 | + ProblematicComponent( |
| 40 | + id=component_id, |
| 41 | + microgrid_id=microgrid_id, |
| 42 | + name="test_problematic", |
| 43 | + manufacturer="test_manufacturer", |
| 44 | + model_name="test_model", |
| 45 | + status=ComponentStatus.ACTIVE, |
| 46 | + category=ComponentCategory.UNSPECIFIED, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def test_unspecified_component( |
| 51 | + component_id: ComponentId, microgrid_id: MicrogridId |
| 52 | +) -> None: |
| 53 | + """Test initialization and properties of UnspecifiedComponent.""" |
| 54 | + component = UnspecifiedComponent( |
| 55 | + id=component_id, |
| 56 | + microgrid_id=microgrid_id, |
| 57 | + name="unspecified_component", |
| 58 | + manufacturer="test_manufacturer", |
| 59 | + model_name="test_model", |
| 60 | + status=ComponentStatus.ACTIVE, |
| 61 | + ) |
| 62 | + |
| 63 | + assert component.id == component_id |
| 64 | + assert component.microgrid_id == microgrid_id |
| 65 | + assert component.name == "unspecified_component" |
| 66 | + assert component.manufacturer == "test_manufacturer" |
| 67 | + assert component.model_name == "test_model" |
| 68 | + assert component.status == ComponentStatus.ACTIVE |
| 69 | + assert component.category == ComponentCategory.UNSPECIFIED |
| 70 | + |
| 71 | + |
| 72 | +def test_mismatched_category_component_with_known_category( |
| 73 | + component_id: ComponentId, microgrid_id: MicrogridId |
| 74 | +) -> None: |
| 75 | + """Test MismatchedCategoryComponent with a known ComponentCategory.""" |
| 76 | + expected_category = ComponentCategory.BATTERY |
| 77 | + component = MismatchedCategoryComponent( |
| 78 | + id=component_id, |
| 79 | + microgrid_id=microgrid_id, |
| 80 | + name="mismatched_battery", |
| 81 | + manufacturer="test_manufacturer", |
| 82 | + model_name="test_model", |
| 83 | + status=ComponentStatus.ACTIVE, |
| 84 | + category=expected_category, |
| 85 | + ) |
| 86 | + |
| 87 | + assert component.id == component_id |
| 88 | + assert component.microgrid_id == microgrid_id |
| 89 | + assert component.name == "mismatched_battery" |
| 90 | + assert component.manufacturer == "test_manufacturer" |
| 91 | + assert component.model_name == "test_model" |
| 92 | + assert component.status == ComponentStatus.ACTIVE |
| 93 | + assert component.category == expected_category |
| 94 | + |
| 95 | + |
| 96 | +def test_mismatched_category_component_with_unrecognized_category( |
| 97 | + component_id: ComponentId, microgrid_id: MicrogridId |
| 98 | +) -> None: |
| 99 | + """Test MismatchedCategoryComponent with an unrecognized integer category.""" |
| 100 | + expected_category = 999 |
| 101 | + component = MismatchedCategoryComponent( |
| 102 | + id=component_id, |
| 103 | + microgrid_id=microgrid_id, |
| 104 | + name="mismatched_unrecognized", |
| 105 | + manufacturer="test_manufacturer", |
| 106 | + model_name="test_model", |
| 107 | + status=ComponentStatus.ACTIVE, |
| 108 | + category=expected_category, |
| 109 | + ) |
| 110 | + |
| 111 | + assert component.id == component_id |
| 112 | + assert component.microgrid_id == microgrid_id |
| 113 | + assert component.name == "mismatched_unrecognized" |
| 114 | + assert component.manufacturer == "test_manufacturer" |
| 115 | + assert component.model_name == "test_model" |
| 116 | + assert component.status == ComponentStatus.ACTIVE |
| 117 | + assert component.category == expected_category |
| 118 | + |
| 119 | + |
| 120 | +def test_unrecognized_component_type( |
| 121 | + component_id: ComponentId, microgrid_id: MicrogridId |
| 122 | +) -> None: |
| 123 | + """Test initialization and properties of UnrecognizedComponent type.""" |
| 124 | + component = UnrecognizedComponent( |
| 125 | + id=component_id, |
| 126 | + microgrid_id=microgrid_id, |
| 127 | + name="unrecognized_component", |
| 128 | + manufacturer="test_manufacturer", |
| 129 | + model_name="test_model", |
| 130 | + status=ComponentStatus.ACTIVE, |
| 131 | + category=999, |
| 132 | + ) |
| 133 | + |
| 134 | + assert component.id == component_id |
| 135 | + assert component.microgrid_id == microgrid_id |
| 136 | + assert component.name == "unrecognized_component" |
| 137 | + assert component.manufacturer == "test_manufacturer" |
| 138 | + assert component.model_name == "test_model" |
| 139 | + assert component.status == ComponentStatus.ACTIVE |
| 140 | + assert component.category == 999 |
0 commit comments