From b04ca64143d33b4b2d2f6cecfa1544afc6c5a0ee Mon Sep 17 00:00:00 2001 From: Sai Ganesh Maganti Date: Fri, 17 Apr 2026 19:00:40 +0530 Subject: [PATCH] Add tests for schemas routes and CRUD operations --- test/optimization schemas.test.ts | 117 ++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 test/optimization schemas.test.ts diff --git a/test/optimization schemas.test.ts b/test/optimization schemas.test.ts new file mode 100644 index 00000000..b3d626df --- /dev/null +++ b/test/optimization schemas.test.ts @@ -0,0 +1,117 @@ +import { describe, test, expect, beforeAll, afterAll } from "vitest"; +import { build } from "../src/server/app.js"; +import { TEST_CONNECTION_STRING } from "./lib/utils.js"; + +let app; + +const inject = (options) => + app.inject({ + headers: { pg: TEST_CONNECTION_STRING }, + ...options, + }); + +beforeAll(async () => { + app = build(); +}); + +afterAll(async () => { + await app.close(); +}); + +describe("server/routes/schemas", () => { + test("GET /schemas → list schemas", async () => { + const res = await inject({ method: "GET", url: "/schemas" }); + + expect(res.statusCode).toBe(200); + expect(res.headers["content-type"]).toContain("application/json"); + expect(Array.isArray(res.json())).toBe(true); + }); + + test("GET /schemas with query params", async () => { + const res = await inject({ + method: "GET", + url: "/schemas?include_system_schemas=true&limit=5&offset=0", + }); + + expect(res.statusCode).toBe(200); + expect(Array.isArray(res.json())).toBe(true); + }); + + test("GET /schemas/:id → 404 when missing", async () => { + const res = await inject({ + method: "GET", + url: "/schemas/non-existent-schema", + }); + + expect(res.statusCode).toBe(404); + }); + + test("CRUD /schemas lifecycle", async () => { + // ---- CREATE ---- + const createRes = await inject({ + method: "POST", + url: "/schemas", + payload: { name: "test_schema" }, + }); + + expect(createRes.statusCode).toBe(200); + + const created = createRes.json(); + expect(created).toMatchObject({ + id: expect.any(Number), + name: "test_schema", + owner: "postgres", + }); + + const { id } = created; + + // ---- READ ---- + const getRes = await inject({ + method: "GET", + url: `/schemas/${id}`, + }); + + expect(getRes.statusCode).toBe(200); + expect(getRes.json()).toMatchObject(created); + + // ---- UPDATE ---- + const updateRes = await inject({ + method: "PATCH", + url: `/schemas/${id}`, + payload: { name: "test_schema_updated" }, + }); + + expect(updateRes.statusCode).toBe(200); + expect(updateRes.json()).toMatchObject({ + id, + name: "test_schema_updated", + owner: "postgres", + }); + + // ---- DELETE ---- + const deleteRes = await inject({ + method: "DELETE", + url: `/schemas/${id}`, + }); + + expect(deleteRes.statusCode).toBe(200); + expect(deleteRes.json()).toMatchObject({ + id, + name: "test_schema_updated", + owner: "postgres", + }); + }); + + test("POST /schemas → invalid payload", async () => { + const res = await inject({ + method: "POST", + url: "/schemas", + payload: { name: "pg_" }, + }); + + expect(res.statusCode).toBe(400); + expect(res.json()).toEqual({ + error: 'unacceptable schema name "pg_"', + }); + }); +});