714486f43c
Check Deno code / Check Deno code (pull_request) Successful in 5s
Tests / Unit tests (pull_request) Successful in 12s
Tests / Integration tests (pull_request) Successful in 1m9s
Check Deno code / Check Deno code (push) Successful in 6s
Tests / Unit tests (push) Successful in 12s
Tests / Integration tests (push) Successful in 1m13s
165 lines
4.8 KiB
TypeScript
165 lines
4.8 KiB
TypeScript
// 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);
|
|
});
|