Skip to content

Commit 4bd97e0

Browse files
committed
Use the core warnings helpers in the tests
The tests used `warnings.catch_warnings()` blocks for two different things, now done by the `frequenz.core.warnings` helper made for each: * An "ignore" filter, to call deprecated APIs the test needs as setup, becomes `ignoring_deprecations()`, like in the library. * An "error" filter, to check that a replacement API doesn't go through a deprecated one, becomes `asserting_no_deprecations()`. It records the warnings instead of raising them inside the code under test, so a broad `except` there can't swallow the failure, and it reports every offending warning with its location when the block ends. The blocks that record warnings to check what a helper lets through are left alone: that is what `catch_warnings(record=True)` is for. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent 75d2e73 commit 4bd97e0

12 files changed

Lines changed: 42 additions & 74 deletions

‎tests/grid/_delivery_area/test_delivery_area.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
"""Tests for the DeliveryArea class."""
55

6-
import warnings
76
from dataclasses import dataclass
87

98
import pytest
9+
from frequenz.core.warnings import asserting_no_deprecations
1010

1111
from frequenz.client.common import (
1212
UnrecognizedEnumValueError,
@@ -62,8 +62,7 @@ class _TestCase:
6262
)
6363
def test_creation_valid(case: _TestCase) -> None:
6464
"""Well-formed DeliveryArea construction succeeds without warnings."""
65-
with warnings.catch_warnings():
66-
warnings.simplefilter("error", DeprecationWarning)
65+
with asserting_no_deprecations():
6766
area = DeliveryArea(code=case.code, code_type=case.code_type)
6867
assert area.code == case.code
6968
assert area.code_type == case.code_type
@@ -161,8 +160,7 @@ def test_creation_with_unspecified_code_type_member_emits_deprecation_warning()
161160
)
162161
def test_creation_raises_on_invalid_code(case: _TestCase) -> None:
163162
"""`_raise_on_invalid=True` raises `ValueError` on empty/`None` code."""
164-
with warnings.catch_warnings():
165-
warnings.simplefilter("error", DeprecationWarning)
163+
with asserting_no_deprecations():
166164
with pytest.raises(ValueError, match="`code` cannot be None or empty"):
167165
DeliveryArea(
168166
code=case.code,
@@ -173,8 +171,7 @@ def test_creation_raises_on_invalid_code(case: _TestCase) -> None:
173171

174172
def test_creation_raises_on_unspecified_int_code_type() -> None:
175173
"""`_raise_on_invalid=True` raises `ValueError` on `code_type=0`."""
176-
with warnings.catch_warnings():
177-
warnings.simplefilter("error", DeprecationWarning)
174+
with asserting_no_deprecations():
178175
with pytest.raises(
179176
ValueError, match="`code_type` cannot be 0 \\(UNSPECIFIED\\)"
180177
):
@@ -185,8 +182,7 @@ def test_creation_raises_on_unspecified_member_code_type() -> None:
185182
"""`_raise_on_invalid=True` raises `ValueError` on the UNSPECIFIED member."""
186183
with pytest.deprecated_call():
187184
unspecified = EnergyMarketCodeType.UNSPECIFIED
188-
with warnings.catch_warnings():
189-
warnings.simplefilter("error", DeprecationWarning)
185+
with asserting_no_deprecations():
190186
with pytest.raises(
191187
ValueError, match="`code_type` cannot be 0 \\(UNSPECIFIED\\)"
192188
):
@@ -195,8 +191,7 @@ def test_creation_raises_on_unspecified_member_code_type() -> None:
195191

196192
def test_creation_does_not_raise_when_valid() -> None:
197193
"""`_raise_on_invalid=True` does not raise on well-formed data."""
198-
with warnings.catch_warnings():
199-
warnings.simplefilter("error", DeprecationWarning)
194+
with asserting_no_deprecations():
200195
area = DeliveryArea(
201196
code="DE",
202197
code_type=EnergyMarketCodeType.EUROPE_EIC,
@@ -263,8 +258,7 @@ def test_get_code_type_raises_unspecified_for_value_zero_member() -> None:
263258
unspecified = EnergyMarketCodeType.UNSPECIFIED
264259
with pytest.deprecated_call():
265260
area = DeliveryArea(code="TEST", code_type=unspecified)
266-
with warnings.catch_warnings():
267-
warnings.simplefilter("error", DeprecationWarning)
261+
with asserting_no_deprecations():
268262
with pytest.raises(UnspecifiedEnumValueError):
269263
area.get_code_type()
270264

‎tests/grid/_delivery_area/test_invalid_delivery_area.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
"""Tests for the InvalidDeliveryArea class."""
55

6-
import warnings
76
from dataclasses import dataclass
87

98
import pytest
9+
from frequenz.core.warnings import asserting_no_deprecations
1010

1111
from frequenz.client.common.grid import (
1212
BaseDeliveryArea,
@@ -70,8 +70,7 @@ def test_is_base_delivery_area_subclass() -> None:
7070
)
7171
def test_creation(case: _TestCase) -> None:
7272
"""`InvalidDeliveryArea` accepts any data with no invariants and no warnings."""
73-
with warnings.catch_warnings():
74-
warnings.simplefilter("error", DeprecationWarning)
73+
with asserting_no_deprecations():
7574
area = InvalidDeliveryArea(code=case.code, code_type=case.code_type)
7675
assert area.code == case.code
7776
assert area.code_type == case.code_type

‎tests/grid/proto/v1alpha8/test_delivery_area.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010
from frequenz.api.common.v1alpha8.grid import delivery_area_pb2
11+
from frequenz.core.warnings import asserting_no_deprecations
1112

1213
from frequenz.client.common import UnspecifiedEnumValueError
1314
from frequenz.client.common.grid import (
@@ -264,8 +265,7 @@ def test_from_proto2(
264265
code=case.code, code_type=case.code_type # type: ignore[arg-type]
265266
)
266267
with caplog.at_level("WARNING"):
267-
with warnings.catch_warnings():
268-
warnings.simplefilter("error", DeprecationWarning)
268+
with asserting_no_deprecations():
269269
area = delivery_area_from_proto2(proto)
270270

271271
assert isinstance(area, case.expected_type)

‎tests/metrics/proto/v1alpha8/test_sample_metric_connection.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
"""Tests for MetricConnection protobuf conversion."""
55

6-
import warnings
7-
86
import pytest
97
from frequenz.api.common.v1alpha8.metrics import metrics_pb2
8+
from frequenz.core.warnings import asserting_no_deprecations
109

1110
from frequenz.client.common.metrics import MetricConnectionCategory
1211
from frequenz.client.common.metrics.proto.v1alpha8 import (
@@ -111,8 +110,7 @@ def test_from_proto_unspecified_category() -> None:
111110
name="some_connection",
112111
)
113112

114-
with warnings.catch_warnings():
115-
warnings.simplefilter("error", DeprecationWarning)
113+
with asserting_no_deprecations():
116114
connection = metric_connection_from_proto(proto)
117115

118116
assert connection.category == 0

‎tests/metrics/proto/v1alpha8/test_sample_metric_sample.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"""Tests for MetricSample protobuf conversion."""
55

66
import math
7-
import warnings
87
from dataclasses import dataclass, field
98
from datetime import datetime, timezone
109
from typing import Final
1110

1211
import pytest
1312
from frequenz.api.common.v1alpha8.metrics import bounds_pb2, metrics_pb2
13+
from frequenz.core.warnings import asserting_no_deprecations
1414
from google.protobuf.timestamp_pb2 import Timestamp
1515

1616
from frequenz.client.common import InvalidDatetime, InvalidDatetimeError
@@ -373,8 +373,7 @@ def test_from_proto_unspecified_metric() -> None:
373373
),
374374
)
375375

376-
with warnings.catch_warnings():
377-
warnings.simplefilter("error", DeprecationWarning)
376+
with asserting_no_deprecations():
378377
sample = metric_sample_from_proto(proto)
379378

380379
assert sample.metric == 0

‎tests/microgrid/electrical_components/proto/v1alpha8/conftest.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Fixtures and utilities for testing electrical component protobuf conversion."""
55

6-
import warnings
76
from datetime import datetime, timezone
87

98
import pytest
@@ -12,6 +11,7 @@
1211
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
1312
electrical_components_pb2,
1413
)
14+
from frequenz.core.warnings import ignoring_deprecations
1515
from google.protobuf.timestamp_pb2 import Timestamp
1616

1717
from frequenz.client.common.metrics import Bounds, BoundsSet, Metric
@@ -53,8 +53,7 @@ def default_component_base_data(
5353
component_id: ElectricalComponentId, microgrid_id: MicrogridId
5454
) -> _ElectricalComponentBaseData:
5555
"""Provide a fixture for common component fields."""
56-
with warnings.catch_warnings():
57-
warnings.simplefilter("ignore", DeprecationWarning)
56+
with ignoring_deprecations():
5857
category = ElectricalComponentCategory.UNSPECIFIED
5958
return _ElectricalComponentBaseData(
6059
component_id=component_id,

‎tests/microgrid/electrical_components/proto/v1alpha8/test_electrical_component_base.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"""Tests for protobuf conversion of the base/common part of electrical components."""
55

66
import math
7-
import warnings
87
from datetime import timezone
98

109
import pytest
1110
from frequenz.api.common.v1alpha8.metrics import bounds_pb2, metrics_pb2
1211
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
1312
electrical_components_pb2,
1413
)
14+
from frequenz.core.warnings import ignoring_deprecations
1515
from google.protobuf.timestamp_pb2 import Timestamp
1616

1717
from frequenz.client.common.metrics import (
@@ -85,8 +85,7 @@ def test_operational_mode_to_bools(
8585

8686
def test_complete(default_component_base_data: _ElectricalComponentBaseData) -> None:
8787
"""Test parsing of a complete base component proto."""
88-
with warnings.catch_warnings():
89-
warnings.simplefilter("ignore", DeprecationWarning)
88+
with ignoring_deprecations():
9089
category = ElectricalComponentCategory.CHP # Just to pick a valid category
9190
base_data = default_component_base_data._replace(category=category)
9291
proto = base_data_as_proto(base_data)
@@ -99,8 +98,7 @@ def test_missing_category_specific_info(
9998
default_component_base_data: _ElectricalComponentBaseData,
10099
) -> None:
101100
"""Test parsing with missing optional category specific info."""
102-
with warnings.catch_warnings():
103-
warnings.simplefilter("ignore", DeprecationWarning)
101+
with ignoring_deprecations():
104102
category = ElectricalComponentCategory.UNSPECIFIED
105103
base_data = default_component_base_data._replace(
106104
name="",
@@ -122,8 +120,7 @@ def test_empty_lifetime_is_unbounded(
122120
default_component_base_data: _ElectricalComponentBaseData,
123121
) -> None:
124122
"""A present but empty protobuf lifetime becomes an unbounded `Lifetime`."""
125-
with warnings.catch_warnings():
126-
warnings.simplefilter("ignore", DeprecationWarning)
123+
with ignoring_deprecations():
127124
category = ElectricalComponentCategory.CHP
128125
base_data = default_component_base_data._replace(
129126
category=category,
@@ -141,8 +138,7 @@ def test_category_specific_info_mismatch(
141138
default_component_base_data: _ElectricalComponentBaseData,
142139
) -> None:
143140
"""Test category and category specific info mismatch."""
144-
with warnings.catch_warnings():
145-
warnings.simplefilter("ignore", DeprecationWarning)
141+
with ignoring_deprecations():
146142
category = ElectricalComponentCategory.GRID_CONNECTION_POINT
147143
base_data = default_component_base_data._replace(
148144
category=category,
@@ -165,8 +161,7 @@ def test_invalid_lifetime(
165161
default_component_base_data: _ElectricalComponentBaseData,
166162
) -> None:
167163
"""Test invalid lifetime (start after end)."""
168-
with warnings.catch_warnings():
169-
warnings.simplefilter("ignore", DeprecationWarning)
164+
with ignoring_deprecations():
170165
category = ElectricalComponentCategory.CHP
171166
base_data = default_component_base_data._replace(
172167
category=category,
@@ -204,8 +199,7 @@ def _metric_bound(
204199

205200
def test_metric_config_bounds_stores_unspecified_as_int() -> None:
206201
"""Test UNSPECIFIED metric bounds load as plain int key 0."""
207-
with warnings.catch_warnings():
208-
warnings.simplefilter("ignore", DeprecationWarning)
202+
with ignoring_deprecations():
209203
unspecified = Metric.UNSPECIFIED
210204
message = [
211205
_metric_bound(int(unspecified.value), 0.0, 1.0),

‎tests/microgrid/electrical_components/proto/v1alpha8/test_electrical_component_simple.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Tests for protobuf conversion of simple electrical components."""
55

6-
import warnings
7-
86
import pytest
97
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
108
electrical_components_pb2,
119
)
10+
from frequenz.core.warnings import ignoring_deprecations
1211

