Skip to content

Commit

Permalink
feat: add min macros (#203)
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Inzhyyants <[email protected]>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
artem1205 and coderabbitai[bot] authored Jan 7, 2025
1 parent 884897e commit 3ee710d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions airbyte_cdk/sources/declarative/interpolation/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ def max(*args: typing.Any) -> typing.Any:
return builtins.max(*args)


def min(*args: typing.Any) -> typing.Any:
"""
Returns smallest object of an iterable, or two or more arguments.
min(iterable, *[, default=obj, key=func]) -> value
min(arg1, arg2, *args, *[, key=func]) -> value
Usage:
`"{{ min(2,3) }}"
With a single iterable argument, return its smallest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the smallest argument.
:param args: args to compare
:return: smallest argument
"""
return builtins.min(*args)


def day_delta(num_days: int, format: str = "%Y-%m-%dT%H:%M:%S.%f%z") -> str:
"""
Returns datetime of now() + num_days
Expand Down Expand Up @@ -147,6 +167,7 @@ def format_datetime(
today_utc,
timestamp,
max,
min,
day_delta,
duration,
format_datetime,
Expand Down
3 changes: 3 additions & 0 deletions unit_tests/sources/declarative/interpolation/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def test_to_string(test_name, input_value, expected_output):
id="test_timestamp_from_rfc3339",
),
pytest.param("{{ max(1,2) }}", 2, id="test_max"),
pytest.param("{{ min(1,2) }}", 1, id="test_min"),
],
)
def test_macros(s, expected_value):
Expand Down Expand Up @@ -291,6 +292,8 @@ def test_undeclared_variables(template_string, expected_error, expected_value):
),
pytest.param("{{ max(2, 3) }}", 3, id="test_max_with_arguments"),
pytest.param("{{ max([2, 3]) }}", 3, id="test_max_with_list"),
pytest.param("{{ min(2, 3) }}", 2, id="test_min_with_arguments"),
pytest.param("{{ min([2, 3]) }}", 2, id="test_min_with_list"),
pytest.param("{{ day_delta(1) }}", "2021-09-02T00:00:00.000000+0000", id="test_day_delta"),
pytest.param(
"{{ day_delta(-1) }}", "2021-08-31T00:00:00.000000+0000", id="test_day_delta_negative"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
("test_now_utc", "now_utc", True),
("test_today_utc", "today_utc", True),
("test_max", "max", True),
("test_min", "min", True),
("test_day_delta", "day_delta", True),
("test_format_datetime", "format_datetime", True),
("test_duration", "duration", True),
Expand Down

0 comments on commit 3ee710d

Please sign in to comment.