CI / Build and test (SQLite) (pull_request) Successful in 45s
CI / PostgreSQL 16 integration (pull_request) Skipped
CI / PostgreSQL 17 integration (pull_request) Skipped
CI / YAML check (pull_request) Successful in 3s
CI / Docker image (pull_request) Successful in 51s
Every later milestone stores its data through this layer, so it has to behave the same on both engines and be safe by construction: - Khaemhat.Data: explicit entity maps, typed queries over a safe expression subset, SqlFragment where every interpolated value becomes a parameter, unit of work with transactions, repositories with audit columns and optimistic concurrency on row_version, retries on transient errors. - SQLite provider (STRICT tables, WAL, exact decimals as integers scaled by 10^6, Unicode case-insensitive LIKE, serialised writes) and PostgreSQL 16+ provider (numeric(19,6), timestamptz, jsonb, advisory locks, database creation and server info). Databases are configured from postgres:// and sqlite:// URLs whose password never appears in messages. - C# migrations with system and company catalogues, schema_version with checksums, one transaction per migration under a lock, refusal of unknown applied versions; first system tables sys_companies and sys_settings. - CompanyRegistry stores each company's database URL encrypted with the instance key (AES-256-GCM, HKDF-derived key, purpose as associated data), so a copied row cannot be read for another company. - tools/Khaemhat.DataModelTool, scripts/gen-datamodel.sh and scripts/new-migration.sh: the current schema section of docs/DATA-MODEL.md is generated from the migrations and a test fails when it is stale. - C# 14 binds array.Contains to MemoryExtensions over spans; the query translator unwraps the span conversion. - Tests run every data scenario on SQLite and PostgreSQL (PostgreSQL in CI); ADR 0018 records where the contracts differ from SPEC 5.1. Docs, wiki, changelog and status updated. Closes #7, #8, #9, #10, #11, #12, #13, #14 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
239 lines
11 KiB
C#
239 lines
11 KiB
C#
using Khaemhat.Data.Querying;
|
|
using Khaemhat.Data.Schema;
|
|
using Khaemhat.Data.Sqlite;
|
|
using Khaemhat.Data.Tests.Support;
|
|
|
|
namespace Khaemhat.Data.Tests;
|
|
|
|
/// <summary>The same behaviour on both engines: types, decimals, paging, upsert, LIKE, timestamps.</summary>
|
|
[TestClass]
|
|
public sealed class DialectTests
|
|
{
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task EveryType_RoundTrips(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
var at = new DateTime(2026, 11, 11, 9, 30, 15, DateTimeKind.Utc).AddTicks(1234560);
|
|
var item = new TestItem
|
|
{
|
|
Code = "ALL",
|
|
Name = "Caffè «speciale» ✓",
|
|
Quantity = int.MaxValue,
|
|
Big = long.MinValue,
|
|
Amount = -123_456_789_012.345678m,
|
|
Active = true,
|
|
Day = new DateOnly(2026, 2, 28),
|
|
At = at,
|
|
Uid = Guid.Parse("7a1b0c3e-9d2f-4e5a-8b6c-1d2e3f4a5b6c"),
|
|
Data = [0, 1, 2, 254, 255],
|
|
Payload = "{\"a\": 1}",
|
|
Kind = TestKind.Kit,
|
|
IsDemo = true,
|
|
};
|
|
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
var items = unitOfWork.Repository<TestItem, long>();
|
|
var id = await items.InsertAsync(item);
|
|
var read = await items.GetAsync(id);
|
|
|
|
Assert.IsNotNull(read);
|
|
Assert.AreEqual(item.Name, read.Name);
|
|
Assert.AreEqual(int.MaxValue, read.Quantity);
|
|
Assert.AreEqual(long.MinValue, read.Big);
|
|
Assert.AreEqual(item.Amount, read.Amount);
|
|
Assert.IsTrue(read.Active);
|
|
Assert.AreEqual(item.Day, read.Day);
|
|
Assert.AreEqual(at, read.At);
|
|
Assert.AreEqual(DateTimeKind.Utc, read.At!.Value.Kind);
|
|
Assert.AreEqual(item.Uid, read.Uid);
|
|
CollectionAssert.AreEqual(item.Data, read.Data);
|
|
StringAssert.Contains(read.Payload, "\"a\"");
|
|
Assert.AreEqual(TestKind.Kit, read.Kind);
|
|
Assert.IsTrue(read.IsDemo);
|
|
Assert.AreEqual(TestDatabase.TestUser, read.CreatedBy);
|
|
Assert.AreEqual(1L, read.RowVersion);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Decimals_AreRoundedToSixPlaces_AndSumsAreExact(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
var items = unitOfWork.Repository<TestItem, long>();
|
|
|
|
decimal[] amounts = [999_999_999_999.999999m, 0.000001m, 1.2345675m, -1.0000005m, 0.1m, 0.2m];
|
|
await unitOfWork.InTransactionAsync(async ct =>
|
|
{
|
|
for (var i = 0; i < amounts.Length; i++)
|
|
{
|
|
await items.InsertAsync(new TestItem { Code = "D" + i, Amount = amounts[i] }, ct);
|
|
}
|
|
});
|
|
|
|
var stored = (await items.ListAsync(Query.For<TestItem>().OrderBy(x => x.Id))).Items.Select(x => x.Amount).ToArray();
|
|
CollectionAssert.AreEqual(new[] { 999_999_999_999.999999m, 0.000001m, 1.234568m, -1.000001m, 0.1m, 0.2m }, stored);
|
|
|
|
var sum = await unitOfWork.QuerySingleAsync<decimal>(SqlFragment.Literal("SELECT SUM(amount) FROM test_items"));
|
|
Assert.AreEqual(1_000_000_000_000.534567m, sum);
|
|
|
|
var small = await unitOfWork.QuerySingleAsync<decimal?>(SqlFragment.Of($"SELECT SUM(amount) FROM test_items WHERE amount < {1m}"));
|
|
Assert.AreEqual(-0.7m, small);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SqliteDecimalConverter_RoundsHalfAwayFromZero_AndRefusesOverflow()
|
|
{
|
|
Assert.AreEqual(1_234_568L, SqliteDecimalConverter.ToScaled(1.2345675m));
|
|
Assert.AreEqual(-1_000_001L, SqliteDecimalConverter.ToScaled(-1.0000005m));
|
|
Assert.AreEqual(1_000_000_000_000_000_000L, SqliteDecimalConverter.ToScaled(1_000_000_000_000m));
|
|
Assert.AreEqual(123.456789m, SqliteDecimalConverter.FromScaled(123_456_789L));
|
|
Assert.ThrowsExactly<OverflowException>(() => SqliteDecimalConverter.ToScaled(10_000_000_000_000m));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Like_IsCaseInsensitive_UnicodeAware_AndEscaped(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
var items = unitOfWork.Repository<TestItem, long>();
|
|
string[] names = ["Àlbero", "albero", "ALBERGO", "100% Bio", "a_b", "axb"];
|
|
for (var i = 0; i < names.Length; i++)
|
|
{
|
|
await items.InsertAsync(new TestItem { Code = "L" + i, Name = names[i] });
|
|
}
|
|
|
|
async Task<string[]> Names(Query<TestItem> query) =>
|
|
[.. (await items.ListAsync(query.OrderBy(x => x.Code))).Items.Select(x => x.Name!)];
|
|
|
|
CollectionAssert.AreEqual(new[] { "Àlbero" }, await Names(Query.For<TestItem>().Where(x => x.Name!.Contains("àL"))));
|
|
CollectionAssert.AreEqual(new[] { "albero", "ALBERGO" }, await Names(Query.For<TestItem>().Where(x => x.Name!.StartsWith("alb"))));
|
|
CollectionAssert.AreEqual(new[] { "100% Bio" }, await Names(Query.For<TestItem>().Where(x => x.Name!.Contains("%"))));
|
|
CollectionAssert.AreEqual(new[] { "a_b" }, await Names(Query.For<TestItem>().Where(x => x.Name!.Contains("_"))));
|
|
CollectionAssert.AreEqual(new[] { "Àlbero", "albero" }, await Names(Query.For<TestItem>().Where(x => x.Name!.EndsWith("BERO"))));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Timestamps_SortChronologically_AndCurrentTimestampIsUtc(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
var items = unitOfWork.Repository<TestItem, long>();
|
|
var baseTime = new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
TimeSpan[] offsets = [TimeSpan.FromHours(10), TimeSpan.FromMilliseconds(1), TimeSpan.FromDays(400), TimeSpan.FromTicks(10), TimeSpan.Zero];
|
|
for (var i = 0; i < offsets.Length; i++)
|
|
{
|
|
await items.InsertAsync(new TestItem { Code = "T" + i, At = baseTime + offsets[i] });
|
|
}
|
|
|
|
var sorted = (await items.ListAsync(Query.For<TestItem>().OrderBy(x => x.At))).Items.Select(x => x.Code).ToArray();
|
|
var after = (await items.ListAsync(Query.For<TestItem>().Where(x => x.At > baseTime).OrderBy(x => x.At))).Items.Select(x => x.Code).ToArray();
|
|
var now = await unitOfWork.QuerySingleAsync<DateTime>(SqlFragment.Literal("SELECT " + db.Dialect.CurrentTimestampUtc));
|
|
|
|
CollectionAssert.AreEqual(new[] { "T4", "T3", "T1", "T0", "T2" }, sorted);
|
|
CollectionAssert.AreEqual(new[] { "T3", "T1", "T0", "T2" }, after);
|
|
Assert.AreEqual(DateTimeKind.Utc, now.Kind);
|
|
Assert.IsLessThan(TimeSpan.FromMinutes(1), (now - DateTime.UtcNow).Duration());
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Paging_ReturnsStablePagesAndTotal(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
var items = unitOfWork.Repository<TestItem, long>();
|
|
await unitOfWork.InTransactionAsync(async ct =>
|
|
{
|
|
for (var i = 0; i < 25; i++)
|
|
{
|
|
await items.InsertAsync(new TestItem { Code = "P" + i.ToString("D2", System.Globalization.CultureInfo.InvariantCulture), Quantity = i }, ct);
|
|
}
|
|
});
|
|
|
|
var page = await items.ListAsync(Query.For<TestItem>().Where(x => x.Quantity >= 5).OrderBy(x => x.Code).Page(10, 4));
|
|
|
|
Assert.AreEqual(20, page.TotalCount);
|
|
CollectionAssert.AreEqual(new[] { "P15", "P16", "P17", "P18" }, page.Items.Select(x => x.Code).ToArray());
|
|
Assert.IsTrue(page.HasMore);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task Upsert_InsertsThenUpdates(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
await unitOfWork.ExecuteAsync(SqlFragment.Literal(string.Join(";", db.Provider.Ddl.Generate(UpsertTable()))));
|
|
var text = db.Dialect.Upsert("kv", ["k", "v"], ["@p0", "@p1"], ["k"], ["v"]);
|
|
|
|
foreach (var value in new[] { "first", "second" })
|
|
{
|
|
await using var command = unitOfWork.Executor.CreateCommand(
|
|
unitOfWork.Connection, null, text, [new SqlParameterValue("p0", "key", ColumnType.Text), new SqlParameterValue("p1", value, ColumnType.Text)]);
|
|
await command.ExecuteNonQueryAsync();
|
|
}
|
|
|
|
Assert.AreEqual(1L, await unitOfWork.QuerySingleAsync<long>(SqlFragment.Literal("SELECT COUNT(*) FROM kv")));
|
|
Assert.AreEqual("second", await unitOfWork.QuerySingleAsync<string>(SqlFragment.Literal("SELECT v FROM kv")));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task StringLength_IsEnforcedOnBothEngines(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
await db.CreateTestTableAsync();
|
|
await using var unitOfWork = await db.OpenAsync();
|
|
|
|
await Assert.ThrowsAsync<System.Data.Common.DbException>(
|
|
() => unitOfWork.Repository<TestItem, long>().InsertAsync(new TestItem { Code = new string('x', 21) }));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow(TestDatabase.Sqlite)]
|
|
[DataRow(TestDatabase.Postgres)]
|
|
public async Task ServerInfo_IsReported(string engine)
|
|
{
|
|
await using var db = await TestDatabase.CreateAsync(engine);
|
|
|
|
var info = await db.Provider.GetServerInfoAsync(db.Target.ConnectionString);
|
|
|
|
Assert.AreEqual(db.Provider.Engine, info.Engine);
|
|
Assert.IsGreaterThan(0, info.VersionNumber);
|
|
if (engine == TestDatabase.Postgres)
|
|
{
|
|
Assert.IsGreaterThanOrEqualTo(160000, info.VersionNumber, "CI runs PostgreSQL 16 and 17");
|
|
Assert.AreEqual(db.Url.Database, info.Database);
|
|
Assert.IsEmpty(info.Warnings);
|
|
}
|
|
}
|
|
|
|
private static IReadOnlyList<SchemaOperation> UpsertTable()
|
|
{
|
|
var schema = new SchemaBuilder();
|
|
schema.CreateTable("kv", t =>
|
|
{
|
|
t.String("k", 50).NotNull();
|
|
t.Text("v");
|
|
t.PrimaryKey("k");
|
|
});
|
|
return schema.Operations;
|
|
}
|
|
}
|