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>
113 lines
4.0 KiB
C#
113 lines
4.0 KiB
C#
using Khaemhat.Data.PostgreSql;
|
|
using Khaemhat.Data.Querying;
|
|
using Khaemhat.Data.Sqlite;
|
|
using Khaemhat.Data.Tests.Support;
|
|
|
|
namespace Khaemhat.Data.Tests;
|
|
|
|
[TestClass]
|
|
public sealed class QueryTranslationTests
|
|
{
|
|
private static readonly TestItemMap Map = CreateMap();
|
|
|
|
[TestMethod]
|
|
public void Comparisons_AndLogic_UseParameters()
|
|
{
|
|
var min = 5;
|
|
var code = "A'1";
|
|
var sql = Where(Query.For<TestItem>().Where(x => (x.Quantity >= min && x.Code != code) || !x.Active));
|
|
|
|
Assert.AreEqual(" WHERE ((\"quantity\" >= @p0 AND \"code\" <> @p1) OR NOT (\"active\" = @p2))", sql.Text);
|
|
CollectionAssert.AreEqual(new object?[] { 5, "A'1", true }, sql.Parameters.Select(p => p.Value).ToArray());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ValueOnTheLeft_IsMirrored()
|
|
{
|
|
var sql = Where(Query.For<TestItem>().Where(x => 10 < x.Quantity));
|
|
|
|
Assert.AreEqual(" WHERE \"quantity\" > @p0", sql.Text);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Null_BecomesIsNull()
|
|
{
|
|
string? none = null;
|
|
var sql = Where(Query.For<TestItem>().Where(x => x.Name == none && x.Day != null));
|
|
|
|
Assert.AreEqual(" WHERE (\"name\" IS NULL AND \"day\" IS NOT NULL)", sql.Text);
|
|
Assert.IsEmpty(sql.Parameters);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Enums_AreComparedByName()
|
|
{
|
|
var sql = Where(Query.For<TestItem>().Where(x => x.Kind == TestKind.Service));
|
|
|
|
Assert.AreEqual("Service", sql.Parameters[0].Value);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void In_FromListContains_AndEmptyListIsFalse()
|
|
{
|
|
var codes = new List<string> { "A", "B" };
|
|
var sql = Where(Query.For<TestItem>().Where(x => codes.Contains(x.Code)));
|
|
var none = Where(Query.For<TestItem>().Where(x => Array.Empty<long>().Contains(x.Id)));
|
|
|
|
Assert.AreEqual(" WHERE \"code\" IN (@p0, @p1)", sql.Text);
|
|
Assert.AreEqual(" WHERE 1 = 0", none.Text);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Like_IsCaseInsensitiveAndEscaped_PerDialect()
|
|
{
|
|
var query = Query.For<TestItem>().Where(x => x.Name!.Contains("10%_off"));
|
|
|
|
var sqlite = new QueryBuilder<TestItem>(Map, SqliteDialect.Instance).Where(query).Render(SqliteDialect.Instance);
|
|
var postgres = new QueryBuilder<TestItem>(Map, PostgresDialect.Instance).Where(query).Render(PostgresDialect.Instance);
|
|
|
|
Assert.AreEqual(" WHERE kh_lower(\"name\") LIKE kh_lower(@p0) ESCAPE '\\'", sqlite.Text);
|
|
Assert.AreEqual(" WHERE \"name\" ILIKE @p0 ESCAPE '\\'", postgres.Text);
|
|
Assert.AreEqual("%10\\%\\_off%", sqlite.Parameters[0].Value);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void UnsupportedExpressions_AreRefused()
|
|
{
|
|
var query = Query.For<TestItem>().Where(x => x.Code.ToUpperInvariant() == "A");
|
|
|
|
Assert.ThrowsExactly<NotSupportedException>(() => Where(query));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Select_OrdersAndPages_AndDefaultsToKeyOrder()
|
|
{
|
|
var builder = new QueryBuilder<TestItem>(Map, SqliteDialect.Instance);
|
|
|
|
var ordered = builder.Select(Query.For<TestItem>().OrderByDescending(x => x.Amount).OrderBy(x => x.Code).Page(20, 10)).Render(SqliteDialect.Instance);
|
|
var paged = builder.Select(Query.For<TestItem>().Take(5)).Render(SqliteDialect.Instance);
|
|
|
|
StringAssert.EndsWith(ordered.Text, " ORDER BY \"amount\" DESC, \"code\" ASC LIMIT 10 OFFSET 20");
|
|
StringAssert.EndsWith(paged.Text, " ORDER BY \"id\" ASC LIMIT 5 OFFSET 0");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void SqlConditions_AreParenthesizedAndCombined()
|
|
{
|
|
var max = 100m;
|
|
var sql = Where(Query.For<TestItem>().Where(x => x.Active).Where(SqlFragment.Of($"{SqlFragment.Identifier("amount")} < {max}")));
|
|
|
|
Assert.AreEqual(" WHERE \"active\" = @p0 AND (\"amount\" < @p1)", sql.Text);
|
|
}
|
|
|
|
private static TestItemMap CreateMap()
|
|
{
|
|
var map = new TestItemMap();
|
|
map.Freeze();
|
|
return map;
|
|
}
|
|
|
|
private static RenderedSql Where(Query<TestItem> query) =>
|
|
new QueryBuilder<TestItem>(Map, SqliteDialect.Instance).Where(query).Render(SqliteDialect.Instance);
|
|
}
|