-
Couldn't load subscription status.
- Fork 20
Add contains function to Bounds and SystemBounds #962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # License: MIT | ||
| # Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Tests for timeseries base types.""" | ||
|
|
||
|
|
||
| from datetime import datetime | ||
|
|
||
| from frequenz.sdk.timeseries._base_types import Bounds, SystemBounds | ||
| from frequenz.sdk.timeseries._quantities import Power | ||
|
|
||
|
|
||
| def test_bounds_contains() -> None: | ||
| """Tests with complete bounds.""" | ||
| bounds = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) | ||
| assert Power.from_watts(50) in bounds # within | ||
| assert Power.from_watts(10) in bounds # at lower | ||
| assert Power.from_watts(100) in bounds # at upper | ||
| assert Power.from_watts(9) not in bounds # below lower | ||
| assert Power.from_watts(101) not in bounds # above upper | ||
|
|
||
|
|
||
| def test_bounds_contains_no_lower() -> None: | ||
| """Tests without lower bound.""" | ||
| bounds_no_lower = Bounds(lower=None, upper=Power.from_watts(100)) | ||
| assert Power.from_watts(50) in bounds_no_lower # within upper | ||
| assert Power.from_watts(100) in bounds_no_lower # at upper | ||
| assert Power.from_watts(101) not in bounds_no_lower # above upper | ||
|
|
||
|
|
||
| def test_bounds_contains_no_upper() -> None: | ||
| """Tests without upper bound.""" | ||
| bounds_no_upper = Bounds(lower=Power.from_watts(10), upper=None) | ||
| assert Power.from_watts(50) in bounds_no_upper # within lower | ||
| assert Power.from_watts(10) in bounds_no_upper # at lower | ||
| assert Power.from_watts(9) not in bounds_no_upper # below lower | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add some tests for corner cases, like using plain I think also adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reopened this because the suggested changes were not made. Even if the extra validation is not added, at least having tests for this would be good:
If you think this should not be done, it would be good to give a reason. |
||
| def test_bounds_contains_no_bounds() -> None: | ||
| """Tests with no bounds.""" | ||
| bounds_no_bounds: Bounds[Power | None] = Bounds(lower=None, upper=None) | ||
| assert Power.from_watts(50) in bounds_no_bounds # any value within bounds | ||
|
|
||
|
|
||
| INCLUSION_BOUND = Bounds(lower=Power.from_watts(10), upper=Power.from_watts(100)) | ||
| EXCLUSION_BOUND = Bounds(lower=Power.from_watts(40), upper=Power.from_watts(50)) | ||
|
|
||
|
|
||
| def test_system_bounds_contains() -> None: | ||
| """Tests with complete system bounds.""" | ||
| system_bounds = SystemBounds( | ||
| timestamp=datetime.now(), | ||
| inclusion_bounds=INCLUSION_BOUND, | ||
| exclusion_bounds=EXCLUSION_BOUND, | ||
| ) | ||
|
|
||
| assert Power.from_watts(30) in system_bounds # within inclusion, not in exclusion | ||
| assert Power.from_watts(45) not in system_bounds # within inclusion and exclusion | ||
| assert Power.from_watts(110) not in system_bounds # outside inclusion | ||
|
|
||
|
|
||
| def test_system_bounds_contains_no_exclusion() -> None: | ||
| """Tests with no exclusion bounds.""" | ||
| system_bounds_no_exclusion = SystemBounds( | ||
| timestamp=datetime.now(), | ||
| inclusion_bounds=INCLUSION_BOUND, | ||
| exclusion_bounds=None, | ||
| ) | ||
| assert Power.from_watts(30) in system_bounds_no_exclusion # within inclusion | ||
| assert Power.from_watts(110) not in system_bounds_no_exclusion # outside inclusion | ||
|
|
||
|
|
||
| def test_system_bounds_contains_no_inclusion() -> None: | ||
| """Tests with no inclusion bounds.""" | ||
| system_bounds_no_inclusion = SystemBounds( | ||
| timestamp=datetime.now(), | ||
| inclusion_bounds=None, | ||
| exclusion_bounds=EXCLUSION_BOUND, | ||
| ) | ||
| assert Power.from_watts(30) not in system_bounds_no_inclusion # outside exclusion | ||
| assert Power.from_watts(45) not in system_bounds_no_inclusion # within exclusion | ||
|
|
||
|
|
||
| def test_system_bounds_contains_no_bounds() -> None: | ||
| """Tests with no bounds.""" | ||
| system_bounds_no_bounds = SystemBounds( | ||
| timestamp=datetime.now(), | ||
| inclusion_bounds=None, | ||
| exclusion_bounds=None, | ||
| ) | ||
| assert Power.from_watts(30) not in system_bounds_no_bounds # any value outside | ||
| assert Power.from_watts(110) not in system_bounds_no_bounds # any value outside | ||
Uh oh!
There was an error while loading. Please reload this page.