|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for battery pool.""" |
| 5 | + |
| 6 | + |
| 7 | +from datetime import datetime |
| 8 | + |
| 9 | +from frequenz.sdk.timeseries._base_types import Bounds, SystemBounds |
| 10 | +from frequenz.sdk.timeseries._quantities import Power |
| 11 | + |
| 12 | + |
| 13 | +def test_bounds_contains() -> None: |
| 14 | + """ |
| 15 | + Test the `__contains__` method of the `Bounds` class. |
| 16 | +
|
| 17 | + This method checks if a value is within the defined bounds. |
| 18 | + """ |
| 19 | + bounds = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) |
| 20 | + |
| 21 | + assert Power.from_watts(50) in bounds # within bounds |
| 22 | + assert Power.from_watts(10) in bounds # at lower bound |
| 23 | + assert Power.from_watts(100) in bounds # at upper bound |
| 24 | + assert Power.from_watts(9) not in bounds # below lower bound |
| 25 | + assert Power.from_watts(101) not in bounds # above upper bound |
| 26 | + |
| 27 | + bounds_no_lower = Bounds(lower=None, upper=Power.from_watts(100)) |
| 28 | + assert Power.from_watts(50) in bounds_no_lower # within upper bound |
| 29 | + assert Power.from_watts(100) in bounds_no_lower # at upper bound |
| 30 | + assert Power.from_watts(101) not in bounds_no_lower # above upper bound |
| 31 | + |
| 32 | + bounds_no_upper = Bounds(lower=Power.from_watts(10), upper=None) |
| 33 | + assert Power.from_watts(50) in bounds_no_upper # within lower bound |
| 34 | + assert Power.from_watts(10) in bounds_no_upper # at lower bound |
| 35 | + assert Power.from_watts(9) not in bounds_no_upper # below lower bound |
| 36 | + |
| 37 | + # test succeeds, mypy 'Unsupported operand types for in ("Power" and "Bounds[None]")' |
| 38 | + # bounds_none = Bounds(lower=None, upper=None) |
| 39 | + # assert Power.from_watts(15) not in bounds_none |
| 40 | + |
| 41 | + |
| 42 | +def test_system_bounds_contains() -> None: |
| 43 | + """ |
| 44 | + Test the `__contains__` method of the `SystemBounds` class. |
| 45 | +
|
| 46 | + Checks if value is within inclusion bounds and not exclusion bounds. |
| 47 | + """ |
| 48 | + inclusion_bounds = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) |
| 49 | + exclusion_bounds = Bounds(lower=Power.from_watts(40), upper=Power.from_watts(50)) |
| 50 | + system_bounds = SystemBounds( |
| 51 | + timestamp=datetime.now(), |
| 52 | + inclusion_bounds=inclusion_bounds, |
| 53 | + exclusion_bounds=exclusion_bounds, |
| 54 | + ) |
| 55 | + |
| 56 | + assert Power.from_watts(30) in system_bounds # within inclusion, not in exclusion |
| 57 | + assert ( |
| 58 | + Power.from_watts(45) not in system_bounds |
| 59 | + ) # within inclusion, also in exclusion |
| 60 | + assert Power.from_watts(110) not in system_bounds # outside inclusion bounds |
| 61 | + |
| 62 | + system_bounds_no_exclusion = SystemBounds( |
| 63 | + timestamp=datetime.now(), |
| 64 | + inclusion_bounds=inclusion_bounds, |
| 65 | + exclusion_bounds=None, |
| 66 | + ) |
| 67 | + assert Power.from_watts(30) in system_bounds_no_exclusion # within inclusion bounds |
| 68 | + assert ( |
| 69 | + Power.from_watts(110) not in system_bounds_no_exclusion |
| 70 | + ) # outside inclusion bounds |
| 71 | + |
| 72 | + system_bounds_no_inclusion = SystemBounds( |
| 73 | + timestamp=datetime.now(), |
| 74 | + inclusion_bounds=None, |
| 75 | + exclusion_bounds=exclusion_bounds, |
| 76 | + ) |
| 77 | + assert ( |
| 78 | + Power.from_watts(30) not in system_bounds_no_inclusion |
| 79 | + ) # not in inclusion bounds |
0 commit comments