|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for timeseries base types.""" |
| 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 | + """Tests with complete bounds.""" |
| 15 | + bounds = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) |
| 16 | + assert Power.from_watts(50) in bounds # within |
| 17 | + assert Power.from_watts(10) in bounds # at lower |
| 18 | + assert Power.from_watts(100) in bounds # at upper |
| 19 | + assert Power.from_watts(9) not in bounds # below lower |
| 20 | + assert Power.from_watts(101) not in bounds # above upper |
| 21 | + |
| 22 | + |
| 23 | +def test_bounds_contains_no_lower() -> None: |
| 24 | + """Tests without lower bound.""" |
| 25 | + bounds_no_lower = Bounds(lower=None, upper=Power.from_watts(100)) |
| 26 | + assert Power.from_watts(50) in bounds_no_lower # within upper |
| 27 | + assert Power.from_watts(100) in bounds_no_lower # at upper |
| 28 | + assert Power.from_watts(101) not in bounds_no_lower # above upper |
| 29 | + |
| 30 | + |
| 31 | +def test_bounds_contains_no_upper() -> None: |
| 32 | + """Tests without upper bound.""" |
| 33 | + bounds_no_upper = Bounds(lower=Power.from_watts(10), upper=None) |
| 34 | + assert Power.from_watts(50) in bounds_no_upper # within lower |
| 35 | + assert Power.from_watts(10) in bounds_no_upper # at lower |
| 36 | + assert Power.from_watts(9) not in bounds_no_upper # below lower |
| 37 | + |
| 38 | + |
| 39 | +def test_bounds_contains_no_bounds() -> None: |
| 40 | + """Tests with no bounds.""" |
| 41 | + bounds_no_bounds: Bounds[Power | None] = Bounds(lower=None, upper=None) |
| 42 | + assert Power.from_watts(50) in bounds_no_bounds # any value within bounds |
| 43 | + |
| 44 | + |
| 45 | +INCLUSION_BOUND = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) |
| 46 | +EXCLUSION_BOUND = Bounds(lower=Power.from_watts(40), upper=Power.from_watts(50)) |
| 47 | + |
| 48 | + |
| 49 | +def test_system_bounds_contains() -> None: |
| 50 | + """Tests with complete system bounds.""" |
| 51 | + system_bounds = SystemBounds( |
| 52 | + timestamp=datetime.now(), |
| 53 | + inclusion_bounds=INCLUSION_BOUND, |
| 54 | + exclusion_bounds=EXCLUSION_BOUND, |
| 55 | + ) |
| 56 | + |
| 57 | + assert Power.from_watts(30) in system_bounds # within inclusion, not in exclusion |
| 58 | + assert Power.from_watts(45) not in system_bounds # within inclusion and exclusion |
| 59 | + assert Power.from_watts(110) not in system_bounds # outside inclusion |
| 60 | + |
| 61 | + |
| 62 | +def test_system_bounds_contains_no_exclusion() -> None: |
| 63 | + """Tests with no exclusion bounds.""" |
| 64 | + system_bounds_no_exclusion = SystemBounds( |
| 65 | + timestamp=datetime.now(), |
| 66 | + inclusion_bounds=INCLUSION_BOUND, |
| 67 | + exclusion_bounds=None, |
| 68 | + ) |
| 69 | + assert Power.from_watts(30) in system_bounds_no_exclusion # within inclusion |
| 70 | + assert Power.from_watts(110) not in system_bounds_no_exclusion # outside inclusion |
| 71 | + |
| 72 | + |
| 73 | +def test_system_bounds_contains_no_inclusion() -> None: |
| 74 | + """Tests with no inclusion bounds.""" |
| 75 | + system_bounds_no_inclusion = SystemBounds( |
| 76 | + timestamp=datetime.now(), |
| 77 | + inclusion_bounds=None, |
| 78 | + exclusion_bounds=EXCLUSION_BOUND, |
| 79 | + ) |
| 80 | + assert Power.from_watts(30) not in system_bounds_no_inclusion # outside exclusion |
| 81 | + assert Power.from_watts(45) not in system_bounds_no_inclusion # within exclusion |
| 82 | + |
| 83 | + |
| 84 | +def test_system_bounds_contains_no_bounds() -> None: |
| 85 | + """Tests with no bounds.""" |
| 86 | + system_bounds_no_bounds = SystemBounds( |
| 87 | + timestamp=datetime.now(), |
| 88 | + inclusion_bounds=None, |
| 89 | + exclusion_bounds=None, |
| 90 | + ) |
| 91 | + assert Power.from_watts(30) not in system_bounds_no_bounds # any value outside |
| 92 | + assert Power.from_watts(110) not in system_bounds_no_bounds # any value outside |
0 commit comments