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>
106 lines
3.7 KiB
C#
106 lines
3.7 KiB
C#
using System.Globalization;
|
|
using System.Runtime.CompilerServices;
|
|
using Mimante.Data;
|
|
using Npgsql;
|
|
using Xunit;
|
|
|
|
namespace Mimante.Tests;
|
|
|
|
/// <summary>Numeri all'italiana anche nei test: i messaggi del bot si leggono così.</summary>
|
|
internal static class TestCulture
|
|
{
|
|
[ModuleInitializer]
|
|
internal static void Init()
|
|
{
|
|
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("it-IT");
|
|
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo("it-IT");
|
|
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("it-IT");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Il PostgreSQL dei test, da <c>TEST_DB_HOST</c> (più <c>TEST_DB_USER</c>,
|
|
/// <c>TEST_DB_PASSWORD</c>, <c>TEST_DB_NAME</c>). Senza la variabile i test su database
|
|
/// saltano: in locale basta il compose di <c>tools/dev</c>, nel workflow c'è il servizio.
|
|
/// </summary>
|
|
public static class TestDb
|
|
{
|
|
public static string? Host => Environment.GetEnvironmentVariable("TEST_DB_HOST") is { Length: > 0 } h ? h : null;
|
|
|
|
public static DbConfig Config => new(
|
|
Host ?? "localhost",
|
|
5432,
|
|
Environment.GetEnvironmentVariable("TEST_DB_USER") ?? "admin",
|
|
Environment.GetEnvironmentVariable("TEST_DB_PASSWORD") ?? "dev",
|
|
Environment.GetEnvironmentVariable("TEST_DB_NAME") ?? "mimante_test");
|
|
}
|
|
|
|
/// <summary>Un test su PostgreSQL: salta se <c>TEST_DB_HOST</c> non è impostata.</summary>
|
|
public sealed class DbFactAttribute : FactAttribute
|
|
{
|
|
public DbFactAttribute()
|
|
{
|
|
if (TestDb.Host == null) Skip = "TEST_DB_HOST non impostata: test su PostgreSQL saltato";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Database di prova pulito, una volta per esecuzione: si cancella e si ricrea, poi si
|
|
/// applicano le migrazioni. I test della collezione «Database» girano uno alla volta.
|
|
/// </summary>
|
|
public sealed class DatabaseFixture : IAsyncLifetime
|
|
{
|
|
public Db? Db { get; private set; }
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
if (TestDb.Host == null) return;
|
|
|
|
var cfg = TestDb.Config;
|
|
await using (var admin = new NpgsqlConnection(cfg.Builder("postgres").ConnectionString))
|
|
{
|
|
await admin.OpenAsync();
|
|
var name = cfg.Database.Replace("\"", "\"\"", StringComparison.Ordinal);
|
|
await using (var drop = new NpgsqlCommand($"DROP DATABASE IF EXISTS \"{name}\" WITH (FORCE)", admin))
|
|
await drop.ExecuteNonQueryAsync();
|
|
await using (var create = new NpgsqlCommand($"CREATE DATABASE \"{name}\"", admin))
|
|
await create.ExecuteNonQueryAsync();
|
|
}
|
|
|
|
Db = new Db(cfg);
|
|
await Migrations.ApplyAsync(Db.Writer);
|
|
await Partitions.EnsureAsync(Db, DateTime.UtcNow);
|
|
}
|
|
|
|
public async Task DisposeAsync()
|
|
{
|
|
if (Db != null) await Db.DisposeAsync();
|
|
}
|
|
|
|
/// <summary>Svuota le tabelle fra un test e l'altro (le partizioni restano).</summary>
|
|
public async Task TruncateAsync()
|
|
{
|
|
await Db!.ExecuteAsync(
|
|
"TRUNCATE settings, documents, items, auctions, bids, resets, my_bids, bot_decisions, auction_forecasts, " +
|
|
"bidders, risk_ledger, products, watched_products, product_stats RESTART IDENTITY CASCADE");
|
|
}
|
|
}
|
|
|
|
[CollectionDefinition("Database")]
|
|
public sealed class DatabaseCollection : ICollectionFixture<DatabaseFixture>
|
|
{
|
|
}
|
|
|
|
/// <summary>Cartella temporanea che si cancella da sola: i test non scrivono mai altrove.</summary>
|
|
public sealed class TempDir : IDisposable
|
|
{
|
|
public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "mimante-test-" + Guid.NewGuid().ToString("N")[..10]);
|
|
|
|
public TempDir() => Directory.CreateDirectory(Path);
|
|
|
|
public void Dispose()
|
|
{
|
|
try { Directory.Delete(Path, true); } catch { }
|
|
}
|
|
}
|