-
Notifications
You must be signed in to change notification settings - Fork 0
Add tests for Jinja expression reference validation #397
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
zhongkaifu
wants to merge
4
commits into
main
Choose a base branch
from
codex/validate-jinja-field-references-in-workflow
base: main
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
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Author: Zhongkai Fu ([email protected]) | ||
| # License: BSD 3-Clause License | ||
|
|
||
| from pathlib import Path | ||
| import sys | ||
|
|
||
| ROOT_DIR = Path(__file__).resolve().parents[1] | ||
| if str(ROOT_DIR) not in sys.path: | ||
| sys.path.insert(0, str(ROOT_DIR)) | ||
|
|
||
| from velvetflow.verification.jinja_validation import ( | ||
| normalize_condition_params_to_jinja, | ||
| normalize_params_to_jinja, | ||
| ) | ||
|
|
||
|
|
||
| BASE_WORKFLOW = { | ||
| "workflow_name": "normalize_expr", | ||
| "description": "", | ||
| "nodes": [ | ||
| { | ||
| "id": "fetch", | ||
| "type": "action", | ||
| "action_id": "hr.get_today_temperatures.v1", | ||
| "params": {"date": "2024-01-01"}, | ||
| }, | ||
| { | ||
| "id": "record", | ||
| "type": "action", | ||
| "action_id": "hr.record_health_event.v1", | ||
| "params": { | ||
| "event_type": "temperature", | ||
| "date": "result_of.fetch.date", | ||
| "abnormal_count": "result_of.fetch.data | length > 0", | ||
| }, | ||
| }, | ||
| { | ||
| "id": "check", | ||
| "type": "condition", | ||
| "params": { | ||
| "expression": "result_of.fetch.data | length > 0", | ||
| }, | ||
| "true_to_node": None, | ||
| "false_to_node": None, | ||
| }, | ||
| ], | ||
| "edges": [ | ||
| {"from": "fetch", "to": "record"}, | ||
| {"from": "record", "to": "check"}, | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def test_normalize_params_wraps_unwrapped_jinja_expression(): | ||
| normalized, summary, errors = normalize_params_to_jinja(BASE_WORKFLOW) | ||
|
|
||
| assert errors == [] | ||
| assert summary.get("applied") is True | ||
|
|
||
| record_params = normalized["nodes"][1]["params"] | ||
| assert record_params["date"] == "{{ result_of.fetch.date }}" | ||
| assert record_params["abnormal_count"] == "{{ result_of.fetch.data | length > 0 }}" | ||
|
|
||
|
|
||
| def test_normalize_condition_params_wraps_unwrapped_expression(): | ||
| normalized, summary, errors = normalize_condition_params_to_jinja(BASE_WORKFLOW) | ||
|
|
||
| assert errors == [] | ||
| assert summary.get("applied") is True | ||
|
|
||
| condition_params = normalized["nodes"][2]["params"] | ||
| assert condition_params["expression"] == "{{ result_of.fetch.data | length > 0 }}" |
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,63 @@ | ||
| # Author: Zhongkai Fu ([email protected]) | ||
| # License: BSD 3-Clause License | ||
|
|
||
| from pathlib import Path | ||
| import sys | ||
|
|
||
| ROOT_DIR = Path(__file__).resolve().parents[1] | ||
| if str(ROOT_DIR) not in sys.path: | ||
| sys.path.insert(0, str(ROOT_DIR)) | ||
|
|
||
| from velvetflow.action_registry import BUSINESS_ACTIONS | ||
| from velvetflow.verification.validation import validate_completed_workflow | ||
|
|
||
| ACTION_REGISTRY = BUSINESS_ACTIONS | ||
|
|
||
|
|
||
| def _workflow_with_expression_param(expression: str): | ||
| return { | ||
| "workflow_name": "jinja_expression_param", | ||
| "description": "", | ||
| "nodes": [ | ||
| { | ||
| "id": "fetch_temperatures", | ||
| "type": "action", | ||
| "action_id": "hr.get_today_temperatures.v1", | ||
| "params": {"date": "2024-01-01"}, | ||
| }, | ||
| { | ||
| "id": "record_event", | ||
| "type": "action", | ||
| "action_id": "hr.record_health_event.v1", | ||
| "params": { | ||
| "event_type": "temperature", | ||
| "date": "{{ result_of.fetch_temperatures.date }}", | ||
| "abnormal_count": expression, | ||
| }, | ||
| }, | ||
| ], | ||
| "edges": [ | ||
| {"from": "fetch_temperatures", "to": "record_event"}, | ||
| ], | ||
| } | ||
|
|
||
|
|
||
| def test_template_expression_with_missing_field_is_reported(): | ||
| workflow = _workflow_with_expression_param( | ||
| "{{ (result_of.fetch_temperatures.data | length) > 0 and result_of.fetch_temperatures.data[0].missing }}" | ||
| ) | ||
| errors = validate_completed_workflow(workflow, action_registry=ACTION_REGISTRY) | ||
|
|
||
| assert any( | ||
| err.code == "SCHEMA_MISMATCH" and err.field == "abnormal_count" | ||
| for err in errors | ||
| ) | ||
|
|
||
|
|
||
| def test_template_expression_with_valid_field_passes(): | ||
| workflow = _workflow_with_expression_param( | ||
| "{{ (result_of.fetch_temperatures.data | length) > 0 and result_of.fetch_temperatures.data[0].temperature }}" | ||
| ) | ||
| errors = validate_completed_workflow(workflow, action_registry=ACTION_REGISTRY) | ||
|
|
||
| assert errors == [] |
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
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
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.
This conversion treats any string index (e.g.
foo['a.b']orfoo['0']) as dotted access, which loses the original bracket semantics. When a schema has property names with dots or numeric strings,extract_jinja_reference_pathswill emitfoo.a.borfoo.0, andparse_field_pathwill split or coerce those into nested fields/array indices. That can raiseSCHEMA_MISMATCHfor templates that are actually valid in Jinja. This regression shows up only when bracket string keys are used in expressions, but in that case the new validation will flag false errors.Useful? React with 👍 / 👎.