Files
Chronos/tests/Chronos.Data.Tests/Infrastructure/DatabaseTest.cs
T
Alby96andClaude Opus 5.5 5f27a86406
CI / Build and test (pull_request) Failing after 2m3s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / Docker image (pull_request) Skipped
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
refactor!: rename Titano to Chronos and move CI and releases to Gitea
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout. The release cross-compiles the Windows programs, checks their
  icon, version and DPI manifest, packs installers and zips, publishes the
  Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only
  tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow) and
  pe-check.

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 11:34:48 +02:00

110 lines
4.7 KiB
C#

using Chronos.Data.Postgres;
using Chronos.Testing;
namespace Chronos.Data.Tests.Infrastructure;
/// <summary>Engine a dialect-parameterised test class runs against.</summary>
public enum Backend
{
SqliteMemory,
SqliteFile,
Postgres,
}
/// <summary>
/// The PostgreSQL server named by <c>CHRONOS_TEST_PG</c>. Every test gets its own schema (selected through
/// <c>Search Path</c>), so classes run in parallel without seeing each other's tables.
/// </summary>
internal static class PgTestServer
{
public const string Variable = "CHRONOS_TEST_PG";
private static readonly Lazy<PostgresDatabase> Admin = new(() =>
new PostgresDatabase(PostgresSettings.Parse(ConnectionString) with { MaxPoolSize = 4 }));
public static string ConnectionString =>
Environment.GetEnvironmentVariable(Variable) is { Length: > 0 } cs ? cs : throw new SkipException($"{Variable} not set");
/// <summary>A connection string for another role (MD5, cleartext, trust tests), or a skip when not configured.</summary>
public static string Optional(string variable) =>
Environment.GetEnvironmentVariable(variable) is { Length: > 0 } cs ? cs : throw new SkipException($"{variable} not set");
/// <summary>Set to 1 where the server was set up with TLS (CI): a server that refuses TLS then fails the TLS tests.</summary>
public const string TlsVariable = "CHRONOS_TEST_PG_TLS";
public static bool TlsExpected => Environment.GetEnvironmentVariable(TlsVariable) is "1" or "true";
public static async Task<string> CreateSchemaAsync(CancellationToken cancellationToken)
{
var name = "chronos_t_" + Guid.NewGuid().ToString("N")[..16];
await using var session = await Admin.Value.OpenAsync(cancellationToken);
await session.ExecuteScriptAsync($"CREATE SCHEMA {name}", cancellationToken);
return name;
}
public static async Task DropSchemaAsync(string name)
{
await using var session = await Admin.Value.OpenAsync(CancellationToken.None);
await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {name} CASCADE", CancellationToken.None);
}
public static string With(string connectionString, string extra) => connectionString.TrimEnd(';', ' ') + ";" + extra;
}
/// <summary>
/// Base of tests that run against a fresh database per test: an in-memory or temporary-file SQLite database, or
/// a private schema on the PostgreSQL test server. Everything opened through it is disposed after the test.
/// </summary>
public abstract class DatabaseTest(TestContext context) : IAsyncDisposable
{
private readonly List<IAsyncDisposable> _owned = [];
private readonly List<string> _schemas = [];
protected TestContext Context => context;
protected CancellationToken Ct => context.Cancellation;
protected abstract Backend Backend { get; }
protected Dialect Dialect => Backend == Backend.Postgres ? Dialect.Postgres : Dialect.Sqlite;
/// <summary>Picks the DDL/SQL variant of the current dialect.</summary>
protected string ForDialect(string sqlite, string postgres) => Dialect == Dialect.Postgres ? postgres : sqlite;
/// <summary>Options for a new empty database; <paramref name="multiConnection"/> forces a SQLite file (pooled readers).</summary>
protected async Task<DatabaseOptions> NewOptionsAsync(bool multiConnection = false, string? extraPostgres = null)
{
switch (Backend)
{
case Backend.SqliteMemory when !multiConnection:
return DatabaseOptions.SqliteInMemory();
case Backend.SqliteMemory or Backend.SqliteFile:
return DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory(), "chronos.db"));
default:
var schema = await PgTestServer.CreateSchemaAsync(Ct);
_schemas.Add(schema);
var cs = PgTestServer.With(PgTestServer.ConnectionString, $"Search Path={schema};Maximum Pool Size=12");
return DatabaseOptions.Postgres(extraPostgres is null ? cs : PgTestServer.With(cs, extraPostgres));
}
}
/// <summary>Opens a bare database (no migrations).</summary>
protected async Task<IDatabase> OpenDatabaseAsync(bool multiConnection = false)
{
var database = DatabaseFactory.Open(await NewOptionsAsync(multiConnection));
return Own(database);
}
protected T Own<T>(T disposable) where T : IAsyncDisposable
{
_owned.Add(disposable);
return disposable;
}
public async ValueTask DisposeAsync()
{
for (var i = _owned.Count - 1; i >= 0; i--) await _owned[i].DisposeAsync();
foreach (var schema in _schemas) await PgTestServer.DropSchemaAsync(schema);
}
}