// Integration tests for /notes — Drizzle ORM direct on real DB import { assertEquals, assertExists, assertRejects } from "@std/assert"; import { seedModules, seedNotes, seedPromotions, seedStudents, testDb, truncateAll, } from "../helpers/db_integration.ts"; import { notes } from "$root/databases/schema.ts"; import { and, eq } from "npm:drizzle-orm@0.45.2"; Deno.test({ name: "integration notes: list all notes", async fn() { await truncateAll(); await seedPromotions([{ id: "PROMO-2024" }]); const [s] = await seedStudents([{ nom: "Dupont", prenom: "Jean", idPromo: "PROMO-2024" }]); await seedModules([{ id: "MOD101", nom: "Module A" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "MOD101", note: 15.5 }]); const rows = await testDb.select().from(notes); assertEquals(rows.length, 1); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration notes: create and retrieve by composite key", async fn() { await truncateAll(); await seedPromotions([{ id: "PROMO-2024" }]); const [s] = await seedStudents([{ nom: "Martin", prenom: "Alice", idPromo: "PROMO-2024" }]); await seedModules([{ id: "MOD102", nom: "Module B" }]); const [created] = await testDb.insert(notes).values({ numEtud: s.numEtud, idModule: "MOD102", note: 12.0 }).returning(); assertExists(created); assertEquals(created.note, 12.0); const row = await testDb .select() .from(notes) .where(and(eq(notes.numEtud, s.numEtud), eq(notes.idModule, "MOD102"))) .then((r) => r[0] ?? null); assertExists(row); assertEquals(row.note, 12.0); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration notes: get by composite key returns null when not found", async fn() { await truncateAll(); const row = await testDb .select() .from(notes) .where(and(eq(notes.numEtud, 99999), eq(notes.idModule, "GHOST"))) .then((r) => r[0] ?? null); assertEquals(row, null); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration notes: duplicate composite key insert fails", async fn() { await truncateAll(); await seedPromotions([{ id: "PROMO-2024" }]); const [s] = await seedStudents([{ nom: "Durand", prenom: "Claire", idPromo: "PROMO-2024" }]); await seedModules([{ id: "MOD103", nom: "Module C" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "MOD103", note: 10.0 }]); await assertRejects(() => testDb.insert(notes).values({ numEtud: s.numEtud, idModule: "MOD103", note: 11.0 }) ); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration notes: update note value", async fn() { await truncateAll(); await seedPromotions([{ id: "PROMO-2024" }]); const [s] = await seedStudents([{ nom: "Bernard", prenom: "Lucie", idPromo: "PROMO-2024" }]); await seedModules([{ id: "MOD104", nom: "Module D" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "MOD104", note: 8.0 }]); const [updated] = await testDb .update(notes) .set({ note: 16.0 }) .where(and(eq(notes.numEtud, s.numEtud), eq(notes.idModule, "MOD104"))) .returning(); assertEquals(updated.note, 16.0); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration notes: delete removes the note", async fn() { await truncateAll(); await seedPromotions([{ id: "PROMO-2024" }]); const [s] = await seedStudents([{ nom: "Thomas", prenom: "Eva", idPromo: "PROMO-2024" }]); await seedModules([{ id: "MOD105", nom: "Module E" }]); await seedNotes([{ numEtud: s.numEtud, idModule: "MOD105", note: 14.0 }]); await testDb.delete(notes).where(and(eq(notes.numEtud, s.numEtud), eq(notes.idModule, "MOD105"))); const row = await testDb .select() .from(notes) .where(and(eq(notes.numEtud, s.numEtud), eq(notes.idModule, "MOD105"))) .then((r) => r[0] ?? null); assertEquals(row, null); }, sanitizeResources: false, sanitizeOps: false, });