Il prodotto prende il nome del repository, come chiesto per allineare la struttura agli altri progetti: soluzione Mimante.slnx, progetti src/Mimante, tests/Mimante.Tests e tools/Mimante.Schermate, spazi dei nomi Mimante.*, immagine alby96/mimante, modello docker/unraid/mimante.xml, compose in docker/, documenti in minuscolo (stato.md, architettura.md, come-funziona.md, schermate/), versioni dei pacchetti in Directory.Packages.props. Versione 2.0.0. Non cambiano database, tabelle, variabili d'ambiente, /config, chiave della sessione e riferimenti del sito: bidoo.default.json è identico e i nomi logici delle risorse incorporate restano quelli che le migrazioni cercano. divSaldoBidBottom è l'id di Bidoo. Compatibilità: il cookie dell'interfaccia diventa mimante_sessione (un accesso in più per browser dopo l'aggiornamento); le preferenze del browser passano da bidbot.* a mimante.* con una copia una tantum. Corretto anche il tema della pagina d'accesso, che leggeva il valore salvato senza togliere le virgolette del JSON. Tolto il codice non compilato: il riferimento del riscatto (lo sostituisce il riscatto nuovo) e il formato SQLite della versione desktop (ora la issue #31); sono leggibili al tag v1.2.0. Nuovi docs/processo.md, docs/decisioni.md, docs/docker.md (con i passi per sostituire il container su Unraid) e ADR 0014. Il Dockerfile copia Directory.Packages.props e .dockerignore esclude dati/. Build con gli avvisi come errori; 288 test verdi, i 14 sul database saltati perché Docker Desktop su questo PC non parte: girano nella CI con il suo PostgreSQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
124 lines
4.7 KiB
C#
124 lines
4.7 KiB
C#
using System.Text.Json;
|
|
using Mimante.Data;
|
|
using Mimante.Settings;
|
|
using Xunit;
|
|
|
|
namespace Mimante.Tests;
|
|
|
|
/// <summary>Migrazioni, partizioni, coda di scrittura, documenti e impostazioni su PostgreSQL 18.</summary>
|
|
[Collection("Database")]
|
|
public class DatabaseTests
|
|
{
|
|
private readonly DatabaseFixture _fx;
|
|
|
|
public DatabaseTests(DatabaseFixture fx) => _fx = fx;
|
|
|
|
[DbFact]
|
|
public async Task Le_migrazioni_sono_applicate_una_volta_sola()
|
|
{
|
|
var again = await Migrations.ApplyAsync(_fx.Db!.Writer);
|
|
Assert.Empty(again);
|
|
Assert.True(await Migrations.CurrentVersionAsync(_fx.Db) >= 1);
|
|
|
|
// Nessuna delle tabelle che la specifica esclude.
|
|
foreach (var t in new[] { "polls", "auction_log", "network_metrics", "app_sessions", "maintenance_log" })
|
|
Assert.False(await _fx.Db.ScalarAsync<bool>("SELECT to_regclass($1) IS NOT NULL", t));
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task Le_partizioni_del_mese_e_del_successivo_esistono()
|
|
{
|
|
var created = await Partitions.EnsureAsync(_fx.Db!, new DateTime(2031, 12, 5));
|
|
Assert.Equal(6, created); // dicembre 2031 e gennaio 2032, per tre tabelle
|
|
Assert.Equal(0, await Partitions.EnsureAsync(_fx.Db!, new DateTime(2031, 12, 20)));
|
|
|
|
var list = await Partitions.ListAsync(_fx.Db!, "bids");
|
|
Assert.Contains(list, p => p.Name == "bids_2032_01" && p.Month == new DateTime(2032, 1, 1));
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task La_coda_scrive_a_lotti_e_svuota_allo_spegnimento()
|
|
{
|
|
await _fx.TruncateAsync();
|
|
await using (var queue = new WriteQueue(_fx.Db!.Writer) { BatchRows = 50 })
|
|
{
|
|
for (var i = 0; i < 230; i++)
|
|
queue.EnqueueBid("INSERT INTO bids(auction_id, username, price_after, bid_type) VALUES($1, $2, $3, $4)",
|
|
"a1", $"u{i % 7}", 0.01 * (i + 1), "Auto");
|
|
|
|
Assert.True(await queue.FlushAsync(TimeSpan.FromSeconds(20)));
|
|
Assert.Equal(230, queue.Written);
|
|
}
|
|
|
|
Assert.Equal(230L, await _fx.Db.ScalarAsync<long>("SELECT COUNT(*) FROM bids WHERE auction_id = 'a1'"));
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task Un_istruzione_sbagliata_non_blocca_le_altre()
|
|
{
|
|
await _fx.TruncateAsync();
|
|
await using var queue = new WriteQueue(_fx.Db!.Writer);
|
|
queue.Enqueue("INSERT INTO risk_ledger(ts, kind, amount_euro) VALUES(now(), 'bid', -0.2)");
|
|
queue.Enqueue("INSERT INTO tabella_che_non_esiste(x) VALUES(1)");
|
|
queue.Enqueue("INSERT INTO risk_ledger(ts, kind, amount_euro) VALUES(now(), 'bid', -0.2)");
|
|
Assert.True(await queue.FlushAsync(TimeSpan.FromSeconds(20)));
|
|
|
|
Assert.Equal(1, queue.Failed);
|
|
Assert.Equal(2L, await _fx.Db.ScalarAsync<long>("SELECT COUNT(*) FROM risk_ledger"));
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task I_documenti_si_rileggono_dopo_il_riavvio()
|
|
{
|
|
await _fx.TruncateAsync();
|
|
await using (var queue = new WriteQueue(_fx.Db!.Writer))
|
|
{
|
|
var docs = new DocumentStore(queue);
|
|
docs.Set("ml/prova", """{"a":1}""");
|
|
docs.Set("ml/altro", """{"b":2}""");
|
|
docs.Set("risk/halt", """{"reason":"x"}""");
|
|
docs.DeletePrefix("ml/");
|
|
docs.Set("ml/nuovo", """{"c":3}""");
|
|
await queue.FlushAsync(TimeSpan.FromSeconds(20));
|
|
}
|
|
|
|
await using var q2 = new WriteQueue(_fx.Db.Writer);
|
|
var reloaded = new DocumentStore(q2);
|
|
await reloaded.LoadAsync(_fx.Db);
|
|
Assert.Null(reloaded.Get("ml/prova"));
|
|
Assert.NotNull(reloaded.Get("ml/nuovo"));
|
|
Assert.Contains("reason", reloaded.Get("risk/halt"));
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task Le_impostazioni_nascono_di_fabbrica_e_sopravvivono_al_riavvio()
|
|
{
|
|
await _fx.TruncateAsync();
|
|
await using var queue = new WriteQueue(_fx.Db!.Writer);
|
|
var store = new SettingsStore(queue, null);
|
|
await store.LoadAsync(_fx.Db);
|
|
Assert.Equal(300, store.Current.LearningMinAuctions);
|
|
|
|
var patch = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>("""{"DataCapGb": 20}""")!;
|
|
Assert.True((await store.SaveAsync(patch)).Ok);
|
|
|
|
var again = new SettingsStore(queue, null);
|
|
await again.LoadAsync(_fx.Db);
|
|
Assert.Equal(20, again.Current.DataCapGb);
|
|
}
|
|
}
|
|
|
|
public class WriteQueueCapacityTests
|
|
{
|
|
[Fact]
|
|
public void A_coda_piena_si_scartano_prima_le_puntate_osservate()
|
|
{
|
|
var queue = new WriteQueue(null) { Capacity = 10 };
|
|
for (var i = 0; i < 8; i++) queue.EnqueueBid("INSERT bid", i);
|
|
for (var i = 0; i < 5; i++) queue.Enqueue("INSERT altro", i);
|
|
|
|
Assert.Equal(10, queue.Pending);
|
|
Assert.Equal(3, queue.Dropped);
|
|
}
|
|
}
|