From a6797338513ef1f310a9d58ccf121996befd7b7a Mon Sep 17 00:00:00 2001 From: Sam Willis Date: Wed, 24 Jul 2024 11:15:14 +0100 Subject: [PATCH] Add another test --- packages/pglite/tests/plpgsql.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/pglite/tests/plpgsql.test.js b/packages/pglite/tests/plpgsql.test.js index 1e54a4a4..55ccf1a4 100644 --- a/packages/pglite/tests/plpgsql.test.js +++ b/packages/pglite/tests/plpgsql.test.js @@ -15,3 +15,28 @@ test("can create and call function", async (t) => { const res = await db.query("SELECT test_func();"); t.is(res.rows[0].test_func, "test"); }); + +test("can create and call complex function", async (t) => { + const db = new PGlite(); + await db.exec(` + CREATE OR REPLACE FUNCTION calculate_factorial(n INT) RETURNS INT AS $$ + DECLARE + result INT := 1; + BEGIN + IF n < 0 THEN + RAISE EXCEPTION 'The input cannot be negative.'; + ELSIF n = 0 OR n = 1 THEN + RETURN result; + ELSE + FOR i IN 2..n LOOP + result := result * i; + END LOOP; + RETURN result; + END IF; + END; + $$ LANGUAGE plpgsql; + `); + + const res = await db.query("SELECT calculate_factorial(5) AS result;"); + t.is(res.rows[0].result, 120); +});