From 3f76ac3e64dcdb90e84cc345f58697649de778c2 Mon Sep 17 00:00:00 2001 From: avallete Date: Sun, 30 Mar 2025 14:15:26 +0200 Subject: [PATCH] chore: add missing functions tests --- packages/pg-meta/test/functions.test.ts | 165 ++++++++++++++++++++++-- 1 file changed, 157 insertions(+), 8 deletions(-) diff --git a/packages/pg-meta/test/functions.test.ts b/packages/pg-meta/test/functions.test.ts index 35230763824..61883f92438 100644 --- a/packages/pg-meta/test/functions.test.ts +++ b/packages/pg-meta/test/functions.test.ts @@ -31,7 +31,8 @@ withTestDatabase('list functions', async ({ executeQuery }) => { // Test for the 'add' function created in init.sql const addFunction = res.find(({ name }) => name === 'add') expect(addFunction).toMatchInlineSnapshot( - { id: expect.any(Number) }, ` + { id: expect.any(Number) }, + ` { "args": [ { @@ -71,9 +72,151 @@ withTestDatabase('list functions', async ({ executeQuery }) => { "schema": "public", "security_definer": false, } - `) + ` + ) }) +withTestDatabase( + 'list set-returning function with single object limit', + async ({ executeQuery }) => { + const { sql, zod } = await pgMeta.functions.list() + const res = zod.parse(await executeQuery(sql)) + + const singleRowFunction = res.find(({ name }) => name === 'get_user_audit_setof_single_row') + expect(singleRowFunction).toMatchInlineSnapshot( + { id: expect.any(Number) }, ` + { + "args": [ + { + "has_default": false, + "mode": "in", + "name": "user_row", + "type_id": 16395, + }, + ], + "argument_types": "user_row users", + "behavior": "STABLE", + "complete_statement": "CREATE OR REPLACE FUNCTION public.get_user_audit_setof_single_row(user_row users) + RETURNS SETOF users_audit + LANGUAGE sql + STABLE ROWS 1 + AS $function$ + SELECT * FROM public.users_audit WHERE user_id = user_row.id; + $function$ + ", + "config_params": null, + "definition": " + SELECT * FROM public.users_audit WHERE user_id = user_row.id; + ", + "id": Any, + "identity_argument_types": "user_row users", + "is_set_returning_function": true, + "language": "sql", + "name": "get_user_audit_setof_single_row", + "return_table_name": "users_audit", + "return_type": "SETOF users_audit", + "return_type_id": 16418, + "return_type_relation_id": 16416, + "returns_multiple_rows": false, + "returns_set_of_table": true, + "schema": "public", + "security_definer": false, + } + `) + } +) + +withTestDatabase( + 'list set-returning function with multiples definitions', + async ({ executeQuery }) => { + const { sql, zod } = await pgMeta.functions.list() + const res = zod.parse(await executeQuery(sql)) + + const multipleRowsFunctions = res.filter(({ name }) => name === 'get_todos_setof_rows') + expect(multipleRowsFunctions).toMatchInlineSnapshot( + multipleRowsFunctions.map((func) => ({ ...func, id: expect.any(Number) })), + ` + [ + { + "args": [ + { + "has_default": false, + "mode": "in", + "name": "user_row", + "type_id": 16395, + }, + ], + "argument_types": "user_row users", + "behavior": "STABLE", + "complete_statement": "CREATE OR REPLACE FUNCTION public.get_todos_setof_rows(user_row users) + RETURNS SETOF todos + LANGUAGE sql + STABLE + AS $function$ + SELECT * FROM public.todos WHERE "user-id" = user_row.id; + $function$ + ", + "config_params": null, + "definition": " + SELECT * FROM public.todos WHERE "user-id" = user_row.id; + ", + "id": Any, + "identity_argument_types": "user_row users", + "is_set_returning_function": true, + "language": "sql", + "name": "get_todos_setof_rows", + "return_table_name": "todos", + "return_type": "SETOF todos", + "return_type_id": 16404, + "return_type_relation_id": 16402, + "returns_multiple_rows": true, + "returns_set_of_table": true, + "schema": "public", + "security_definer": false, + }, + { + "args": [ + { + "has_default": false, + "mode": "in", + "name": "todo_row", + "type_id": 16404, + }, + ], + "argument_types": "todo_row todos", + "behavior": "STABLE", + "complete_statement": "CREATE OR REPLACE FUNCTION public.get_todos_setof_rows(todo_row todos) + RETURNS SETOF todos + LANGUAGE sql + STABLE + AS $function$ + SELECT * FROM public.todos WHERE "user-id" = todo_row."user-id"; + $function$ + ", + "config_params": null, + "definition": " + SELECT * FROM public.todos WHERE "user-id" = todo_row."user-id"; + ", + "id": Any, + "identity_argument_types": "todo_row todos", + "is_set_returning_function": true, + "language": "sql", + "name": "get_todos_setof_rows", + "return_table_name": "todos", + "return_type": "SETOF todos", + "return_type_id": 16404, + "return_type_relation_id": 16402, + "returns_multiple_rows": true, + "returns_set_of_table": true, + "schema": "public", + "security_definer": false, + }, + ] + ` + ) + } +) + withTestDatabase('list functions with included schemas', async ({ executeQuery }) => { const { sql, zod } = await pgMeta.functions.list({ includedSchemas: ['public'], @@ -138,7 +281,8 @@ withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => const res = retrieveZod.parse(retrieve[0]) const functionId = res!.id expect({ data: res, error: null }).toMatchInlineSnapshot( - { data: { id: expect.any(Number) } }, ` + { data: { id: expect.any(Number) } }, + ` { "data": { "args": [ @@ -186,7 +330,8 @@ withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => }, "error": null, } - `) + ` + ) // create test_schema to move the function into: const { sql: createSchemaSql } = await pgMeta.schemas.create({ name: 'test_schema' }) await executeQuery(createSchemaSql) @@ -201,7 +346,8 @@ withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => const retrieveRenamed = await executeQuery(retrieveRenamedSql) const resUpdated = retrieveZod.parse(retrieveRenamed[0]) expect({ data: resUpdated, error: null }).toMatchInlineSnapshot( - { data: { id: expect.any(Number) } }, ` + { data: { id: expect.any(Number) } }, + ` { "data": { "args": [ @@ -249,7 +395,8 @@ withTestDatabase('retrieve, create, update, delete', async ({ executeQuery }) => }, "error": null, } - `) + ` + ) // Remove function const { sql: removeSql } = await pgMeta.functions.remove(resUpdated!) @@ -274,7 +421,8 @@ withTestDatabase('retrieve set-returning function', async ({ executeQuery }) => id: expect.any(Number), return_type_id: expect.any(Number), return_type_relation_id: expect.any(Number), - }, ` + }, + ` { "args": [], "argument_types": "", @@ -305,7 +453,8 @@ withTestDatabase('retrieve set-returning function', async ({ executeQuery }) => "schema": "public", "security_definer": false, } - `) + ` + ) }) withTestDatabase('create function with various config_params values', async ({ executeQuery }) => {