Il servizio stava in Mimante/ con, alla radice, il README e il .gitignore della vecchia app WPF. Come negli altri progetti (Febe), soluzione, sorgenti, documenti e Docker stanno ora alla radice. Solo spostamenti con git mv, così la storia di ogni file si segue con --follow; nessun contenuto cambia. Tolti: il README della app WPF (quello del bot prende il suo posto), Modifiche.txt (vuoto) e i due flussi di .gitea/workflows, che non sono mai partiti e pubblicavano la porta 5432 sul Docker dove gira il PostgreSQL vero: li sostituisce la CI nuova. Fino ad allora nessun flusso parte. .gitignore unico, preso da Febe, con le voci del bot più /dati/ (i dati scaricati per l'analisi) e .claude/worktrees/. Build e 288 test verdi dalla radice (14 sul database saltati senza TEST_DB_HOST). 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 BidooBidBot.Data;
|
|
using Npgsql;
|
|
using Xunit;
|
|
|
|
namespace BidooBidBot.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") ?? "bidbot_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(), "bidbot-test-" + Guid.NewGuid().ToString("N")[..10]);
|
|
|
|
public TempDir() => Directory.CreateDirectory(Path);
|
|
|
|
public void Dispose()
|
|
{
|
|
try { Directory.Delete(Path, true); } catch { }
|
|
}
|
|
}
|