Skip to content

Commit c9d299d

Browse files
committed
fixup! Implement ListComponents
Add Component tests.
1 parent 2690df8 commit c9d299d

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

tests/component/test_base.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# License: MIT
2+
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for active_at functionality across components."""
5+
6+
from datetime import datetime, timezone
7+
from typing import Literal
8+
from unittest.mock import Mock
9+
10+
import pytest
11+
12+
from frequenz.client.microgrid import ComponentId, Lifetime, MicrogridId
13+
from frequenz.client.microgrid.component._base import Component
14+
from frequenz.client.microgrid.component._category import ComponentCategory
15+
from frequenz.client.microgrid.component._status import ComponentStatus
16+
17+
18+
# Test component subclass
19+
class TestComponent(Component):
20+
"""A simple component implementation for testing."""
21+
22+
category: Literal[ComponentCategory.UNSPECIFIED] = ComponentCategory.UNSPECIFIED
23+
24+
25+
@pytest.mark.parametrize(
26+
"status,lifetime_active,expected",
27+
[
28+
pytest.param(
29+
ComponentStatus.ACTIVE,
30+
True,
31+
True,
32+
id="active status and active lifetime",
33+
),
34+
pytest.param(
35+
ComponentStatus.ACTIVE,
36+
False,
37+
False,
38+
id="active status but inactive lifetime",
39+
),
40+
pytest.param(
41+
ComponentStatus.INACTIVE,
42+
True,
43+
False,
44+
id="inactive status but active lifetime",
45+
),
46+
pytest.param(
47+
ComponentStatus.INACTIVE,
48+
False,
49+
False,
50+
id="inactive status and inactive lifetime",
51+
),
52+
pytest.param(
53+
ComponentStatus.UNSPECIFIED,
54+
True,
55+
True,
56+
id="unspecified status but active lifetime",
57+
),
58+
pytest.param(
59+
ComponentStatus.UNSPECIFIED,
60+
False,
61+
False,
62+
id="unspecified status and inactive lifetime",
63+
),
64+
],
65+
)
66+
def test_component_active_at(
67+
status: ComponentStatus,
68+
lifetime_active: bool,
69+
expected: bool,
70+
caplog: pytest.LogCaptureFixture,
71+
) -> None:
72+
"""Test active_at behavior with different status and lifetime combinations.
73+
74+
Args:
75+
status: The component status to test with
76+
lifetime_active: Whether the lifetime should report as active
77+
expected: The expected result
78+
caplog: Fixture to capture log messages
79+
80+
Raises:
81+
AssertionError: If any of the assertions fail.
82+
"""
83+
caplog.set_level("WARNING")
84+
85+
mock_lifetime = Mock(spec=Lifetime)
86+
mock_lifetime.active_at.return_value = lifetime_active
87+
88+
component = TestComponent(
89+
id=ComponentId(1),
90+
category=ComponentCategory.UNSPECIFIED,
91+
microgrid_id=MicrogridId(1),
92+
status=status,
93+
operational_lifetime=mock_lifetime,
94+
)
95+
96+
test_time = datetime.now(timezone.utc)
97+
assert component.active_at(test_time) == expected
98+
99+
# Verify lifetime was checked
100+
if component.status == ComponentStatus.ACTIVE:
101+
mock_lifetime.active_at.assert_called_once_with(test_time)
102+
103+
# Verify warning for unspecified status
104+
if status is ComponentStatus.UNSPECIFIED:
105+
assert "unspecified status" in caplog.text.lower()

0 commit comments

Comments
 (0)