import { assertEquals, assertExists, assertRejects } from "@std/assert"; import { seedModules, testDb, truncateAll, } from "../helpers/db_integration.ts"; import { modules } from "$root/databases/schema.ts"; import { eq } from "npm:drizzle-orm@0.45.2"; Deno.test({ name: "integration: GET /modules - returns all modules", async fn() { await truncateAll(); await seedModules([ { id: "MATH101", nom: "Mathématiques" }, { id: "INFO101", nom: "Informatique" }, ]); const rows = await testDb.select().from(modules); assertEquals(rows.length, 2); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: POST /modules - creates a module", async fn() { await truncateAll(); const [created] = await testDb .insert(modules) .values({ id: "PHYS101", nom: "Physique" }) .returning(); assertExists(created); assertEquals(created.id, "PHYS101"); assertEquals(created.nom, "Physique"); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: POST /modules - rejects duplicate id", async fn() { await truncateAll(); await seedModules([{ id: "MATH101", nom: "Mathématiques" }]); await assertRejects(() => testDb .insert(modules) .values({ id: "MATH101", nom: "Maths (doublon)" }) ); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: GET /modules/:id - returns a specific module", async fn() { await truncateAll(); await seedModules([{ id: "ELEC201", nom: "Électronique" }]); const row = await testDb .select() .from(modules) .where(eq(modules.id, "ELEC201")) .then((r) => r[0] ?? null); assertExists(row); assertEquals(row.nom, "Électronique"); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: GET /modules/:id - returns null when not found", async fn() { await truncateAll(); const row = await testDb .select() .from(modules) .where(eq(modules.id, "NONEXISTENT")) .then((r) => r[0] ?? null); assertEquals(row, null); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: PUT /modules/:id - updates a module", async fn() { await truncateAll(); await seedModules([{ id: "CHIM101", nom: "Chimie" }]); const [updated] = await testDb .update(modules) .set({ nom: "Chimie organique" }) .where(eq(modules.id, "CHIM101")) .returning(); assertExists(updated); assertEquals(updated.nom, "Chimie organique"); }, sanitizeResources: false, sanitizeOps: false, }); Deno.test({ name: "integration: DELETE /modules/:id - deletes a module", async fn() { await truncateAll(); await seedModules([{ id: "BIO101", nom: "Biologie" }]); const [deleted] = await testDb .delete(modules) .where(eq(modules.id, "BIO101")) .returning(); assertExists(deleted); const row = await testDb .select() .from(modules) .where(eq(modules.id, "BIO101")) .then((r) => r[0] ?? null); assertEquals(row, null); }, sanitizeResources: false, sanitizeOps: false, });