Skip to content

Commit 12fe2d2

Browse files
enobayramDanCardin
authored andcommitted
Add RETURNS TABLE(...) example to function/test_create_postgresql.sql
1 parent b939839 commit 12fe2d2

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

tests/function/test_create_postgresql.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ class Base(_Base): # type: ignore
2727
returns="INTEGER",
2828
volatility=FunctionVolatility.STABLE,
2929
),
30+
# Function returning TABLE
31+
Function(
32+
"generate_series_squared",
33+
'''
34+
SELECT i, i*i
35+
FROM generate_series(1, _limit) as i;
36+
''',
37+
language="sql",
38+
parameters=["_limit integer"],
39+
returns="TABLE(num integer, num_squared integer)",
40+
volatility=FunctionVolatility.IMMUTABLE,
41+
),
3042
)
3143

3244

@@ -51,22 +63,21 @@ def test_create(pg):
5163
result = pg.execute(text("SELECT gimme()")).scalar()
5264
assert result == 2
5365

54-
connection = pg.connection()
55-
diff = compare_functions(connection, Base.metadata.info["functions"])
56-
assert diff == []
57-
66+
# Test function with parameters
67+
result_params = pg.execute(text("SELECT add_stable(10)")).scalar()
68+
assert result_params == 11
5869

59-
def test_create_with_params(pg):
60-
Base.metadata.create_all(bind=pg.connection())
61-
pg.commit()
70+
result_params_2 = pg.execute(text("SELECT add_stable(1)")).scalar()
71+
assert result_params_2 == 2
6272

63-
result = pg.execute(text("SELECT add_stable(10)")).scalar()
64-
assert result == 11
65-
66-
result = pg.execute(text("SELECT add_stable(1)")).scalar()
67-
assert result == 2
73+
# Test function returning table
74+
result_table = pg.execute(text("SELECT * FROM generate_series_squared(3)")).fetchall()
75+
assert result_table == [
76+
(1, 1),
77+
(2, 4),
78+
(3, 9),
79+
]
6880

69-
# Verify the function is there via comparison
7081
connection = pg.connection()
7182
diff = compare_functions(connection, Base.metadata.info["functions"])
7283
assert diff == []

0 commit comments

Comments
 (0)