test(integration): add DB integration tests for students, promotions, roles, modules

Covers full CRUD for each resource via testDb:
- promotions: list, create, get by id, not found, update, delete
- students: list, filter by promo, create, get, not found, update, delete
- roles: list, create, get with permissions, update+reset perms, delete
- modules: list, create, duplicate id rejection, get, not found, update, delete

27 integration tests passing in CI (act + Gitea Actions).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 13:36:26 +02:00
parent 367b0b2357
commit c5018d9ced
5 changed files with 568 additions and 9 deletions
+163
View File
@@ -0,0 +1,163 @@
import { assertEquals, assertExists } from "@std/assert";
import {
seedPromotions,
seedStudents,
testDb,
truncateAll,
} from "../helpers/db_integration.ts";
import { students } from "$root/databases/schema.ts";
import { eq } from "npm:drizzle-orm@0.45.2";
Deno.test({
name: "integration: GET /students - returns all students",
async fn() {
await truncateAll();
await seedPromotions([{ id: "PEIP1-2024" }]);
await seedStudents([
{ nom: "Dupont", prenom: "Jean", idPromo: "PEIP1-2024" },
{ nom: "Martin", prenom: "Alice", idPromo: "PEIP1-2024" },
{ nom: "Durand", prenom: "Claire", idPromo: "PEIP1-2024" },
]);
const rows = await testDb.select().from(students);
assertEquals(rows.length, 3);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: GET /students?idPromo - filters by promotion",
async fn() {
await truncateAll();
await seedPromotions([
{ id: "PEIP1-2024" },
{ id: "PEIP2-2024" },
]);
await seedStudents([
{ nom: "Dupont", prenom: "Jean", idPromo: "PEIP1-2024" },
{ nom: "Martin", prenom: "Alice", idPromo: "PEIP1-2024" },
{ nom: "Durand", prenom: "Claire", idPromo: "PEIP2-2024" },
]);
const rows = await testDb
.select()
.from(students)
.where(eq(students.idPromo, "PEIP1-2024"));
assertEquals(rows.length, 2);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: POST /students - creates a student",
async fn() {
await truncateAll();
await seedPromotions([{ id: "INFO3-2024" }]);
const [created] = await testDb
.insert(students)
.values({ nom: "Leroy", prenom: "Paul", idPromo: "INFO3-2024" })
.returning();
assertExists(created);
assertExists(created.numEtud);
assertEquals(created.nom, "Leroy");
assertEquals(created.prenom, "Paul");
assertEquals(created.idPromo, "INFO3-2024");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: GET /students/:numEtud - returns a specific student",
async fn() {
await truncateAll();
await seedPromotions([{ id: "INFO3-2024" }]);
const [student] = await seedStudents([
{ nom: "Bernard", prenom: "Lucie", idPromo: "INFO3-2024" },
]);
const row = await testDb
.select()
.from(students)
.where(eq(students.numEtud, student.numEtud))
.then((r) => r[0] ?? null);
assertExists(row);
assertEquals(row.nom, "Bernard");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: GET /students/:numEtud - returns null when not found",
async fn() {
await truncateAll();
const row = await testDb
.select()
.from(students)
.where(eq(students.numEtud, 999999))
.then((r) => r[0] ?? null);
assertEquals(row, null);
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: PUT /students/:numEtud - updates a student",
async fn() {
await truncateAll();
await seedPromotions([{ id: "INFO3-2024" }, { id: "INFO4-2024" }]);
const [student] = await seedStudents([
{ nom: "Petit", prenom: "Hugo", idPromo: "INFO3-2024" },
]);
const [updated] = await testDb
.update(students)
.set({ nom: "Grand", prenom: "Hugo", idPromo: "INFO4-2024" })
.where(eq(students.numEtud, student.numEtud))
.returning();
assertExists(updated);
assertEquals(updated.nom, "Grand");
assertEquals(updated.idPromo, "INFO4-2024");
},
sanitizeResources: false,
sanitizeOps: false,
});
Deno.test({
name: "integration: DELETE /students/:numEtud - deletes a student",
async fn() {
await truncateAll();
await seedPromotions([{ id: "INFO3-2024" }]);
const [student] = await seedStudents([
{ nom: "Thomas", prenom: "Eva", idPromo: "INFO3-2024" },
]);
const [deleted] = await testDb
.delete(students)
.where(eq(students.numEtud, student.numEtud))
.returning();
assertExists(deleted);
const row = await testDb
.select()
.from(students)
.where(eq(students.numEtud, student.numEtud))
.then((r) => r[0] ?? null);
assertEquals(row, null);
},
sanitizeResources: false,
sanitizeOps: false,
});