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

Allow complex types as function params. #7759

Merged
merged 1 commit into from
Sep 17, 2024
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
1 change: 0 additions & 1 deletion edb/schema/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ def from_ast(
subtypes=[paramt_ast],
)

assert isinstance(paramt_ast, qlast.TypeName)
paramt = utils.ast_to_type_shell(
paramt_ast,
metaclass=s_types.Type,
Expand Down
134 changes: 134 additions & 0 deletions tests/test_edgeql_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8174,3 +8174,137 @@ async def test_edgeql_call_type_as_function_01(self):
await self.con.execute(f"""
select cal::local_date(1);
""")

async def test_edgeql_functions_complex_types_01(self):
await self.con.execute('''
create function foo(x: File | URL) -> File | URL using (
x
);
''')
await self.assert_query_result(
'select foo(<File>{})',
[],
)
await self.assert_query_result(
'select foo(<URL>{})',
[],
)
await self.assert_query_result(
'select foo(<File | URL>{})',
[],
)
await self.assert_query_result(
'select foo((select File)).name',
['screenshot.png'],
sort=True,
)
await self.assert_query_result(
'select foo((select URL)).name',
['edgedb.com'],
sort=True,
)
await self.assert_query_result(
'select foo((select {File, URL})).name',
['edgedb.com', 'screenshot.png'],
sort=True,
)

async def test_edgeql_functions_complex_types_02(self):
await self.con.execute('''
create function foo(x: str) -> optional File | URL using (
select {File, URL} filter .name = x limit 1
);
''')
await self.assert_query_result(
'select foo(<str>{})',
[],
)
await self.assert_query_result(
'select foo("haha")',
[],
)
await self.assert_query_result(
'select foo("screenshot.png").name',
['screenshot.png'],
sort=True,
)
await self.assert_query_result(
'select foo("edgedb.com").name',
['edgedb.com'],
sort=True,
)
await self.assert_query_result(
'select foo({"edgedb.com", "screenshot.png"}).name',
['edgedb.com', 'screenshot.png'],
sort=True,
)

async def test_edgeql_functions_complex_types_03(self):
await self.con.execute('''
create function foo(x: File | URL) -> str using (
x.name
);
''')
await self.assert_query_result(
'select foo(<File>{})',
[],
)
await self.assert_query_result(
'select foo(<URL>{})',
[],
)
await self.assert_query_result(
'select foo(<File | URL>{})',
[],
)
await self.assert_query_result(
'select foo((select File))',
['screenshot.png'],
sort=True,
)
await self.assert_query_result(
'select foo((select URL))',
['edgedb.com'],
sort=True,
)
await self.assert_query_result(
'select foo((select {File, URL}))',
['edgedb.com', 'screenshot.png'],
sort=True,
)

async def test_edgeql_functions_complex_types_04(self):
await self.con.execute('''
create function foo(x: File | URL) -> str using (
if x is URL
then assert_exists(x[is URL]).address
else '~/' ++ x.name
);
''')
await self.assert_query_result(
'select foo(<File>{})',
[],
)
await self.assert_query_result(
'select foo(<URL>{})',
[],
)
await self.assert_query_result(
'select foo(<File | URL>{})',
[],
)
await self.assert_query_result(
'select foo((select File))',
['~/screenshot.png'],
sort=True,
)
await self.assert_query_result(
'select foo((select URL))',
['https://edgedb.com'],
sort=True,
)
await self.assert_query_result(
'select foo((select {File, URL}))',
['https://edgedb.com', '~/screenshot.png'],
sort=True,
)