Skip to content

Commit 78df1c0

Browse files
timsaucerclaude
andcommitted
test: fold lambda tests into pytest parameterization
Combine the eight higher-order function result tests into a single parametrized test_higher_order_function_results, and the two to_lambda rejection tests into test_to_lambda_rejects_invalid_arg. Each case keeps a readable id via pytest.param. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 26d6204 commit 78df1c0

1 file changed

Lines changed: 66 additions & 44 deletions

File tree

python/tests/test_lambda.py

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,57 @@ def _column(df, expr, name):
3131
return df.select(expr.alias(name)).collect_column(name).to_pylist()
3232

3333

34-
def test_array_transform_callable(df):
35-
expr = f.array_transform(col("a"), lambda v: v * 2)
36-
assert _column(df, expr, "d") == [[2, 4, 6], [8, 10]]
37-
38-
39-
def test_array_transform_explicit_lambda(df):
40-
transform = f.lambda_(["v"], f.lambda_var("v") * lit(2))
41-
expr = f.array_transform(col("a"), transform)
42-
assert _column(df, expr, "d") == [[2, 4, 6], [8, 10]]
43-
44-
45-
def test_array_transform_literal_body_is_coerced(df):
46-
expr = f.array_transform(col("a"), lambda v: 0)
47-
assert _column(df, expr, "z") == [[0, 0, 0], [0, 0]]
48-
49-
50-
def test_list_transform_alias(df):
51-
expr = f.list_transform(col("a"), lambda v: v + 1)
52-
assert _column(df, expr, "d") == [[2, 3, 4], [5, 6]]
53-
54-
55-
def test_array_any_match_callable(df):
56-
expr = f.array_any_match(col("a"), lambda v: v > 3)
57-
assert _column(df, expr, "m") == [False, True]
58-
59-
60-
def test_array_any_match_explicit_lambda(df):
61-
predicate = f.lambda_(["v"], f.lambda_var("v") > lit(2))
62-
expr = f.array_any_match(col("a"), predicate)
63-
assert _column(df, expr, "m") == [True, True]
64-
65-
66-
@pytest.mark.parametrize("alias", [f.any_match, f.list_any_match])
67-
def test_any_match_aliases(df, alias):
68-
expr = alias(col("a"), lambda v: v > 4)
69-
assert _column(df, expr, "m") == [False, True]
34+
@pytest.mark.parametrize(
35+
("build_expr", "expected"),
36+
[
37+
pytest.param(
38+
lambda: f.array_transform(col("a"), lambda v: v * 2),
39+
[[2, 4, 6], [8, 10]],
40+
id="array_transform_callable",
41+
),
42+
pytest.param(
43+
lambda: f.array_transform(
44+
col("a"), f.lambda_(["v"], f.lambda_var("v") * lit(2))
45+
),
46+
[[2, 4, 6], [8, 10]],
47+
id="array_transform_explicit_lambda",
48+
),
49+
pytest.param(
50+
lambda: f.array_transform(col("a"), lambda v: 0),
51+
[[0, 0, 0], [0, 0]],
52+
id="array_transform_literal_body_is_coerced",
53+
),
54+
pytest.param(
55+
lambda: f.list_transform(col("a"), lambda v: v + 1),
56+
[[2, 3, 4], [5, 6]],
57+
id="list_transform_alias",
58+
),
59+
pytest.param(
60+
lambda: f.array_any_match(col("a"), lambda v: v > 3),
61+
[False, True],
62+
id="array_any_match_callable",
63+
),
64+
pytest.param(
65+
lambda: f.array_any_match(
66+
col("a"), f.lambda_(["v"], f.lambda_var("v") > lit(2))
67+
),
68+
[True, True],
69+
id="array_any_match_explicit_lambda",
70+
),
71+
pytest.param(
72+
lambda: f.any_match(col("a"), lambda v: v > 4),
73+
[False, True],
74+
id="any_match_alias",
75+
),
76+
pytest.param(
77+
lambda: f.list_any_match(col("a"), lambda v: v > 4),
78+
[False, True],
79+
id="list_any_match_alias",
80+
),
81+
],
82+
)
83+
def test_higher_order_function_results(df, build_expr, expected):
84+
assert _column(df, build_expr(), "r") == expected
7085

7186

7287
def test_lambda_param_name_appears_in_plan(df):
@@ -76,14 +91,21 @@ def test_lambda_param_name_appears_in_plan(df):
7691
assert "value" in expr.canonical_name()
7792

7893

79-
def test_to_lambda_rejects_non_callable():
80-
with pytest.raises(TypeError, match="expected an Expr or callable"):
81-
f.array_transform(col("a"), 42)
82-
83-
84-
def test_to_lambda_rejects_zero_arg_callable():
85-
with pytest.raises(ValueError, match="at least one parameter"):
86-
f.array_transform(col("a"), lambda: lit(1))
94+
@pytest.mark.parametrize(
95+
("arg", "exc_type", "match"),
96+
[
97+
pytest.param(42, TypeError, "expected an Expr or callable", id="non_callable"),
98+
pytest.param(
99+
lambda: lit(1),
100+
ValueError,
101+
"at least one parameter",
102+
id="zero_arg_callable",
103+
),
104+
],
105+
)
106+
def test_to_lambda_rejects_invalid_arg(arg, exc_type, match):
107+
with pytest.raises(exc_type, match=match):
108+
f.array_transform(col("a"), arg)
87109

88110

89111
def test_sql_lambda_requires_duckdb_dialect():

0 commit comments

Comments
 (0)