Skip to content

Commit

Permalink
Add another test
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis committed Jul 24, 2024
1 parent 87525a8 commit a679733
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/pglite/tests/plpgsql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit a679733

Please sign in to comment.