Automatiska tester, och rättningar som testerna hittade
Vitest med enhetstester utan databas och databastester mot en egen testdatabas vars namn måste sluta på _test. Rättar tomma MAP_*-värden i .env och säkerhetskopior med å, ä och ö i filnamnet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e8d79ee10
commit
dcb5d43429
30 files changed
+2872
-13
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { mkdir, readdir, rm } from "node:fs/promises";
|
||||
import type { EntityKind, Prisma } from "@prisma/client";
|
||||
import { describe } from "vitest";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { UPLOAD_DIR } from "@/lib/files";
|
||||
import { nextEntityCode, nextRelationCode } from "@/lib/ids";
|
||||
import { BACKUP_DIR } from "@/lib/backup";
|
||||
import { TRASH_DIR } from "@/lib/trash";
|
||||
|
||||
export { prisma };
|
||||
|
||||
/** Databastester körs bara när en testdatabas är konfigurerad (se vitest.config.ts). */
|
||||
export const hasDb = Boolean(process.env.DATABASE_URL);
|
||||
export const describeDb = hasDb ? describe : describe.skip;
|
||||
|
||||
/** Tömmer alla tabeller och filmapparna. Kräver att databasens namn slutar på _test. */
|
||||
export async function resetAll() {
|
||||
const [{ current_database: name }] = await prisma.$queryRaw<{ current_database: string }[]>`SELECT current_database()`;
|
||||
if (!name.endsWith("_test")) throw new Error(`Vägrar tömma databasen "${name}": namnet slutar inte på _test`);
|
||||
|
||||
const tables = await prisma.$queryRaw<{ tablename: string }[]>`SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename <> '_prisma_migrations'`;
|
||||
if (tables.length > 0) {
|
||||
await prisma.$executeRawUnsafe(`TRUNCATE TABLE ${tables.map((t) => `"${t.tablename}"`).join(", ")} RESTART IDENTITY CASCADE`);
|
||||
}
|
||||
for (const dir of [UPLOAD_DIR, BACKUP_DIR, TRASH_DIR]) {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
await mkdir(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
export async function filesIn(dir: string): Promise<string[]> {
|
||||
try {
|
||||
return (await readdir(dir)).filter((f) => f !== "thumbs").sort();
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function makeUser(name = "Tester", email: string | null = `${name.toLowerCase()}@example.com`) {
|
||||
return prisma.user.create({ data: { externalId: `test:${randomUUID()}`, name, email } });
|
||||
}
|
||||
|
||||
export async function makeBook(ownerId: string, title = "Testbok") {
|
||||
return prisma.book.create({ data: { ownerId, title } });
|
||||
}
|
||||
|
||||
export async function makeEntity(
|
||||
bookId: string,
|
||||
kind: EntityKind,
|
||||
name: string,
|
||||
extra: { data?: Prisma.InputJsonObject; imageFile?: string | null; tags?: string[]; summary?: string } = {},
|
||||
) {
|
||||
return prisma.entity.create({
|
||||
data: { bookId, kind, name, code: await nextEntityCode(bookId, kind), tags: extra.tags ?? [], data: extra.data ?? {}, imageFile: extra.imageFile ?? null, summary: extra.summary ?? null },
|
||||
});
|
||||
}
|
||||
|
||||
export async function makeRelation(bookId: string, fromId: string, toId: string, type = "Vänner", memberIds: string[] = []) {
|
||||
return prisma.relation.create({
|
||||
data: { bookId, fromId, toId, type, code: await nextRelationCode(bookId), members: { create: memberIds.map((entityId) => ({ entityId })) } },
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user