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
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>
272 lines
14 KiB
C#
272 lines
14 KiB
C#
using System.Diagnostics;
|
|
using Chronos.Data.Tests.Infrastructure;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Sessions;
|
|
|
|
/// <summary>Behaviour every <see cref="IDbSession"/> must share, run once per dialect.</summary>
|
|
public abstract class SessionTests(TestContext context) : DatabaseTest(context)
|
|
{
|
|
private string AllTypesTable => ForDialect(
|
|
"CREATE TABLE t (id INTEGER PRIMARY KEY, s TEXT, i INTEGER, l INTEGER, d REAL, b INTEGER, g TEXT, ts TEXT, ldt TEXT, bytes BLOB, j TEXT)",
|
|
"CREATE TABLE t (id integer PRIMARY KEY, s text, i integer, l bigint, d double precision, b boolean, g uuid, ts timestamptz, ldt timestamp, bytes bytea, j jsonb)");
|
|
|
|
[Test]
|
|
public async Task ParametersRoundTripEveryType()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync(AllTypesTable, Ct);
|
|
var guid = Guid.CreateVersion7();
|
|
var instant = new DateTimeOffset(2026, 9, 28, 18, 30, 15, 123, TimeSpan.FromHours(2)).AddTicks(4560);
|
|
var wallClock = new DateTime(2024, 2, 29, 23, 59, 58, 999).AddTicks(9990);
|
|
byte[] bytes = [0, 1, 2, 0x7F, 0x80, 0xFE, 0xFF, (byte)'\\', (byte)'\t'];
|
|
var text = "Città \"quoted\" 'single' \\ back\ttab\nnewline 🎞️ 時間";
|
|
await session.ExecuteAsync(
|
|
"INSERT INTO t (id, s, i, l, d, b, g, ts, ldt, bytes, j) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
|
[1, text, int.MinValue, long.MaxValue, Math.PI, true, guid, instant, wallClock, bytes, DbParam.Json("{\"a\": [1, 2]}")],
|
|
Ct);
|
|
|
|
var row = await session.QuerySingleAsync("SELECT s, i, l, d, b, g, ts, ldt, bytes, j FROM t WHERE id = $1", [1], Ct);
|
|
Assert.Equal(text, row.GetString(0));
|
|
Assert.Equal(int.MinValue, row.GetInt32(1));
|
|
Assert.Equal(long.MaxValue, row.GetInt64(2));
|
|
Assert.Equal(Math.PI, row.GetDouble(3));
|
|
Assert.True(row.GetBool(4));
|
|
Assert.Equal(guid, row.GetGuid(5));
|
|
Assert.Equal(instant, row.GetDateTimeOffset(6));
|
|
Assert.Equal(TimeSpan.Zero, row.GetDateTimeOffset(6).Offset);
|
|
Assert.Equal(wallClock, row.GetDateTime(7));
|
|
Assert.Equal(DateTimeKind.Unspecified, row.GetDateTime(7).Kind);
|
|
Assert.BytesEqual(bytes, row.GetBytes(8));
|
|
Assert.Equal("{\"a\": [1, 2]}", row.GetString(9));
|
|
Assert.Equal(text, row.GetString("S"));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TypedNullsRoundTrip()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync(AllTypesTable, Ct);
|
|
await session.ExecuteAsync(
|
|
"INSERT INTO t (id, s, i, l, d, b, g, ts, ldt, bytes, j) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
|
|
[1, (string?)null, (int?)null, (long?)null, (double?)null, (bool?)null, (Guid?)null, (DateTimeOffset?)null, (DateTime?)null, (byte[]?)null, DbParam.Json(null)],
|
|
Ct);
|
|
var row = await session.QuerySingleAsync("SELECT s, i, l, d, b, g, ts, ldt, bytes, j FROM t", [], Ct);
|
|
for (var i = 0; i < row.FieldCount; i++) Assert.True(row.IsNull(i), $"column {row.GetName(i)} should be NULL");
|
|
Assert.Null(row.GetGuidOrNull(5));
|
|
Assert.Null(row.GetDateTimeOffsetOrNull(6));
|
|
Assert.Throws<InvalidCastException>(() => row.GetInt32(1));
|
|
}
|
|
|
|
[Test]
|
|
public async Task PlaceholdersBindByNumberNotByOrder()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
var row = await session.QuerySingleAsync("SELECT $2 AS b, $1 AS a, $2 AS b2", [10, "x"], Ct);
|
|
Assert.Equal("x", row.GetString("b"));
|
|
Assert.Equal(10, row.GetInt32("a"));
|
|
Assert.Equal("x", row.GetString(2));
|
|
}
|
|
|
|
[Test]
|
|
public async Task ExecuteReportsChangedRowsOnly()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
Assert.Equal(0, await session.ExecuteAsync("CREATE TABLE n (id integer PRIMARY KEY, v integer)", [], Ct));
|
|
for (var i = 0; i < 5; i++) await session.ExecuteAsync("INSERT INTO n (id, v) VALUES ($1, $2)", [i, i % 2], Ct);
|
|
Assert.Equal(3, await session.ExecuteAsync("UPDATE n SET v = 7 WHERE v = $1", [0], Ct));
|
|
Assert.Equal(0, await session.ExecuteAsync("CREATE INDEX ix_n ON n (v)", [], Ct));
|
|
Assert.Equal(2, await session.ExecuteAsync("DELETE FROM n WHERE v = $1", [1], Ct));
|
|
Assert.Equal(3L, await session.ScalarAsync<long>("SELECT count(*) FROM n", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TransactionsCommitRollBackAndRollBackOnDispose()
|
|
{
|
|
var database = await OpenDatabaseAsync();
|
|
await using var session = await database.OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync("CREATE TABLE x (id integer PRIMARY KEY)", Ct);
|
|
|
|
await using (var tx = await session.BeginAsync(Ct))
|
|
{
|
|
Assert.True(session.InTransaction);
|
|
await session.ExecuteAsync("INSERT INTO x (id) VALUES ($1)", [1], Ct);
|
|
await tx.CommitAsync(Ct);
|
|
}
|
|
Assert.False(session.InTransaction);
|
|
|
|
await using (var tx = await session.BeginAsync(Ct))
|
|
{
|
|
await session.ExecuteAsync("INSERT INTO x (id) VALUES ($1)", [2], Ct);
|
|
await tx.RollbackAsync(Ct);
|
|
}
|
|
await using (await session.BeginAsync(Ct))
|
|
{
|
|
await session.ExecuteAsync("INSERT INTO x (id) VALUES ($1)", [3], Ct);
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => session.BeginAsync(Ct));
|
|
}
|
|
|
|
await using var other = await database.OpenAsync(Ct);
|
|
Assert.Equal(1L, await other.ScalarAsync<long>("SELECT count(*) FROM x", [], Ct));
|
|
Assert.Equal(1, await other.ScalarAsync<int>("SELECT max(id) FROM x", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task SessionDisposedInsideTransactionRollsBack()
|
|
{
|
|
var database = await OpenDatabaseAsync();
|
|
await using (var setup = await database.OpenAsync(Ct))
|
|
await setup.ExecuteScriptAsync("CREATE TABLE x (id integer PRIMARY KEY)", Ct);
|
|
var session = await database.OpenAsync(Ct);
|
|
await session.BeginAsync(Ct);
|
|
await session.ExecuteAsync("INSERT INTO x (id) VALUES ($1)", [1], Ct);
|
|
await session.DisposeAsync();
|
|
|
|
await using var check = await database.OpenAsync(Ct);
|
|
Assert.Equal(0L, await check.ScalarAsync<long>("SELECT count(*) FROM x", [], Ct));
|
|
await check.ExecuteAsync("INSERT INTO x (id) VALUES ($1)", [2], Ct);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConstraintViolationsAreTyped()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync(
|
|
"CREATE TABLE p (id integer PRIMARY KEY, code text NOT NULL UNIQUE); " +
|
|
"CREATE TABLE c (id integer PRIMARY KEY, p_id integer NOT NULL REFERENCES p(id))", Ct);
|
|
await session.ExecuteAsync("INSERT INTO p (id, code) VALUES ($1, $2)", [1, "a"], Ct);
|
|
|
|
var unique = await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync("INSERT INTO p (id, code) VALUES ($1, $2)", [2, "a"], Ct));
|
|
Assert.True(unique.IsUniqueViolation);
|
|
var primary = await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync("INSERT INTO p (id, code) VALUES ($1, $2)", [1, "b"], Ct));
|
|
Assert.True(primary.IsUniqueViolation);
|
|
var foreign = await Assert.ThrowsAsync<DatabaseException>(() => session.ExecuteAsync("INSERT INTO c (id, p_id) VALUES ($1, $2)", [1, 99], Ct));
|
|
Assert.True(foreign.IsForeignKeyViolation);
|
|
|
|
// The session stays usable after errors.
|
|
Assert.Equal(1L, await session.ScalarAsync<long>("SELECT count(*) FROM p", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task BulkInsertKeepsSpecialCharactersAndNulls()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync(ForDialect(
|
|
"CREATE TABLE b (id INTEGER PRIMARY KEY, s TEXT, bytes BLOB, ts TEXT, ldt TEXT, d REAL, g TEXT, f INTEGER)",
|
|
"CREATE TABLE b (id integer PRIMARY KEY, s text, bytes bytea, ts timestamptz, ldt timestamp, d double precision, g uuid, f boolean)"), Ct);
|
|
string?[] texts =
|
|
[
|
|
"tab\there", "new\nline", "carriage\rreturn", "back\\slash", "\\N", "", null, "unicode ✓ ñ 漢字 🎉",
|
|
"mixed \\t\t\\n\n end\\", "trailing tab\t", "x",
|
|
];
|
|
var instant = new DateTimeOffset(2025, 1, 2, 3, 4, 5, TimeSpan.Zero);
|
|
var rows = texts.Select((s, i) => new object?[]
|
|
{
|
|
i, s, i % 3 == 0 ? null : new byte[] { (byte)i, (byte)'\\', (byte)'\n', 0 }, instant.AddDays(i),
|
|
new DateTime(2020, 1, 1).AddHours(i), i / 3.0, Guid.CreateVersion7(), i % 2 == 0,
|
|
}).ToList();
|
|
|
|
var inserted = await session.BulkInsertAsync("b", ["id", "s", "bytes", "ts", "ldt", "d", "g", "f"], rows.ToAsyncEnumerable(), Ct);
|
|
Assert.Equal(texts.Length, (int)inserted);
|
|
|
|
var read = await session.QueryListAsync("SELECT id, s, bytes, ts, ldt, d, g, f FROM b ORDER BY id", [], r => r, Ct);
|
|
Assert.Equal(texts.Length, read.Count);
|
|
for (var i = 0; i < texts.Length; i++)
|
|
{
|
|
var r = read[i];
|
|
Assert.Equal(texts[i], r.GetStringOrNull(1), $"row {i}");
|
|
if (rows[i][2] is byte[] expected) Assert.BytesEqual(expected, r.GetBytes(2));
|
|
else Assert.True(r.IsNull(2));
|
|
Assert.Equal(instant.AddDays(i), r.GetDateTimeOffset(3));
|
|
Assert.Equal(new DateTime(2020, 1, 1).AddHours(i), r.GetDateTime(4));
|
|
Assert.Equal(i / 3.0, r.GetDouble(5));
|
|
Assert.Equal((Guid)rows[i][6]!, r.GetGuid(6));
|
|
Assert.Equal(i % 2 == 0, r.GetBool(7));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task FailingBulkProducerLeavesTableUntouched()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync("CREATE TABLE b (id integer PRIMARY KEY)", Ct);
|
|
async IAsyncEnumerable<object?[]> Rows()
|
|
{
|
|
for (var i = 0; i < 1000; i++)
|
|
{
|
|
if (i == 700) throw new InvalidDataException("producer failed");
|
|
yield return [i];
|
|
await Task.Yield();
|
|
}
|
|
}
|
|
await Assert.ThrowsAsync<InvalidDataException>(() => session.BulkInsertAsync("b", ["id"], Rows(), Ct));
|
|
Assert.Equal(0L, await session.ScalarAsync<long>("SELECT count(*) FROM b", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AbandonedQueryLeavesSessionUsable()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync("CREATE TABLE q (id integer PRIMARY KEY)", Ct);
|
|
await session.BulkInsertAsync("q", ["id"], Enumerable.Range(0, 2000).Select(i => new object?[] { i }).ToAsyncEnumerable(), Ct);
|
|
await foreach (var row in session.QueryAsync("SELECT id FROM q ORDER BY id", [], Ct))
|
|
{
|
|
Assert.Equal(0, row.GetInt32(0));
|
|
break;
|
|
}
|
|
Assert.Equal(1999, await session.ScalarAsync<int>("SELECT max(id) FROM q", [], Ct));
|
|
Assert.Equal(2000L, await session.ScalarAsync<long>("SELECT count(*) FROM q", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task SingleRowHelpersEnforceCardinality()
|
|
{
|
|
await using var session = await (await OpenDatabaseAsync()).OpenAsync(Ct);
|
|
await session.ExecuteScriptAsync("CREATE TABLE s (id integer PRIMARY KEY); INSERT INTO s (id) VALUES (1); INSERT INTO s (id) VALUES (2)", Ct);
|
|
Assert.Null(await session.QuerySingleOrDefaultAsync("SELECT id FROM s WHERE id = $1", [9], Ct));
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => session.QuerySingleAsync("SELECT id FROM s WHERE id = $1", [9], Ct));
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => session.QuerySingleOrDefaultAsync("SELECT id FROM s", [], Ct));
|
|
Assert.Equal(0, await session.ScalarAsync<int>("SELECT id FROM s WHERE id = $1", [9], Ct));
|
|
Assert.Null(await session.ScalarAsync<int?>("SELECT id FROM s WHERE id = $1", [9], Ct));
|
|
Assert.Equal("2", await session.ScalarAsync<string>("SELECT max(id) FROM s", [], Ct));
|
|
}
|
|
|
|
[Test]
|
|
public async Task CancellationStopsALongQueryQuickly()
|
|
{
|
|
var database = await OpenDatabaseAsync();
|
|
await using var session = await database.OpenAsync(Ct);
|
|
var sql = ForDialect(
|
|
"WITH RECURSIVE r(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM r WHERE n < 2000000000) SELECT count(*) FROM r",
|
|
"SELECT pg_sleep(10)");
|
|
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
|
|
var watch = Stopwatch.StartNew();
|
|
var cancelledAt = TimeSpan.Zero;
|
|
// A dedicated thread, not CancelAfter: a timer on a busy thread pool can fire late and skew the measurement.
|
|
var canceller = new Thread(() =>
|
|
{
|
|
Thread.Sleep(300);
|
|
cancelledAt = watch.Elapsed;
|
|
cts.Cancel();
|
|
})
|
|
{ IsBackground = true };
|
|
canceller.Start();
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => session.ExecuteAsync(sql, [], cts.Token));
|
|
var stoppedAt = watch.Elapsed;
|
|
canceller.Join();
|
|
Assert.Less((stoppedAt - cancelledAt).TotalSeconds, 1.0, "cancellation latency");
|
|
Assert.Equal(1, await session.ScalarAsync<int>("SELECT 1", [], Ct));
|
|
}
|
|
}
|
|
|
|
public sealed class SqliteSessionTests(TestContext context) : SessionTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.SqliteMemory;
|
|
}
|
|
|
|
[RequiresEnvironment(PgTestServer.Variable)]
|
|
public sealed class PostgresSessionTests(TestContext context) : SessionTests(context)
|
|
{
|
|
protected override Backend Backend => Backend.Postgres;
|
|
}
|