Skip to content

Commit

Permalink
feat(engine): Add FN.range
Browse files Browse the repository at this point in the history
  • Loading branch information
daryllimyt committed Dec 17, 2024
1 parent 057bee9 commit 4c8d3ba
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/unit/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
create_days,
create_hours,
create_minutes,
create_range,
create_seconds,
create_weeks,
days_between,
Expand Down Expand Up @@ -869,3 +870,33 @@ def test_intersect(
result = intersect(items, collection, python_lambda)
# Sort the results to ensure consistent comparison
assert sorted(result) == sorted(expected)


@pytest.mark.parametrize(
"start,end,step,expected",
[
(0, 5, 1, [0, 1, 2, 3, 4]), # Basic range
(1, 10, 2, [1, 3, 5, 7, 9]), # Range with step
(5, 0, -1, [5, 4, 3, 2, 1]), # Descending range
(0, 0, 1, []), # Empty range
(-5, 5, 2, [-5, -3, -1, 1, 3]), # Range with negative start
(10, 5, -2, [10, 8, 6]), # Descending range with step
],
)
def test_create_range(start: int, end: int, step: int, expected: list[int]) -> None:
"""Test create_range function with various inputs.
Tests:
- Basic ascending range
- Range with custom step size
- Descending range
- Empty range
- Range with negative numbers
- Descending range with custom step
"""
result = create_range(start, end, step)
assert list(result) == expected

# Test invalid step
with pytest.raises(ValueError):
create_range(0, 5, 0) # Step cannot be 0
6 changes: 6 additions & 0 deletions tracecat/expressions/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ def iter_product(*iterables: Sequence[Any]) -> list[tuple[Any, ...]]:
return list(itertools.product(*iterables))


def create_range(start: int, end: int, step: int = 1) -> range:
"""Create a range of integers from start to end (exclusive), with a step size."""
return range(start, end, step)


# Dictionary functions


Expand Down Expand Up @@ -835,6 +840,7 @@ def eval_jsonpath(
# Iteration
"zip": zip_iterables,
"iter_product": iter_product,
"range": create_range,
# Generators
"uuid4": generate_uuid,
# Extract JSON keys and values
Expand Down

0 comments on commit 4c8d3ba

Please sign in to comment.