33
44"""Tests for ComponentConnection class and related functionality."""
55
6- from datetime import datetime , timedelta , timezone
6+ from datetime import datetime , timezone
77from typing import Any
8- from unittest .mock import Mock , PropertyMock , patch
8+ from unittest .mock import Mock , patch
99
1010import pytest
1111from frequenz .api .common .v1 .microgrid import lifetime_pb2
1919)
2020
2121
22- def test_component_connection_construction () -> None :
23- """Test basic ComponentConnection construction and validation.
24-
25- Raises:
26- AssertionError: If any of the assertions fail.
27- """
22+ def test_creation () -> None :
23+ """Test basic ComponentConnection creation and validation."""
2824 now = datetime .now (timezone .utc )
2925 lifetime = Lifetime (start = now )
3026 connection = ComponentConnection (
@@ -38,7 +34,7 @@ def test_component_connection_construction() -> None:
3834 assert connection .operational_lifetime == lifetime
3935
4036
41- def test_component_connection_validation () -> None :
37+ def test_validation () -> None :
4238 """Test validation of source and destination components."""
4339 with pytest .raises (
4440 ValueError , match = "Source and destination components must be different"
@@ -49,19 +45,30 @@ def test_component_connection_validation() -> None:
4945 )
5046
5147
48+ @pytest .mark .parametrize ("lifetime_active" , [True , False ])
49+ def test_active_at (lifetime_active : bool ) -> None :
50+ """Test active_at behavior with lifetime.active values."""
51+ mock_lifetime = Mock (spec = Lifetime )
52+ mock_lifetime .active_at .return_value = lifetime_active
53+
54+ connection = ComponentConnection (
55+ source = ComponentId (1 ),
56+ destination = ComponentId (2 ),
57+ operational_lifetime = mock_lifetime ,
58+ )
59+
60+ test_time = datetime .now (timezone .utc )
61+
62+ assert connection .active_at (test_time ) == lifetime_active
63+ mock_lifetime .active_at .assert_called_once_with (test_time )
64+
65+
5266@pytest .mark .parametrize (
5367 "lifetime_active" ,
5468 [True , False ],
5569)
56- def test_component_connection_active_property (lifetime_active : bool ) -> None :
57- """Test the active property of ComponentConnection.
58-
59- Args:
60- lifetime_active: Whether the lifetime should be active or not.
61-
62- Raises:
63- AssertionError: If any of the assertions fail.
64- """
70+ def test_active (lifetime_active : bool ) -> None :
71+ """Test the active property of ComponentConnection."""
6572 mock_lifetime = Mock (spec = Lifetime )
6673 mock_lifetime .active_at .return_value = lifetime_active
6774
@@ -81,12 +88,8 @@ def test_component_connection_active_property(lifetime_active: bool) -> None:
8188 assert connection .active is lifetime_active
8289
8390
84- def test_component_connection_string_representation () -> None :
85- """Test string representation of ComponentConnection.
86-
87- Raises:
88- AssertionError: If the string representation is incorrect.
89- """
91+ def test_str () -> None :
92+ """Test string representation of ComponentConnection."""
9093 connection = ComponentConnection (
9194 source = ComponentId (1 ),
9295 destination = ComponentId (2 ),
@@ -115,19 +118,11 @@ def test_component_connection_string_representation() -> None:
115118 ),
116119 ],
117120)
118- def test_component_connection_from_proto (
121+ def test_from_proto (
119122 proto_data : dict [str , Any ],
120123 caplog : pytest .LogCaptureFixture ,
121124) -> None :
122- """Test conversion from protobuf message to ComponentConnection.
123-
124- Args:
125- proto_data: Test case parameters
126- caplog: Fixture to capture log messages
127-
128- Raises:
129- AssertionError: If any of the assertions fail.
130- """
125+ """Test conversion from protobuf message to ComponentConnection."""
131126 caplog .set_level ("DEBUG" ) # Ensure we capture DEBUG level messages
132127
133128 proto = components_pb2 .ComponentConnection (
@@ -152,7 +147,7 @@ def test_component_connection_from_proto(
152147 assert "missing operational lifetime" in caplog .text .lower ()
153148
154149
155- def test_component_connection_from_proto_same_ids () -> None :
150+ def test_from_proto_same_ids () -> None :
156151 """Test proto conversion with same source and destination IDs."""
157152 proto = components_pb2 .ComponentConnection (
158153 source_component_id = 1 ,
@@ -166,19 +161,11 @@ def test_component_connection_from_proto_same_ids() -> None:
166161
167162
168163@patch ("frequenz.client.microgrid.component._connection_proto.lifetime_from_proto" )
169- def test_component_connection_from_proto_invalid_lifetime (
164+ def test_from_proto_invalid_lifetime (
170165 mock_lifetime_from_proto : Mock ,
171166 caplog : pytest .LogCaptureFixture ,
172167) -> None :
173- """Test proto conversion with invalid lifetime data.
174-
175- Args:
176- mock_lifetime_from_proto: Mock for lifetime conversion
177- caplog: Fixture to capture log messages
178-
179- Raises:
180- AssertionError: If any of the assertions fail.
181- """
168+ """Test proto conversion with invalid lifetime data."""
182169 mock_lifetime_from_proto .side_effect = ValueError ("Invalid lifetime" )
183170
184171 proto = components_pb2 .ComponentConnection (
@@ -198,49 +185,3 @@ def test_component_connection_from_proto_invalid_lifetime(
198185 assert connection .destination == ComponentId (2 )
199186 assert "invalid operational lifetime" in caplog .text .lower ()
200187 mock_lifetime_from_proto .assert_called_once_with (proto .operational_lifetime )
201-
202-
203- @pytest .mark .parametrize (
204- "case" ,
205- [
206- pytest .param (
207- {"test_time" : "past" , "expected" : True },
208- id = "past timestamp" ,
209- ),
210- pytest .param (
211- {"test_time" : "now" , "expected" : True },
212- id = "current timestamp" ,
213- ),
214- pytest .param (
215- {"test_time" : "future" , "expected" : True },
216- id = "future timestamp" ,
217- ),
218- ],
219- )
220- def test_component_connection_active_at (case : dict [str , Any ]) -> None :
221- """Test checking activity at specific timestamps.
222-
223- Args:
224- case: Test case parameters
225-
226- Raises:
227- AssertionError: If any of the assertions fail.
228- """
229- mock_lifetime = Mock (spec = Lifetime )
230- mock_lifetime .active_at = Mock (return_value = case ["expected" ])
231-
232- connection = ComponentConnection (
233- source = ComponentId (1 ),
234- destination = ComponentId (2 ),
235- operational_lifetime = mock_lifetime ,
236- )
237-
238- now = datetime .now (timezone .utc )
239- test_time = {
240- "past" : now - timedelta (hours = 1 ),
241- "now" : now ,
242- "future" : now + timedelta (hours = 1 ),
243- }[case ["test_time" ]]
244-
245- assert connection .active_at (test_time ) == case ["expected" ]
246- mock_lifetime .active_at .assert_called_once_with (test_time )
0 commit comments