|
3 | 3 |
|
4 | 4 | """Tests for quantity types."""
|
5 | 5 |
|
| 6 | +import inspect |
6 | 7 | from datetime import timedelta
|
| 8 | +from typing import Callable |
7 | 9 |
|
8 | 10 | import hypothesis
|
9 | 11 | import pytest
|
10 | 12 | from hypothesis import strategies as st
|
11 | 13 |
|
| 14 | +from frequenz.sdk.timeseries import _quantities |
12 | 15 | from frequenz.sdk.timeseries._quantities import (
|
13 | 16 | Current,
|
14 | 17 | Energy,
|
@@ -45,6 +48,38 @@ class Fz2(
|
45 | 48 | """Frequency quantity with broad exponent unit map."""
|
46 | 49 |
|
47 | 50 |
|
| 51 | +_CtorType = Callable[[float], Quantity] |
| 52 | + |
| 53 | +_QUANTITY_SUBCLASSES = [ |
| 54 | + cls |
| 55 | + for _, cls in inspect.getmembers( |
| 56 | + _quantities, |
| 57 | + lambda m: inspect.isclass(m) and issubclass(m, Quantity) and m is not Quantity, |
| 58 | + ) |
| 59 | +] |
| 60 | +# Make a simple sanity check, 7 is the current number of subclasses |
| 61 | +assert len(_QUANTITY_SUBCLASSES) >= 7 |
| 62 | + |
| 63 | +_QUANTITY_BASE_UNIT_STRINGS = [ |
| 64 | + cls._new(0).base_unit # pylint: disable=protected-access |
| 65 | + for cls in _QUANTITY_SUBCLASSES |
| 66 | +] |
| 67 | +for unit in _QUANTITY_BASE_UNIT_STRINGS: |
| 68 | + assert unit is not None |
| 69 | + |
| 70 | +_QUANTITY_CTORS = [ |
| 71 | + method |
| 72 | + for cls in _QUANTITY_SUBCLASSES |
| 73 | + for _, method in inspect.getmembers( |
| 74 | + cls, |
| 75 | + lambda m: inspect.ismethod(m) |
| 76 | + and m.__name__.startswith("from_") |
| 77 | + and m.__name__ != ("from_string"), |
| 78 | + ) |
| 79 | +] |
| 80 | +assert len(_QUANTITY_CTORS) >= 7 |
| 81 | + |
| 82 | + |
48 | 83 | def test_zero() -> None:
|
49 | 84 | """Test the zero value for quantity."""
|
50 | 85 | assert Quantity(0.0) == Quantity.zero()
|
@@ -101,6 +136,31 @@ def test_zero() -> None:
|
101 | 136 | assert Percentage.zero() is Percentage.zero() # It is a "singleton"
|
102 | 137 |
|
103 | 138 |
|
| 139 | +@pytest.mark.parametrize("quantity_ctor", _QUANTITY_CTORS) |
| 140 | +def test_base_value_from_ctor_is_float(quantity_ctor: _CtorType) -> None: |
| 141 | + """Test that the base value always is a float.""" |
| 142 | + quantity = quantity_ctor(1) |
| 143 | + assert isinstance(quantity.base_value, float) |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.parametrize("quantity_type", _QUANTITY_SUBCLASSES + [Quantity]) |
| 147 | +def test_base_value_from_zero_is_float(quantity_type: type[Quantity]) -> None: |
| 148 | + """Test that the base value always is a float.""" |
| 149 | + quantity = quantity_type.zero() |
| 150 | + assert isinstance(quantity.base_value, float) |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize( |
| 154 | + "quantity_type, unit", zip(_QUANTITY_SUBCLASSES, _QUANTITY_BASE_UNIT_STRINGS) |
| 155 | +) |
| 156 | +def test_base_value_from_string_is_float( |
| 157 | + quantity_type: type[Quantity], unit: str |
| 158 | +) -> None: |
| 159 | + """Test that the base value always is a float.""" |
| 160 | + quantity = quantity_type.from_string(f"1 {unit}") |
| 161 | + assert isinstance(quantity.base_value, float) |
| 162 | + |
| 163 | + |
104 | 164 | def test_string_representation() -> None:
|
105 | 165 | """Test the string representation of the quantities."""
|
106 | 166 | assert str(Quantity(1.024445, exponent=0)) == "1.024"
|
|
0 commit comments