ci / build, test, smoke (push) Failing after 3m33s
ci / the same smoke on an image with no base (push) Successful in 8m10s
ci / lint the workflow and the shell (push) Failing after 19s
ci / the operator page in a real browser (push) Successful in 4m41s
ci / build the image and smoke it (push) Failing after 8m11s
Wall 3 starts. Two projects, one query path, measured. dotnet/OpenWaf.Data is the runtime: OrmContext (a DbDataSource plus an ISqlDialect), Query<T>, RowReader, OrmMeta. It is typed over System.Data.Common and carries no provider package; the driver is supplied at composition, which is how the neighbouring deployment ships it too. Query<T> renders SQL by walking the expression tree rather than compiling it — that is the exact difference from EF's pipeline, and the reason it survives a NativeAOT link where EF does not. The supported grammar is a closed set that refuses by name: joins, subqueries, aggregates other than COUNT, updates, deletes, JSON columns and method calls each raise UnsupportedQueryNodeException naming the node, because a renderer that guesses is a renderer that ships wrong SQL. dotnet/OpenWaf.Data.Generator is a Roslyn incremental generator attached to OpenWaf.Infrastructure as an analyzer. For a [Table] class it emits the column map, the row materializer and an OrmMeta.Register call inside a [ModuleInitializer], so the question a NativeAOT binary cannot answer — what are this type's properties — is never asked. The generated file is ordinary C#, readable in obj/ and asserted against. PolicySetting is the entity moved and EdgePolicyProvider.ReloadAsync the call site, which was the first refusal in the probe log. Program.cs composes the driver and dialect on the same Database:Provider switch EF uses, so one generated query serves sqlite (the shipped image) and PostgreSQL (the migrations' provider). EF still owns every write and every other read; the two layers coexist on one composition root, which is what makes this incremental rather than a rewrite. Measured on the AOT binary, same probe, same window, before and after: queries refused at run time 32 -> 18; EdgePolicyProvider reload failures 1 -> 0; native binary 49,609,680 -> 49,676,016 bytes. The 18 that remain are named by their own frames: 14 EdgeStateStore.RefreshAsync, 2 BlockStore.WriteBanAsync, 1 BlockStore.AddOperatorAsync, 1 admin endpoint. The sqlite timestamp column turned out to be a contract, not a detail. The model puts EF's DateTimeOffsetToBinaryConverter on every timestamp column, so every row already written says (localTicks / 1000) * 2048 + (offsetMinutes & 2047); the formula was derived by probing EF's converter and is now pinned by SqliteTimestampEncodingTests against that converter over five instants including +05:30, -05:00 and DateTimeOffset.MinValue — including the microsecond truncation the /1000 imposes, which is EF's behaviour and is now a test rather than a future surprise. Two build traps, both paid for and both recorded where the next person will hit them. -p:PublishAot on the command line is a global property, so it reaches every project in the graph including a netstandard2.0 Roslyn analyzer, and the publish dies with NETSDK1207; AOT is now switched by OpenWafAot=true, which only OpenWaf.Edge.csproj turns into PublishAot — the same shape as the existing OpenWafCompiledModel switch. And a raw Microsoft.Data.Sqlite data source needs SQLitePCL.Batteries.Init() before its first open, because the order in which EF's provider and this data source are first used is not guaranteed. Gates on this tree: build 0 warnings, unit 391/391 (13 new: rendered SQL for both dialects, null tests, the captured-null case, the refusals, the timestamp encoding), integration 408/408, smoke 256/256, and the AOT probe above. One earlier run of the 408 carried a single failure in AdminConsoleTests at a host load average near 30 and the next run was clean; recorded rather than hidden, in the same family as the load-sensitive compose gates this README already documents.
198 lines
8.2 KiB
C#
198 lines
8.2 KiB
C#
using System.Data.Common;
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
using OpenWaf.Data;
|
|
using OpenWaf.Infrastructure.Persistence;
|
|
|
|
namespace OpenWaf.Tests.Unit;
|
|
|
|
/// <summary>
|
|
/// The generated query layer, checked against rendered SQL rather than against a
|
|
/// database. A wrong query should be red against a string — the same reason the
|
|
/// neighbouring deployment keeps its renderers public.
|
|
///
|
|
/// These tests also assert that the generator contributed to the build at all:
|
|
/// the metadata registration is a module initializer, and a generator that stops
|
|
/// running would otherwise show up as a failure inside the edge.
|
|
/// </summary>
|
|
public sealed class GeneratedQueryTests
|
|
{
|
|
private const string SelectAll =
|
|
"SELECT \"Key\", \"Value\", \"UpdatedAt\" FROM \"policy_settings\"";
|
|
|
|
/// <summary>A data source that is never asked for a connection: rendering a
|
|
/// query must not need one, and this says so in the type system.</summary>
|
|
private sealed class NeverOpenedDataSource : DbDataSource
|
|
{
|
|
public override string ConnectionString { get; } = string.Empty;
|
|
|
|
protected override DbConnection CreateDbConnection() =>
|
|
throw new NotSupportedException("rendering a query must not open a connection");
|
|
}
|
|
|
|
private static OrmContext Sqlite => new(new NeverOpenedDataSource(), SqliteDialect.Instance);
|
|
private static OrmContext Postgres => new(new NeverOpenedDataSource(), PostgresDialect.Instance);
|
|
|
|
[Fact]
|
|
public void The_generator_registered_the_table_it_was_given()
|
|
{
|
|
Assert.True(OrmMeta.TryGet(typeof(PolicySetting), out var meta),
|
|
"no generated metadata is registered for PolicySetting — the analyzer is not running");
|
|
|
|
Assert.Equal("policy_settings", meta.Table);
|
|
Assert.Equal(new[] { "Key", "Value", "UpdatedAt" }, meta.Columns.ToArray());
|
|
Assert.Equal("UpdatedAt", PolicySettingOrm.ColumnOf("UpdatedAt"));
|
|
Assert.Equal(string.Empty, PolicySettingOrm.ColumnOf("NotAColumn"));
|
|
}
|
|
|
|
[Fact]
|
|
public void A_whole_table_read_renders_the_columns_the_schema_has() =>
|
|
Assert.Equal(SelectAll, PolicySettingOrm.Query(Sqlite).RenderSelect());
|
|
|
|
[Fact]
|
|
public void A_captured_local_becomes_a_parameter_and_never_text_in_the_statement()
|
|
{
|
|
const string key = "streakThreshold";
|
|
var query = PolicySettingOrm.Query(Sqlite).Where(p => p.Key == key);
|
|
|
|
Assert.Equal(SelectAll + " WHERE (\"Key\" = @p0)", query.RenderSelect());
|
|
Assert.Single(query.Arguments);
|
|
Assert.Equal("streakThreshold", query.Arguments[0]);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_null_test_stays_an_is_null_and_never_becomes_an_equality_with_null()
|
|
{
|
|
// "col = NULL" is unknown rather than false: a filter written that way
|
|
// silently stops matching the rows it was written for.
|
|
var query = PolicySettingOrm.Query(Sqlite).Where(p => p.Key == "mode" && p.Value != null);
|
|
|
|
Assert.Equal(
|
|
SelectAll + " WHERE ((\"Key\" = @p0) AND (\"Value\" IS NOT NULL))",
|
|
query.RenderSelect());
|
|
}
|
|
|
|
[Fact]
|
|
public void A_captured_null_is_a_null_test_too()
|
|
{
|
|
string? value = null;
|
|
var query = PolicySettingOrm.Query(Sqlite).Where(p => p.Value == value);
|
|
|
|
Assert.Equal(SelectAll + " WHERE (\"Value\" IS NULL)", query.RenderSelect());
|
|
Assert.Empty(query.Arguments);
|
|
}
|
|
|
|
[Fact]
|
|
public void Ordering_and_paging_render_in_the_dialects_own_grammar()
|
|
{
|
|
Assert.Equal(
|
|
SelectAll + " ORDER BY \"Key\" ASC",
|
|
PolicySettingOrm.Query(Sqlite).OrderBy(p => p.Key).RenderSelect());
|
|
|
|
Assert.Equal(
|
|
SelectAll + " ORDER BY \"UpdatedAt\" DESC LIMIT 10 OFFSET 20",
|
|
PolicySettingOrm.Query(Sqlite).OrderByDescending(p => p.UpdatedAt).Skip(20).Take(10).RenderSelect());
|
|
}
|
|
|
|
[Fact]
|
|
public void Count_renders_against_the_same_where_clause() =>
|
|
Assert.Equal(
|
|
"SELECT COUNT(*) FROM \"policy_settings\" WHERE (\"Key\" = @p0)",
|
|
PolicySettingOrm.Query(Sqlite).Where(p => p.Key == "mode").RenderCount());
|
|
|
|
[Fact]
|
|
public void Both_providers_get_the_same_text_for_this_query()
|
|
{
|
|
// One SQL text for both providers is not a coincidence to lean on: it
|
|
// holds because both quote with double quotes and both bind named
|
|
// parameters. Where they differ is the dialect's job — and paging already
|
|
// differs, which is why the two tails are asserted apart.
|
|
Assert.Equal(
|
|
SelectAll + " LIMIT 2147483647 OFFSET 5",
|
|
PolicySettingOrm.Query(Postgres).Skip(5).RenderSelect());
|
|
Assert.Equal(
|
|
SelectAll + " LIMIT -1 OFFSET 5",
|
|
PolicySettingOrm.Query(Sqlite).Skip(5).RenderSelect());
|
|
}
|
|
|
|
[Fact]
|
|
public void A_shape_this_layer_has_not_built_refuses_by_name()
|
|
{
|
|
var ex = Assert.Throws<UnsupportedQueryNodeException>(
|
|
() => PolicySettingOrm.Query(Sqlite).Where(p => p.Key.Length == 4));
|
|
Assert.Contains("Length", ex.What, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_member_that_is_not_a_column_refuses_rather_than_guessing()
|
|
{
|
|
var ex = Assert.Throws<UnsupportedQueryNodeException>(
|
|
() => PolicySettingOrm.Query(Sqlite).OrderBy(p => "not a column"));
|
|
Assert.Contains("not a column", ex.Message, StringComparison.Ordinal);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The integer a DateTimeOffset becomes on sqlite. The model puts EF's
|
|
/// <c>DateTimeOffsetToBinaryConverter</c> on every timestamp column, so that
|
|
/// encoding is the storage contract of every row this application has written;
|
|
/// the generated layer reproduces it, and this test is what makes "reproduces"
|
|
/// mean something rather than "looks plausible". It also records the resolution:
|
|
/// the encoding divides the ticks by 1000, so a round trip through the column
|
|
/// drops the sub-microsecond digits. That is EF's behaviour, not a loss added
|
|
/// here — and it is asserted, so it cannot become a surprise later.
|
|
/// </summary>
|
|
public sealed class SqliteTimestampEncodingTests
|
|
{
|
|
private static readonly DateTimeOffset[] Instants =
|
|
[
|
|
new DateTimeOffset(2026, 10, 7, 12, 34, 56, TimeSpan.FromHours(3)),
|
|
new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(123456),
|
|
new DateTimeOffset(2020, 1, 1, 0, 0, 0, TimeSpan.FromHours(-5)),
|
|
new DateTimeOffset(2024, 2, 29, 23, 59, 59, TimeSpan.FromMinutes(330)),
|
|
DateTimeOffset.MinValue,
|
|
];
|
|
|
|
[Fact]
|
|
public void The_encoding_this_layer_writes_is_the_one_ef_writes()
|
|
{
|
|
var efEncodes = new DateTimeOffsetToBinaryConverter().ConvertToProviderExpression.Compile();
|
|
|
|
foreach (var instant in Instants)
|
|
Assert.Equal(efEncodes(instant), Convert.ToInt64(SqliteDialect.Instance.WriteDateTimeOffset(instant)));
|
|
}
|
|
|
|
[Fact]
|
|
public void What_ef_writes_this_layer_reads_back()
|
|
{
|
|
var converter = new DateTimeOffsetToBinaryConverter();
|
|
var efEncodes = converter.ConvertToProviderExpression.Compile();
|
|
|
|
foreach (var instant in Instants)
|
|
{
|
|
var stored = efEncodes(instant);
|
|
var efRead = Assert.IsAssignableFrom<DateTimeOffset>(converter.ConvertFromProvider(stored));
|
|
Assert.Equal(efRead, ReadFromStored(stored));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void The_column_has_microsecond_resolution_and_says_so()
|
|
{
|
|
var instant = new DateTimeOffset(2026, 5, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(1234567);
|
|
var read = ReadFromStored(Convert.ToInt64(SqliteDialect.Instance.WriteDateTimeOffset(instant)));
|
|
|
|
Assert.Equal(new DateTimeOffset(2026, 5, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(1234000), read);
|
|
Assert.NotEqual(instant.Ticks, read.Ticks);
|
|
}
|
|
|
|
/// <summary>Reads the integer the way the sqlite dialect does, without a
|
|
/// reader. The dialect's own reader path is exercised end to end by the
|
|
/// integration suite against a real database.</summary>
|
|
private static DateTimeOffset ReadFromStored(long stored)
|
|
{
|
|
var offsetMinutes = (int)(stored & 2047);
|
|
if (offsetMinutes >= 1024) offsetMinutes -= 2048;
|
|
return new DateTimeOffset((stored >> 11) * 1000, TimeSpan.FromMinutes(offsetMinutes));
|
|
}
|
|
}
|