1312
from frequenz.client.common.microgrid.electrical_components import (
1413
Breaker,
@@ -100,8 +99,7 @@ def test_category_mismatch(
10099
assert electrical_component_class_to_proto(component) == (1, None)
101100

102101

103-
with warnings.catch_warnings():
104-
warnings.simplefilter("ignore", DeprecationWarning)
102+
with ignoring_deprecations():
105103
_TRIVIAL_CASES = [
106104
pytest.param(ElectricalComponentCategory.BREAKER, Breaker, id="Breaker"),
107105
pytest.param(
@@ -167,8 +165,7 @@ def test_power_transformer(
167165
secondary: float | None,
168166
) -> None:
169167
"""Test PowerTransformer component."""
170-
with warnings.catch_warnings():
171-
warnings.simplefilter("ignore", DeprecationWarning)
168+
with ignoring_deprecations():
172169
category = ElectricalComponentCategory.POWER_TRANSFORMER
173170
base_data = default_component_base_data._replace(category=category)
174171

@@ -196,8 +193,7 @@ def test_grid(
196193
rated_fuse_current: int | None,
197194
) -> None:
198195
"""Test GridConnectionPoint component with default values."""
199-
with warnings.catch_warnings():
200-
warnings.simplefilter("ignore", DeprecationWarning)
196+
with ignoring_deprecations():
201197
category = ElectricalComponentCategory.GRID_CONNECTION_POINT
202198
base_data = default_component_base_data._replace(category=category)
203199

‎tests/microgrid/electrical_components/proto/v1alpha8/test_electrical_component_with_type.py‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Tests for protobuf conversion of components with a type."""
55

6-
import warnings
7-
86
import pytest
97
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
108
electrical_components_pb2,
119
)
10+
from frequenz.core.warnings import ignoring_deprecations
1211

1312
from frequenz.client.common.microgrid.electrical_components import (
1413
AcEvCharger,
@@ -72,8 +71,7 @@ def test_battery(
7271
pb_battery_type: int,
7372
) -> None:
7473
"""Test battery component."""
75-
with warnings.catch_warnings():
76-
warnings.simplefilter("ignore", DeprecationWarning)
74+
with ignoring_deprecations():
7775
category = ElectricalComponentCategory.BATTERY
7876
base_data = default_component_base_data._replace(category=category)
7977
proto = base_data_as_proto(base_data)
@@ -126,8 +124,7 @@ def test_ev_charger(
126124
pb_ev_charger_type: int,
127125
) -> None:
128126
"""Test EV Charger component."""
129-
with warnings.catch_warnings():
130-
warnings.simplefilter("ignore", DeprecationWarning)
127+
with ignoring_deprecations():
131128
category = ElectricalComponentCategory.EV_CHARGER
132129
base_data = default_component_base_data._replace(category=category)
133130
proto = base_data_as_proto(base_data)
@@ -180,8 +177,7 @@ def test_inverter(
180177
pb_inverter_type: int,
181178
) -> None:
182179
"""Test inverter component."""
183-
with warnings.catch_warnings():
184-
warnings.simplefilter("ignore", DeprecationWarning)
180+
with ignoring_deprecations():
185181
category = ElectricalComponentCategory.INVERTER
186182
base_data = default_component_base_data._replace(category=category)
187183
proto = base_data_as_proto(base_data)

‎tests/microgrid/electrical_components/proto/v1alpha8/test_enum_deprecation.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
"""Tests for deprecation of the category enum and its proto converters."""
55

6-
import warnings
7-
86
import pytest
97
from frequenz.api.common.v1alpha8.microgrid.electrical_components import (
108
electrical_components_pb2,
119
)
10+
from frequenz.core.warnings import asserting_no_deprecations, ignoring_deprecations
1211

1312
from frequenz.client.common.microgrid.electrical_components import (
1413
ElectricalComponentCategory,
@@ -29,8 +28,7 @@ def test_electrical_component_category_member_warns() -> None:
2928

3029
def test_electrical_component_category_to_proto_warns() -> None:
3130
"""Calling `electrical_component_category_to_proto` must warn."""
32-
with warnings.catch_warnings():
33-
warnings.simplefilter("ignore", DeprecationWarning)
31+
with ignoring_deprecations():
3432
member = ElectricalComponentCategory.BATTERY
3533
with pytest.deprecated_call():
3634
_ = electrical_component_category_to_proto(member)
@@ -46,8 +44,7 @@ def test_electrical_component_category_from_proto_warns() -> None:
4644

4745
def test_class_to_proto_does_not_warn() -> None:
4846
"""The non-deprecated `electrical_component_class_to_proto` must NOT warn."""
49-
with warnings.catch_warnings():
50-
warnings.simplefilter("error", DeprecationWarning)
47+
with asserting_no_deprecations():
5148
result = electrical_component_class_to_proto(LiIonBattery)
5249
assert result == (
5350
electrical_components_pb2.ELECTRICAL_COMPONENT_CATEGORY_BATTERY,

0 commit comments

Comments
 (0)