From 8891876530035c7f847cfcbc17242eb5e6f130fe Mon Sep 17 00:00:00 2001 From: dnwpark Date: Tue, 17 Sep 2024 12:41:18 -0400 Subject: [PATCH] Allow complex types as function params. --- edb/schema/functions.py | 1 - tests/test_edgeql_functions.py | 134 +++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/edb/schema/functions.py b/edb/schema/functions.py index 9cca3de6def..0f7477f756c 100644 --- a/edb/schema/functions.py +++ b/edb/schema/functions.py @@ -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, diff --git a/tests/test_edgeql_functions.py b/tests/test_edgeql_functions.py index 089186b7730..72bec5b10fd 100644 --- a/tests/test_edgeql_functions.py +++ b/tests/test_edgeql_functions.py @@ -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({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + 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({})', + [], + ) + 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({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + 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({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + await self.assert_query_result( + 'select foo({})', + [], + ) + 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, + )