|
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,48 @@ class Fz2( |
45 | 48 | """Frequency quantity with broad exponent unit map.""" |
46 | 49 |
|
47 | 50 |
|
| 51 | +_CtorType = Callable[[float], Quantity] |
| 52 | + |
| 53 | +# Thi is the current number of subclasses. This probably will get outdated, but it will |
| 54 | +# provide at least some safety against something going really wrong and end up testing |
| 55 | +# an empty list. With this we should at least make sure we are not testint less classes |
| 56 | +# than before. We don't get the actual number using len(_QUANTITY_SUBCLASSES) because it |
| 57 | +# would defeat the purpose of the test. |
| 58 | +_SANITFY_NUM_CLASSES = 7 |
| 59 | + |
| 60 | +_QUANTITY_SUBCLASSES = [ |
| 61 | + cls |
| 62 | + for _, cls in inspect.getmembers( |
| 63 | + _quantities, |
| 64 | + lambda m: inspect.isclass(m) and issubclass(m, Quantity) and m is not Quantity, |
| 65 | + ) |
| 66 | +] |
| 67 | + |
| 68 | +# A very basic sainty check that we are messing up the introspection |
| 69 | +assert len(_QUANTITY_SUBCLASSES) >= _SANITFY_NUM_CLASSES |
| 70 | + |
| 71 | +_QUANTITY_BASE_UNIT_STRINGS = [ |
| 72 | + cls._new(0).base_unit # pylint: disable=protected-access |
| 73 | + for cls in _QUANTITY_SUBCLASSES |
| 74 | +] |
| 75 | +for unit in _QUANTITY_BASE_UNIT_STRINGS: |
| 76 | + assert unit is not None |
| 77 | + |
| 78 | +_QUANTITY_CTORS = [ |
| 79 | + method |
| 80 | + for cls in _QUANTITY_SUBCLASSES |
| 81 | + for _, method in inspect.getmembers( |
| 82 | + cls, |
| 83 | + lambda m: inspect.ismethod(m) |
| 84 | + and m.__name__.startswith("from_") |
| 85 | + and m.__name__ != ("from_string"), |
| 86 | + ) |
| 87 | +] |
| 88 | +# A very basic sainty check that we are messing up the introspection. There are actually |
| 89 | +# many more constructors than classes, but this still works as a very basic check. |
| 90 | +assert len(_QUANTITY_CTORS) >= _SANITFY_NUM_CLASSES |
| 91 | + |
| 92 | + |
48 | 93 | def test_zero() -> None: |
49 | 94 | """Test the zero value for quantity.""" |
50 | 95 | assert Quantity(0.0) == Quantity.zero() |
@@ -101,6 +146,31 @@ def test_zero() -> None: |
101 | 146 | assert Percentage.zero() is Percentage.zero() # It is a "singleton" |
102 | 147 |
|
103 | 148 |
|
| 149 | +@pytest.mark.parametrize("quantity_ctor", _QUANTITY_CTORS) |
| 150 | +def test_base_value_from_ctor_is_float(quantity_ctor: _CtorType) -> None: |
| 151 | + """Test that the base value always is a float.""" |
| 152 | + quantity = quantity_ctor(1) |
| 153 | + assert isinstance(quantity.base_value, float) |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.parametrize("quantity_type", _QUANTITY_SUBCLASSES + [Quantity]) |
| 157 | +def test_base_value_from_zero_is_float(quantity_type: type[Quantity]) -> None: |
| 158 | + """Test that the base value always is a float.""" |
| 159 | + quantity = quantity_type.zero() |
| 160 | + assert isinstance(quantity.base_value, float) |
| 161 | + |
| 162 | + |
| 163 | +@pytest.mark.parametrize( |
| 164 | + "quantity_type, unit", zip(_QUANTITY_SUBCLASSES, _QUANTITY_BASE_UNIT_STRINGS) |
| 165 | +) |
| 166 | +def test_base_value_from_string_is_float( |
| 167 | + quantity_type: type[Quantity], unit: str |
| 168 | +) -> None: |
| 169 | + """Test that the base value always is a float.""" |
| 170 | + quantity = quantity_type.from_string(f"1 {unit}") |
| 171 | + assert isinstance(quantity.base_value, float) |
| 172 | + |
| 173 | + |
104 | 174 | def test_string_representation() -> None: |
105 | 175 | """Test the string representation of the quantities.""" |
106 | 176 | assert str(Quantity(1.024445, exponent=0)) == "1.024" |
|
0 commit comments