Add scouthub-eventi-be
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
// Test di integrazione reale (Prisma NON mockato): verifica il vincolo ON DELETE
|
||||
// CASCADE definito nella migration fra evento.parent_id -> evento(id) e fra
|
||||
// risorsa_collegata.evento_id -> evento(id). Un mock di Prisma non potrebbe
|
||||
// verificare un comportamento che vive nel database, quindi qui si usa una
|
||||
// connessione reale. Richiede un Postgres raggiungibile con lo schema di
|
||||
// scouthub-eventi-be già migrato (`npx prisma migrate deploy`), es. il database
|
||||
// locale di sviluppo scouthub_eventi.
|
||||
process.env.DATABASE_URL =
|
||||
process.env.DATABASE_URL ?? 'postgresql://postgres:postgres@localhost:5432/scouthub_eventi?schema=public';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
|
||||
describe('cascade delete evento -> figli e risorse collegate (vincolo DB)', () => {
|
||||
test('eliminare il parent elimina anche i figli e le risorse collegate', async () => {
|
||||
const parent = await prisma.evento.create({
|
||||
data: {
|
||||
orgId: 'org-cascade-test',
|
||||
brancaId: 'Lupetti',
|
||||
titolo: 'Campo estivo',
|
||||
dataInizio: new Date('2026-08-01'),
|
||||
dataFine: new Date('2026-08-10'),
|
||||
tipo: 'campo',
|
||||
creatoDa: 'user-test',
|
||||
},
|
||||
});
|
||||
|
||||
const figlio = await prisma.evento.create({
|
||||
data: {
|
||||
orgId: parent.orgId,
|
||||
brancaId: parent.brancaId,
|
||||
parentId: parent.id,
|
||||
titolo: 'Uscita di un giorno',
|
||||
dataInizio: new Date('2026-08-02'),
|
||||
dataFine: new Date('2026-08-02'),
|
||||
tipo: 'uscita',
|
||||
creatoDa: 'user-test',
|
||||
},
|
||||
});
|
||||
|
||||
const risorsa = await prisma.risorsaCollegata.create({
|
||||
data: {
|
||||
eventoId: parent.id,
|
||||
tipoRisorsa: 'attivita',
|
||||
risorsaId: 'att-1',
|
||||
servizioOrigine: 'scouthub-attivita-be',
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.evento.delete({ where: { id: parent.id } });
|
||||
|
||||
const figlioRimasto = await prisma.evento.findUnique({ where: { id: figlio.id } });
|
||||
const risorsaRimasta = await prisma.risorsaCollegata.findUnique({ where: { id: risorsa.id } });
|
||||
|
||||
expect(figlioRimasto).toBeNull();
|
||||
expect(risorsaRimasta).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user