-
Notifications
You must be signed in to change notification settings - Fork 21
Coalesce function unsubscribes only after receiving 3 samples #1374
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
Open
simonvoelcker
wants to merge
3
commits into
frequenz-floss:v1.x.x
Choose a base branch
from
simonvoelcker:coalesce_cautious_unsubscribe
base: v1.x.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| # License: MIT | ||
| # Copyright © 2025 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """Tests for the Formula implementation.""" | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
| from frequenz.quantities import Quantity | ||
|
|
||
| from frequenz.sdk.timeseries.formulas._exceptions import FormulaSyntaxError | ||
| from frequenz.sdk.timeseries.formulas._parser import parse | ||
|
|
||
|
|
||
| class TestFormulaValidation: | ||
| """Tests for Formula validation.""" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("formula_str", "parsed_formula_str"), | ||
| [ | ||
| ("#1", "[f](#1)"), | ||
| ("-(1+#1)", "[f](0.0 - (1.0 + #1))"), | ||
| ("1*(2+3)", "[f](1.0 * (2.0 + 3.0))"), | ||
| ], | ||
| ) | ||
| async def test_parser_validation( | ||
| self, | ||
| formula_str: str, | ||
| parsed_formula_str: str, | ||
| ) -> None: | ||
| """Test formula parser validation.""" | ||
| try: | ||
| formula = parse( | ||
| name="f", | ||
| formula=formula_str, | ||
| create_method=Quantity, | ||
| telemetry_fetcher=Mock(), | ||
| ) | ||
| assert str(formula) == parsed_formula_str | ||
| except FormulaSyntaxError: | ||
| assert False, "Parser should not raise an error for this formula" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("formula_str", "expected_error_line"), | ||
| [ | ||
| ( | ||
| "1++", | ||
| " ^ Expected expression", | ||
| ), | ||
| ( | ||
| "1**", | ||
| " ^ Expected expression", | ||
| ), | ||
| ( | ||
| "--1", | ||
| " ^ Expected expression", | ||
| ), | ||
| ( | ||
| "(", | ||
| " ^ Expected expression", | ||
| ), | ||
| ( | ||
| "(1", | ||
| "^ Unmatched parenthesis", | ||
| ), | ||
| ( | ||
| "max", | ||
| " ^ Expected '(' after function name", | ||
| ), | ||
| ( | ||
| "max()", | ||
| " ^ Expected argument", | ||
| ), | ||
| ( | ||
| "max(1(", | ||
| " ^ Expected ',' or ')'", | ||
| ), | ||
| ( | ||
| "max(1", | ||
| " ^ Unmatched parenthesis", | ||
| ), | ||
| ( | ||
| "foo", | ||
| "^^^ Unknown function name", | ||
| ), | ||
| ( | ||
| "foo(1)", | ||
| "^^^ Unknown function name", | ||
| ), | ||
| ( | ||
| "max(1,,2)", | ||
| " ^ Expected argument", | ||
| ), | ||
| ( | ||
| "1 2", | ||
| " ^ Unexpected token", | ||
| ), | ||
| ( | ||
| "1, 2", | ||
| " ^ Unexpected token", | ||
| ), | ||
| ( | ||
| "max(1, 2,)", | ||
| " ^ Expected argument", | ||
| ), | ||
| ( | ||
| "max(1, 2))", | ||
| " ^ Unexpected token", | ||
| ), | ||
| ( | ||
| "max(1, 2),", | ||
| " ^ Unexpected token", | ||
| ), | ||
| ], | ||
| ) | ||
| async def test_parser_validation_errors( | ||
| self, formula_str: str, expected_error_line: str | ||
| ) -> None: | ||
| """Test formula parser validation.""" | ||
| with pytest.raises(FormulaSyntaxError) as error: | ||
| _ = parse( | ||
| name="f", | ||
| formula=formula_str, | ||
| create_method=Quantity, | ||
| telemetry_fetcher=Mock(), | ||
| ) | ||
|
|
||
| assert str(error.value) == ( | ||
| "Formula syntax error:\n" | ||
| f" Formula: {formula_str}\n" | ||
| f" {expected_error_line}" | ||
| ) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| ("formula_str", "expected_error"), | ||
| [ | ||
| # Long formula with error near start -> Ellipsize end | ||
| ( | ||
| "max(coalesce(#1001, %1002, 0), coalesce(#1003, #1004, 0), coalesce(#1005, #1006, 0), coalesce(#1007, #1008, 0))", # noqa: E501 | ||
| "Formula syntax error:\n" | ||
| " Formula: max(coalesce(#1001, %1002, 0), coalesce(#1003, #1004, 0), coalesc ...\n" | ||
| " ^ Unexpected character", | ||
| ), | ||
| # Long formula with error near the end -> Ellipsize start | ||
| ( | ||
| "max(coalesce(#1001, #1002, 0), coalesce(#1003, #1004, 0), coalesce(#1005, #1006, 0), coalesce(#10.07, #1008, 0))", # noqa: E501 | ||
| "Formula syntax error:\n" | ||
| " Formula: ... 0), coalesce(#1005, #1006, 0), coalesce(#10.07, #1008, 0))\n" | ||
| " ^ Unexpected character", | ||
| ), | ||
| # Very long formula with error in the middle -> Ellipsize both sides | ||
| ( | ||
| "max(coalesce(#1001, #1002, 0), coalesce(#1003, #1004, 0), coalesce(#1005, #1006, 0), coalesce(#1007, #1008, 0)) :) " # noqa: E501 | ||
| "min(coalesce(#2001, #2002, 0), coalesce(#2003, #2004, 0), coalesce(#2005, #2006, 0), coalesce(#2007, #2008, 0))", # noqa: E501 | ||
| "Formula syntax error:\n" | ||
| " Formula: ... 005, #1006, 0), coalesce(#1007, #1008, 0)) :) min(coalesce(#2 ...\n" | ||
| " ^ Unexpected character", | ||
| ), | ||
| ], | ||
| ) | ||
| async def test_parser_validation_errors_in_long_formulas( | ||
| self, formula_str: str, expected_error: str | ||
| ) -> None: | ||
| """Test formula parser validation for long formulas.""" | ||
| with pytest.raises(FormulaSyntaxError) as error: | ||
| _ = parse( | ||
| name="f", | ||
| formula=formula_str, | ||
| create_method=Quantity, | ||
| telemetry_fetcher=Mock(), | ||
| ) | ||
|
|
||
| assert str(error.value) == expected_error | ||
| assert all(len(line) <= 80 for line in str(error.value).splitlines()) | ||
|
|
||
| async def test_empty_formula(self) -> None: | ||
| """Test formula parser validation.""" | ||
| with pytest.raises(FormulaSyntaxError) as error: | ||
| _ = parse( | ||
| name="f", | ||
| formula="", | ||
| create_method=Quantity, | ||
| telemetry_fetcher=Mock(), | ||
| ) | ||
|
|
||
| assert str(error.value) == "Empty formula" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2026