E4: video_analysis con istante del fotogramma e nota (migrazione 0005)

«Salva il fotogramma» deve salvare il fotogramma scelto dall'analisi (il
più nitido, mai uno nero) e il visualizzatore deve dire perché un video è
«Non analizzabile». La migrazione 0005 aggiunge a video_analysis
frame_at_ms e note, con i CHECK nello schema: un «Fermo» ha sempre il suo
istante e nessun altro esito ne ha uno, un «Non analizzabile» ha sempre la
nota. Un indice parziale sugli esiti fermi serve al filtro «Video fermi».
Le righe impossibili (scritte a mano: nessun Febe prima di E4 scrive la
tabella) si tolgono prima, così la migrazione non si ferma.

VideoAnalysis ha FrameAt e Note; VideoAnalysisNotes elenca i codici,
compreso SingleColor per il video tutto di un colore (risposta 42 di
Alberto: tutto nero o di un colore è «Non analizzabile»). L'archivio
legge e scrive le colonne nuove e rifiuta le forme che lo schema rifiuta,
e una nota che non sia fatta di codici.

Prove: versione dello schema 5, aggiornamento dalla 4, la 0005 cambia
solo video_analysis, vincoli e archivio.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-29 13:54:30 +01:00
1 parent b3c15da821
commit 0cd842a518
8 files changed
+278 -20

No files matched your search

