|
3 | 3 |
|
4 | 4 | """Tests for the Sample class and related classes.""" |
5 | 5 |
|
| 6 | +from dataclasses import dataclass, field |
| 7 | + |
| 8 | +import pytest |
| 9 | +from frequenz.api.common.v1.metrics import metric_sample_pb2 |
| 10 | + |
6 | 11 | from frequenz.client.microgrid.metrics import AggregatedMetricValue, AggregationMethod |
| 12 | +from frequenz.client.microgrid.metrics._sample_proto import ( |
| 13 | + aggregated_metric_sample_from_proto, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@dataclass(frozen=True, kw_only=True) |
| 18 | +class _AggregatedValueTestCase: |
| 19 | + """Test case for AggregatedMetricValue protobuf conversion.""" |
| 20 | + |
| 21 | + name: str |
| 22 | + """The description of the test case.""" |
| 23 | + |
| 24 | + avg_value: float |
| 25 | + """The average value to set.""" |
| 26 | + |
| 27 | + has_min: bool = True |
| 28 | + """Whether to include min value.""" |
| 29 | + |
| 30 | + has_max: bool = True |
| 31 | + """Whether to include max value.""" |
| 32 | + |
| 33 | + min_value: float | None = None |
| 34 | + """The minimum value to set.""" |
| 35 | + |
| 36 | + max_value: float | None = None |
| 37 | + """The maximum value to set.""" |
| 38 | + |
| 39 | + raw_values: list[float] = field(default_factory=list) |
| 40 | + """The raw values to include.""" |
7 | 41 |
|
8 | 42 |
|
9 | 43 | def test_aggregation_method_values() -> None: |
@@ -40,3 +74,54 @@ def test_aggregated_metric_value() -> None: |
40 | 74 | assert value.max is None |
41 | 75 | assert not value.raw_values |
42 | 76 | assert str(value) == "avg:5.0" |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "case", |
| 81 | + [ |
| 82 | + _AggregatedValueTestCase( |
| 83 | + name="full", |
| 84 | + avg_value=5.0, |
| 85 | + min_value=1.0, |
| 86 | + max_value=10.0, |
| 87 | + raw_values=[1.0, 5.0, 10.0], |
| 88 | + ), |
| 89 | + _AggregatedValueTestCase( |
| 90 | + name="minimal", |
| 91 | + avg_value=5.0, |
| 92 | + has_min=False, |
| 93 | + has_max=False, |
| 94 | + ), |
| 95 | + _AggregatedValueTestCase( |
| 96 | + name="only_min", |
| 97 | + avg_value=5.0, |
| 98 | + has_max=False, |
| 99 | + min_value=1.0, |
| 100 | + ), |
| 101 | + _AggregatedValueTestCase( |
| 102 | + name="only_max", |
| 103 | + avg_value=5.0, |
| 104 | + has_min=False, |
| 105 | + max_value=10.0, |
| 106 | + ), |
| 107 | + ], |
| 108 | + ids=lambda case: case.name, |
| 109 | +) |
| 110 | +def test_aggregated_metric_value_from_proto(case: _AggregatedValueTestCase) -> None: |
| 111 | + """Test conversion from protobuf message to AggregatedMetricValue.""" |
| 112 | + proto = metric_sample_pb2.AggregatedMetricValue( |
| 113 | + avg_value=case.avg_value, |
| 114 | + ) |
| 115 | + if case.has_min and case.min_value is not None: |
| 116 | + proto.min_value = case.min_value |
| 117 | + if case.has_max and case.max_value is not None: |
| 118 | + proto.max_value = case.max_value |
| 119 | + if case.raw_values: |
| 120 | + proto.raw_values.extend(case.raw_values) |
| 121 | + |
| 122 | + value = aggregated_metric_sample_from_proto(proto) |
| 123 | + |
| 124 | + assert value.avg == case.avg_value |
| 125 | + assert value.min == (case.min_value if case.has_min else None) |
| 126 | + assert value.max == (case.max_value if case.has_max else None) |
| 127 | + assert list(value.raw_values) == case.raw_values |
0 commit comments