test: add full test coverage for notes, ues, ue-modules, ajustements, enseignements, users
- Unit tests (mock DB + API) for all missing endpoints - Integration tests (Drizzle direct) for all missing entities - E2E tests (handler + real DB) for all missing endpoints - Robustness tests: invalid inputs, SQL injection, type errors, business rule violations - Seed helpers: seedNotes, seedUeModules, seedEnseignements, seedAjustements - Add test:coverage and test:coverage:html tasks to deno.json Tests expose known handler bugs (marked [BUG] in test names): - ajustements PUT/DELETE: .where() without and() modifies all rows for student - Missing try/catch in modules, users, enseignements handlers - Whitespace accepted as valid string values - No type or business rule validation (note bounds, coeff >= 0)
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
// Unit tests for /ues endpoints — fixtures, mock API, mock DB
|
||||
|
||||
import { assertEquals, assertExists } from "@std/assert";
|
||||
import { mockFetch, restoreFetch } from "../helpers/api_mock.ts";
|
||||
import { createMockDb } from "../helpers/db_mock.ts";
|
||||
import { type UE, ues } from "../helpers/fixtures.ts";
|
||||
|
||||
// --- Fixtures ---
|
||||
|
||||
Deno.test("ues: fixtures have correct shape", () => {
|
||||
assertEquals(ues.length, 2);
|
||||
assertEquals(typeof ues[0].id, "number");
|
||||
assertEquals(typeof ues[0].nom, "string");
|
||||
});
|
||||
|
||||
// --- Mock API ---
|
||||
|
||||
Deno.test("mock API: GET /ues returns list", async () => {
|
||||
mockFetch({ "/ues": ues });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues");
|
||||
assertEquals(res.status, 200);
|
||||
const data: UE[] = await res.json();
|
||||
assertEquals(data.length, 2);
|
||||
assertExists(data.find((u) => u.nom === "UE Informatique"));
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /ues/:id returns one UE", async () => {
|
||||
mockFetch({ "/ues/1": ues[0] });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/1");
|
||||
assertEquals(res.status, 200);
|
||||
const data: UE = await res.json();
|
||||
assertEquals(data.id, 1);
|
||||
assertEquals(data.nom, "UE Informatique");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: GET /ues/:id 404 when not found", async () => {
|
||||
mockFetch({ "/ues/99": { status: 404, body: { error: "Ressource introuvable" } } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/99");
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /ues creates UE (201)", async () => {
|
||||
const newUE: UE = { id: 3, nom: "UE Physique" };
|
||||
mockFetch({ "/ues": { method: "POST", status: 201, body: newUE } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nom: "UE Physique" }),
|
||||
});
|
||||
assertEquals(res.status, 201);
|
||||
const data: UE = await res.json();
|
||||
assertEquals(data.nom, "UE Physique");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: POST /ues 400 on missing nom", async () => {
|
||||
mockFetch({ "/ues": { method: "POST", status: 400 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({}),
|
||||
});
|
||||
assertEquals(res.status, 400);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: PUT /ues/:id updates nom", async () => {
|
||||
const updated: UE = { id: 1, nom: "UE Informatique avancée" };
|
||||
mockFetch({ "/ues/1": { method: "PUT", status: 200, body: updated } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/1", {
|
||||
method: "PUT",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ nom: "UE Informatique avancée" }),
|
||||
});
|
||||
assertEquals(res.status, 200);
|
||||
const data: UE = await res.json();
|
||||
assertEquals(data.nom, "UE Informatique avancée");
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: PUT /ues/:id 404 when not found", async () => {
|
||||
mockFetch({ "/ues/99": { method: "PUT", status: 404 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/99", {
|
||||
method: "PUT",
|
||||
body: JSON.stringify({ nom: "X" }),
|
||||
});
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: DELETE /ues/:id returns 204", async () => {
|
||||
mockFetch({ "/ues/1": { method: "DELETE", status: 204 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/1", { method: "DELETE" });
|
||||
assertEquals(res.status, 204);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("mock API: DELETE /ues/:id 404 when not found", async () => {
|
||||
mockFetch({ "/ues/99": { method: "DELETE", status: 404 } });
|
||||
try {
|
||||
const res = await fetch("http://localhost/api/ues/99", { method: "DELETE" });
|
||||
assertEquals(res.status, 404);
|
||||
} finally {
|
||||
restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
// --- Mock DB ---
|
||||
|
||||
Deno.test("mock DB: find UE by id", () => {
|
||||
const db = createMockDb({ tables: { ues: [...ues] } });
|
||||
const u = db.findOne<UE>("ues", (u) => u.id === 1);
|
||||
assertExists(u);
|
||||
assertEquals(u.nom, "UE Informatique");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: insert UE", () => {
|
||||
const db = createMockDb({ tables: { ues: [...ues] } });
|
||||
db.insert<UE>("ues", { id: 3, nom: "UE Physique" });
|
||||
assertEquals(db.getTable("ues").length, 3);
|
||||
});
|
||||
|
||||
Deno.test("mock DB: update UE nom", () => {
|
||||
const db = createMockDb({ tables: { ues: [...ues] } });
|
||||
db.updateWhere<UE>("ues", (u) => u.id === 1, { nom: "Updated" });
|
||||
assertEquals(db.findOne<UE>("ues", (u) => u.id === 1)?.nom, "Updated");
|
||||
});
|
||||
|
||||
Deno.test("mock DB: delete UE", () => {
|
||||
const db = createMockDb({ tables: { ues: [...ues] } });
|
||||
db.deleteWhere<UE>("ues", (u) => u.id === 1);
|
||||
assertEquals(db.getTable("ues").length, 1);
|
||||
});
|
||||
Reference in New Issue
Block a user