schema: la tabella score rifiuta un valore sotto uno stato che lo nega
CHECK ((stato='ok' AND valore NOT NULL) OR (stato='insufficiente' AND valore NULL)): il tipo Punteggio protegge il codice, non la tabella, e l'interfaccia leggerà 'score' direttamente. Idempotente sui database nuovi (CREATE TABLE IF NOT EXISTS); un longevity.db già esistente NON riceve il vincolo, SQLite non lo permette via ALTER TABLE. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XhLbMQ1q7wHwJSykgXRwQF
This commit is contained in:
@@ -84,7 +84,15 @@ CREATE TABLE IF NOT EXISTS score (
|
||||
stato TEXT NOT NULL CHECK (stato IN ('ok','insufficiente')),
|
||||
quest_version TEXT,
|
||||
model_version TEXT NOT NULL,
|
||||
calcolato_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
calcolato_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
-- Il tipo Punteggio protegge il codice ma non la tabella: l'interfaccia leggera'
|
||||
-- 'score' direttamente, dove quel tipo non c'e' piu'. Questo vincolo lo riscrive
|
||||
-- a livello di dato: o lo stato e' 'ok' e il valore c'e', o e' 'insufficiente' e
|
||||
-- il valore e' NULL, mai un numero scritto sotto uno stato che lo nega.
|
||||
-- SQLite non permette di aggiungere un CHECK con ALTER TABLE a una tabella gia'
|
||||
-- creata: un database longevity.db esistente, creato prima di questa modifica,
|
||||
-- NON avra' questo vincolo finche' non viene ricreato.
|
||||
CHECK ((stato = 'ok' AND valore IS NOT NULL) OR (stato = 'insufficiente' AND valore IS NULL))
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_score_cliente ON score (client_code, calcolato_at DESC);
|
||||
`;
|
||||
|
||||
@@ -43,6 +43,33 @@ describe('schema longevity', () => {
|
||||
expect(row.stato).toBe('insufficiente');
|
||||
});
|
||||
|
||||
it('la tabella rifiuta uno score insufficiente CON un valore scritto', () => {
|
||||
// Il tipo Punteggio protegge il codice, ma l'interfaccia legge dalla tabella,
|
||||
// dove il tipo non c'e' piu': senza questo vincolo si potrebbe scrivere un
|
||||
// numero dichiarato "insufficiente" e chi legge crud dalla tabella lo prenderebbe.
|
||||
const db = createLongevityDb(':memory:');
|
||||
db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run();
|
||||
db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run();
|
||||
expect(() =>
|
||||
db.prepare(
|
||||
`INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version)
|
||||
VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', 42, 0.2, 'insufficiente', 'v1.0')`
|
||||
).run()
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('la tabella rifiuta uno score ok SENZA valore', () => {
|
||||
const db = createLongevityDb(':memory:');
|
||||
db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run();
|
||||
db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run();
|
||||
expect(() =>
|
||||
db.prepare(
|
||||
`INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version)
|
||||
VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', NULL, 1, 'ok', 'v1.0')`
|
||||
).run()
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it('identity tiene la persona, longevity non la conosce', () => {
|
||||
const id = createIdentityDb(':memory:');
|
||||
const cols = (id.prepare(`PRAGMA table_info(clienti)`).all() as { name: string }[]).map((c) => c.name);
|
||||
|
||||
Reference in New Issue
Block a user