Skip to content
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

Make json and yaml available in templates #28930

Merged
merged 4 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions airflow/macros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations

import json # noqa
import time # noqa
import uuid # noqa
from datetime import datetime, timedelta
Expand All @@ -26,6 +27,7 @@
import dateutil # noqa
from pendulum import DateTime

import airflow.utils.yaml as yaml # noqa
from airflow.utils.deprecation_tools import add_deprecated_classes

__deprecated_classes = {
Expand Down
31 changes: 31 additions & 0 deletions tests/macros/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,34 @@ def test_ds_format(ds, input_format, output_format, expected):
def test_datetime_diff_for_humans(dt, since, expected):
result = macros.datetime_diff_for_humans(dt, since)
assert result == expected


@pytest.mark.parametrize(
"input_value, expected",
[
('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}),
(
'{"field1": [ 1, 2, 3, 4, 5 ], "field2" : {"mini1" : 1, "mini2" : "2"}}',
{"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}},
),
],
)
def test_json_loads(input_value, expected):
result = macros.json.loads(input_value)
assert result == expected


@pytest.mark.parametrize(
"input_value, expected",
[
('{"field1":"value1", "field2":4, "field3":true}', {"field1": "value1", "field2": 4, "field3": True}),
("field1: value1\nfield2: value2", {"field1": "value1", "field2": "value2"}),
(
'field1: [ 1, 2, 3, 4, 5 ]\nfield2: {"mini1" : 1, "mini2" : "2"}',
{"field1": [1, 2, 3, 4, 5], "field2": {"mini1": 1, "mini2": "2"}},
),
],
)
def test_yaml_loads(input_value, expected):
result = macros.yaml.safe_load(input_value)
assert result == expected