+26 -1
View File
@@ -57,7 +57,7 @@ public sealed class ExportConstraintTests
await RejectedAsync(db, "export_item", w.ItemRow(run, ("state", "Done"), ("media_id", w.LucaMedia)), "stato del file");
await RejectedAsync(db, "export_item", w.ItemRow(run, ("variant", "Intero"), ("media_id", w.LucaMedia)), "variante del file");
await RejectedAsync(db, "export_item", w.ItemRow(run, ("format", "Gif"), ("variant", "Converted"), ("media_id", w.LucaMedia)), "formato del file");
await RejectedAsync(db, "video_analysis", AnalysisRow(("outcome", "Fermo"), ("sha256", Make.Sha(6))), "esito");
await RejectedAsync(db, "video_analysis", AnalysisRow(("outcome", "Fermo"), ("frame_at_ms", null), ("sha256", Make.Sha(6))), "esito");
await RejectedAsync(db, "video_analysis", AnalysisRow(("algorithm", 0L), ("sha256", Make.Sha(6))), "algoritmo 0");
await RejectedAsync(db, "media_conversion", w.ConversionRow(("format", "Original"), ("media_id", w.LucaMedia)), "un convertito ha un altro formato");
await RejectedAsync(
@@ -188,6 +188,31 @@ public sealed class ExportConstraintTests
await InsertAsync(db, "media_conversion", Converted(w, "Jpg", Make.Sha(2), ("bytes", 0L)));
}
/// <summary>
/// The columns of migration 0005 (tappa E4): a still video has the instant of its frame, no other outcome has one, and
/// a «Non analizzabile» says why. Each row breaks that CHECK alone (the same row with a valid value is accepted).
/// </summary>
[TestMethod]
public async Task VideoAnalysisFrameAndNoteGoWithTheOutcome()
{
using var temp = new TempFolder();
await using var db = await TestDatabase.OpenAsync(temp);
await RejectedAsync(db, "video_analysis", AnalysisRow(("frame_at_ms", null)), "fermo senza istante");
await RejectedAsync(db, "video_analysis", AnalysisRow(("frame_at_ms", -1L)), "istante negativo");
await RejectedAsync(db, "video_analysis", AnalysisRow(("outcome", "Moving")), "in movimento con un istante");
await RejectedAsync(db, "video_analysis", AnalysisRow(("outcome", "NotAnalyzable"), ("frame_at_ms", null)), "non analizzabile senza nota");
await RejectedAsync(db, "video_analysis", AnalysisRow(("outcome", "Moving"), ("frame_at_ms", null), ("note", "")), "nota vuota");
await SchemaDeclaresAsync(db, "video_analysis", "CHECK ((outcome = 'Still') = (frame_at_ms IS NOT NULL))");
await SchemaDeclaresAsync(db, "video_analysis", "CHECK (outcome <> 'NotAnalyzable' OR note IS NOT NULL)");
await InsertAsync(db, "video_analysis", AnalysisRow());
await InsertAsync(db, "video_analysis", AnalysisRow(("sha256", Make.Sha(6)), ("frame_at_ms", 1125L), ("note", "Prova")));
await InsertAsync(db, "video_analysis", AnalysisRow(("sha256", Make.Sha(7)), ("outcome", "Moving"), ("frame_at_ms", null)));
await InsertAsync(db, "video_analysis", AnalysisRow(("sha256", Make.Sha(8)), ("outcome", "NotAnalyzable"), ("frame_at_ms", null), ("note", "TooSlow")));
Assert.AreEqual(4L, await ScalarAsync(db, "SELECT COUNT(*) FROM video_analysis"));
}
/// <summary>The removed account kept on a copy: only on a row without account, never empty, and both may be unknown.</summary>
[TestMethod]
public async Task TheRemovedAccountIsKeptOnlyOnOrphanCopies()
+56 -7
View File
@@ -791,20 +791,22 @@ public sealed class ExportStoreTests
await using var db = await TestDatabase.OpenAsync(temp);
Assert.IsNull(await db.VideoAnalyses.GetAsync(Make.Sha(1)));
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1).ToUpperInvariant(), 1, VideoAnalysisOutcome.Still, Make.T0));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Still, Make.T0), await db.VideoAnalyses.GetAsync(Make.Sha(1)));
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1).ToUpperInvariant(), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(1125)));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(1125)), await db.VideoAnalyses.GetAsync(Make.Sha(1)));
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Moving, Make.T0.AddDays(1)));
Assert.AreEqual(2, (await db.VideoAnalyses.GetAsync(Make.Sha(1)))!.Algorithm, "vale l'algoritmo più alto");
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0.AddDays(2)));
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0.AddDays(2), Note: VideoAnalysisNotes.TooSlow));
Assert.AreEqual(VideoAnalysisOutcome.Moving, (await db.VideoAnalyses.GetAsync(Make.Sha(1)))!.Outcome);
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3)));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3)), await db.VideoAnalyses.GetAsync(Make.Sha(1)), "lo stesso algoritmo si sostituisce");
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3), TimeSpan.Zero));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 2, VideoAnalysisOutcome.Still, Make.T0.AddDays(3), TimeSpan.Zero), await db.VideoAnalyses.GetAsync(Make.Sha(1)), "lo stesso algoritmo si sostituisce");
Assert.AreEqual(2L, await ScalarAsync(db, "SELECT COUNT(*) FROM video_analysis"));
for (var i = 10; i < 710; i++)
{
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(i), 1, i % 2 == 0 ? VideoAnalysisOutcome.Still : VideoAnalysisOutcome.Moving, Make.T0));
await db.VideoAnalyses.SetAsync(i % 2 == 0
? new VideoAnalysis(Make.Sha(i), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(i))
: new VideoAnalysis(Make.Sha(i), 1, VideoAnalysisOutcome.Moving, Make.T0));
}
var keys = Enumerable.Range(0, 800).Select(Make.Sha).ToList();
@@ -812,7 +814,54 @@ public sealed class ExportStoreTests
Assert.HasCount(701, many);
Assert.AreEqual(2, many[Make.Sha(1)].Algorithm);
Assert.AreEqual(VideoAnalysisOutcome.Moving, many[Make.Sha(709)].Outcome);
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(() => db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 0, VideoAnalysisOutcome.Still, Make.T0)));
Assert.AreEqual(TimeSpan.FromMilliseconds(708), many[Make.Sha(708)].FrameAt, "GetManyAsync legge anche l'istante");
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(() => db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 0, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.Zero)));
}
/// <summary>
/// The instant of the frame and the note of migration 0005 (tappa E4) round-trip; the store keeps a still video with
/// its instant and only it, and a «Non analizzabile» with the reason, so the viewer can always say why.
/// </summary>
[TestMethod]
public async Task VideoAnalysesKeepTheFrameAndTheNote()
{
using var temp = new TempFolder();
await using var db = await TestDatabase.OpenAsync(temp);
foreach (var code in VideoAnalysisNotes.All)
{
var sha = Make.Sha(100 + VideoAnalysisNotes.All.ToList().IndexOf(code));
await db.VideoAnalyses.SetAsync(new VideoAnalysis(sha, 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, Note: code));
Assert.AreEqual(new VideoAnalysis(sha, 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, null, code), await db.VideoAnalyses.GetAsync(sha), code);
}
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, Note: " TooSlow Unreadable "));
Assert.AreEqual("TooSlow Unreadable", (await db.VideoAnalyses.GetAsync(Make.Sha(1)))!.Note, "codici separati da uno spazio");
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(2500.7), " "));
var still = await db.VideoAnalyses.GetAsync(Make.Sha(1));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(2500), null), still, "millisecondi interi, nessuna nota vuota");
await db.VideoAnalyses.SetAsync(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Moving, Make.T0));
Assert.AreEqual(new VideoAnalysis(Make.Sha(1), 1, VideoAnalysisOutcome.Moving, Make.T0), await db.VideoAnalyses.GetAsync(Make.Sha(1)), "lo stesso algoritmo toglie istante e nota");
Assert.AreEqual(1L, await ScalarAsync(db, $"SELECT COUNT(*) FROM video_analysis WHERE sha256 = '{Make.Sha(1)}'"));
var refused = new[]
{
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.Still, Make.T0),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(-1)),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.Moving, Make.T0, TimeSpan.Zero),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, TimeSpan.Zero, VideoAnalysisNotes.TooSlow),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, Note: " "),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, Note: "C:/video/a.mp4"),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.NotAnalyzable, Make.T0, Note: "Il video non si legge"),
new VideoAnalysis(Make.Sha(2), 1, VideoAnalysisOutcome.Moving, Make.T0, Note: "1Code"),
};
foreach (var analysis in refused)
{
await Assert.ThrowsExactlyAsync<ArgumentException>(() => db.VideoAnalyses.SetAsync(analysis), analysis.ToString());
}
Assert.IsNull(await db.VideoAnalyses.GetAsync(Make.Sha(2)), "nulla di rifiutato si scrive");
}
[TestMethod]
+1 -1
View File
@@ -104,7 +104,7 @@ internal sealed record ExportWorld(long Marta, long Luca, long MartaEntry, long
changes);
public static Dictionary<string, object?> AnalysisRow(params (string Column, object? Value)[] changes) => Row(
new() { ["sha256"] = Make.Sha(5), ["algorithm"] = 1L, ["outcome"] = "Still", ["analyzed_at"] = 0L },
new() { ["sha256"] = Make.Sha(5), ["algorithm"] = 1L, ["outcome"] = "Still", ["analyzed_at"] = 0L, ["frame_at_ms"] = 0L },
changes);
public Dictionary<string, object?> ConversionRow(params (string Column, object? Value)[] changes) => Row(
+68 -1
View File
@@ -37,6 +37,8 @@ public sealed class MigrationTests
Assert.AreEqual("schema", migrations[0].Name);
Assert.AreEqual("ordine_scaricamento", migrations[1].Name);
Assert.AreEqual("esportazione", migrations[2].Name);
Assert.AreEqual("esportazione_filtro", migrations[3].Name);
Assert.AreEqual("video_fermi", migrations[4].Name);
}
[TestMethod]
@@ -167,6 +169,9 @@ public sealed class MigrationTests
[TestMethod]
public async Task DatabaseAtVersion3IsUpgradedWithForeignKeysClean() => await UpgradeKeepsEverything(3);
[TestMethod]
public async Task DatabaseAtVersion4IsUpgradedWithForeignKeysClean() => await UpgradeKeepsEverything(4);
[TestMethod]
public void MigrationThreeChangesNoExistingTable()
{
@@ -213,6 +218,68 @@ public sealed class MigrationTests
StringAssert.Contains(plan, "COVERING INDEX ix_media_sha256_entry", plan);
}
/// <summary>
/// Migration 5 (tappa E4) adds to video_analysis only the columns frame_at_ms and note and the partial index of the
/// still outcomes: every other table, column and index stays the same, and the «Video fermi» filter reads the index.
/// </summary>
[TestMethod]
public void MigrationFiveOnlyAddsTheFrameTheNoteAndTheStillIndex()
{
using var temp = new TempFolder();
var path = temp.Combine("dati", FebeDatabase.FileName);
var all = MigrationRunner.LoadEmbedded();
Assert.IsGreaterThanOrEqualTo(5, all.Count);
Close(FebeDatabase.Open(path, all.Take(4).ToList()));
var tables = TablesOf(path);
var others = tables.Where(t => t != "video_analysis").ToList();
var before = SchemaOf(path, others);
var shapesBefore = TableShapes(path, others);
var columnsBefore = ColumnsOf(path, ["video_analysis"])["video_analysis"];
Close(FebeDatabase.Open(path, all.Take(5).ToList()));
CollectionAssert.AreEqual(tables, TablesOf(path), "nessuna tabella nuova");
CollectionAssert.AreEqual(before, SchemaOf(path, others), "le altre tabelle non cambiano");
CollectionAssert.AreEqual(shapesBefore, TableShapes(path, others), "colonne e indici delle altre tabelle restano gli stessi");
CollectionAssert.AreEqual(
columnsBefore.Concat(["frame_at_ms", "note"]).ToList(),
ColumnsOf(path, ["video_analysis"])["video_analysis"],
"due colonne in fondo");
var indexes = TableShapes(path, ["video_analysis"]).Where(s => s.Contains(" index ", StringComparison.Ordinal)).ToList();
CollectionAssert.Contains(indexes, "video_analysis index ix_video_analysis_still 0 c 1", string.Join(" | ", indexes));
using var connection = Connect(path);
var plan = string.Join(" | ", ReadColumn(connection,
"EXPLAIN QUERY PLAN SELECT m.entry_id FROM video_analysis va JOIN media m ON m.sha256 = va.sha256 WHERE va.outcome = 'Still'", 3));
StringAssert.Contains(plan, "ix_video_analysis_still", plan);
}
/// <summary>
/// No Febe before tappa E4 wrote video_analysis; a row written there by hand anyway does not stop migration 5: an
/// «In movimento» stays, a «Fermo» without its instant and a «Non analizzabile» without its reason go.
/// </summary>
[TestMethod]
public void MigrationFiveKeepsOnlyTheRowsItCanKeep()
{
using var temp = new TempFolder();
var path = temp.Combine("dati", FebeDatabase.FileName);
var all = MigrationRunner.LoadEmbedded();
Close(FebeDatabase.Open(path, all.Take(4).ToList()));
using (var connection = Connect(path))
{
connection.Command($"""
INSERT INTO video_analysis (sha256, algorithm, outcome, analyzed_at)
VALUES ('{Make.Sha(1)}', 1, 'Still', 1000), ('{Make.Sha(2)}', 1, 'Moving', 2000), ('{Make.Sha(3)}', 1, 'NotAnalyzable', 3000);
""").ExecuteNonQuery();
}
Close(FebeDatabase.Open(path, all));
using var after = Connect(path);
var rows = ReadColumn(after, "SELECT sha256 || ' ' || outcome || ' ' || analyzed_at || ' ' || ifnull(frame_at_ms, '-') || ' ' || ifnull(note, '-') FROM video_analysis", 0);
CollectionAssert.AreEqual(new[] { $"{Make.Sha(2)} Moving 2000 - -" }, rows);
Assert.AreEqual("ok", after.Command("PRAGMA integrity_check").ExecuteScalar());
}
[TestMethod]
public async Task DatabaseOneVersionNewerIsRefusedAndLeftUntouched()
{
@@ -251,7 +318,7 @@ public sealed class MigrationTests
await using (var db = FebeDatabase.Open(path, all))
{
Assert.AreEqual(all.Count, db.SchemaVersion);
Assert.AreEqual(4, db.SchemaVersion);
Assert.AreEqual(5, db.SchemaVersion);
}
using var connection = Connect(path);