La finestra «Aggiungi asta» mandava sempre un modo (Osserva per primo), e il server lo usava prima di tutto il resto: un'aggiunta a mano ignorava sia il modo scritto sulla scheda del prodotto sia «Modo delle aste aggiunte a mano», che pure il suo suggerimento prometteva. Ora la prima scelta, proposta, è «Predefinito» e manda mode null: il server applica il modo della scheda e, se manca, l'impostazione (l'ordine in ProductRules.Resolve era già giusto, ora è commentato e provato da un test puro e da uno sul monitor contro il simulatore). Il suggerimento di «Predefinito» dice quale modo vale adesso, perché il messaggio mode porta il valore dell'impostazione (newAuctionMode). Il catalogo continua a mandare il suo modo. I suggerimenti della colonna Stato dei Prodotti dicono che quel modo vale anche per le aste aggiunte con il + in Predefinito. «Tema scuro» valeva solo per il browser da cui si salvava, «Menu compresso» arrivava al browser ma nessuno lo leggeva: si tolgono da AppSettings, dal messaggio mode e dalla pagina Impostazioni (la sezione diventa «Registri»). Tema e menu restano ai loro bottoni, ricordati da ogni browser. Una riga delle impostazioni che contiene ancora le due chiavi si legge lo stesso (chiavi sconosciute ignorate): lo provano un test puro su SettingsStore.Parse e uno su PostgreSQL. Lo scostamento dalla specifica §15 è annotato in fondo all'ADR 0016; guida aggiornata in Aste, Impostazioni, Cerca e prodotti e Primo avvio. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 68ab6fdee7f38eb57584c4c60476e74034198fe9)
144 lines
5.8 KiB
C#
144 lines
5.8 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);
|
|
}
|
|
|
|
[DbFact]
|
|
public async Task Una_riga_delle_impostazioni_con_tema_e_menu_compresso_si_carica_lo_stesso()
|
|
{
|
|
// Le chiavi DarkMode e NavCollapsed non esistono più (#48): un database che le ha ancora parte.
|
|
await _fx.TruncateAsync();
|
|
await _fx.Db!.ExecuteAsync("INSERT INTO settings(id, value) VALUES(1, $1) ON CONFLICT(id) DO UPDATE SET value = excluded.value",
|
|
new Jsonb($$"""{"SettingsSchemaVersion": {{SettingsStore.CurrentSchema}}, "DarkMode": true, "NavCollapsed": true, "DataCapGb": 33}"""));
|
|
await using var queue = new WriteQueue(_fx.Db.Writer);
|
|
var store = new SettingsStore(queue, null);
|
|
await store.LoadAsync(_fx.Db);
|
|
Assert.Equal(33, store.Current.DataCapGb);
|
|
|
|
// Al primo salvataggio la riga si riscrive senza le due chiavi.
|
|
var patch = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>("""{"DataCapGb": 34}""")!;
|
|
Assert.True((await store.SaveAsync(patch)).Ok);
|
|
var json = await _fx.Db.ScalarAsync<string>("SELECT value::text FROM settings WHERE id = 1");
|
|
Assert.DoesNotContain("DarkMode", json);
|
|
Assert.DoesNotContain("NavCollapsed", json);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|