Files
Khaemhat/tests/Khaemhat.Data.Tests/SqlFragmentTests.cs
T
Alby96andClaude Opus 5.5 738c43ba0a
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
feat: data layer with SQLite and PostgreSQL providers, migrations and encrypted company registry (M1)
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>
2026-11-11 13:11:34 +01:00

71 lines
2.6 KiB
C#

using Khaemhat.Data.PostgreSql;
using Khaemhat.Data.Schema;
using Khaemhat.Data.Sqlite;
namespace Khaemhat.Data.Tests;
[TestClass]
public sealed class SqlFragmentTests
{
[TestMethod]
public void Holes_BecomeParameters_AndNeverReachTheText()
{
const string hostile = "x'); DROP TABLE parties; --";
var fragment = SqlFragment.Of($"SELECT * FROM parties WHERE name = {hostile} AND id > {42L}");
var rendered = fragment.Render(SqliteDialect.Instance);
Assert.AreEqual("SELECT * FROM parties WHERE name = @p0 AND id > @p1", rendered.Text);
Assert.HasCount(2, rendered.Parameters);
Assert.AreEqual(hostile, rendered.Parameters[0].Value);
Assert.AreEqual(42L, rendered.Parameters[1].Value);
Assert.DoesNotContain("DROP", rendered.Text);
}
[TestMethod]
public void NestedFragments_AreRenumbered()
{
var inner = SqlFragment.Of($"code = {"A"}");
var outer = SqlFragment.Of($"SELECT 1 WHERE {inner} OR name = {"B"}");
var rendered = outer.Render(PostgresDialect.Instance);
Assert.AreEqual("SELECT 1 WHERE code = @p0 OR name = @p1", rendered.Text);
CollectionAssert.AreEqual(new object?[] { "A", "B" }, rendered.Parameters.Select(p => p.Value).ToArray());
}
[TestMethod]
public void Identifiers_AreValidatedAndQuoted()
{
var fragment = SqlFragment.Of($"SELECT {SqlFragment.Identifier("parties.vat_number")} FROM {SqlFragment.Identifier("parties")}");
Assert.AreEqual("SELECT \"parties\".\"vat_number\" FROM \"parties\"", fragment.Render(SqliteDialect.Instance).Text);
Assert.ThrowsExactly<ArgumentException>(() => SqlFragment.Identifier("parties; DROP TABLE x"));
Assert.ThrowsExactly<ArgumentException>(() => SqlFragment.Identifier("Name"));
}
[TestMethod]
public void ToString_ShowsPlaceholdersButNotValues()
{
var fragment = SqlFragment.Of($"UPDATE sys_settings SET value = {"s3cr3t-password"}");
Assert.AreEqual("UPDATE sys_settings SET value = @p0", fragment.ToString());
}
[TestMethod]
public void TypedParameter_KeepsItsType()
{
var fragment = SqlFragment.Of($"INSERT INTO t (data) VALUES ({SqlFragment.Parameter("{}", ColumnType.Json)})");
Assert.AreEqual(ColumnType.Json, fragment.Render(PostgresDialect.Instance).Parameters[0].Type);
}
[TestMethod]
public void Join_SkipsEmptyFragments()
{
var joined = SqlFragment.Join(" AND ", [SqlFragment.Literal("a = 1"), SqlFragment.Empty, SqlFragment.Of($"b = {2}")]);
Assert.AreEqual("a = 1 AND b = @p0", joined.Render(SqliteDialect.Instance).Text);
}
}