Path: redb-route/tests/redb.Route.Tests.Sql/Batch/BatchStrictBindingTests.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/Batch/BatchStrictBindingTests.cs
----
using redb.Route.Core;
using redb.Route.Sql;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql.Batch;
///
/// A placeholder with no value anywhere is an error, as in Apache Camel ("Cannot find key ... to use when setting named
/// parameter"); a key that is present with a null value binds NULL.
///
public sealed class BatchStrictBindingTests : IDisposable
{
private const string Insert = "INSERT INTO strict_items (id, val) VALUES (:#id, :#val)";
private readonly SqliteTestHelper _db = new();
public BatchStrictBindingTests() => _db.Execute("CREATE TABLE strict_items (id INTEGER NOT NULL, val TEXT NULL)");
public void Dispose() => _db.Dispose();
[Fact]
public async Task Batch_MissingKey_BreakMode_FailsWithItemIndexAndParameterName()
{
var carrier = Carrier(new Dictionary { ["id"] = 1, ["val"] = "a" }, new Dictionary { ["id"] = 2 });
var thrown = await RunAsync(carrier, breakOnError: true);
thrown.Should().BeOfType(Outcome.Describe(thrown));
thrown!.Message.Should().Contain("item 1").And.Contain(":#val");
thrown.Data[SqlHeaders.BatchFailedIndex].Should().Be(1);
Ids().Should().BeEmpty("the batch breaks on that item and rolls back");
}
[Fact]
public async Task Batch_MissingKey_ContinueMode_ReportedAsItemError()
{
var carrier = Carrier(new Dictionary { ["id"] = 1, ["val"] = "a" }, new Dictionary { ["id"] = 2 }, new Dictionary { ["id"] = 3, ["val"] = "c" });
var thrown = await RunAsync(carrier, breakOnError: false);
thrown.Should().BeNull(Outcome.Describe(thrown));
Ids().Should().Equal(new object?[] { 1L, 3L }, "the item without a value is skipped, the others commit");
carrier.In.Headers.Should().ContainKey(SqlHeaders.BatchErrors);
var errors = carrier.In.Headers[SqlHeaders.BatchErrors].Should().BeAssignableTo>().Subject;
errors.Should().ContainSingle().Which.Index.Should().Be(1);
errors[0].Message.Should().Contain(":#val");
}
[Fact]
public async Task Batch_KeyWithNullValue_BindsNull()
{
var carrier = Carrier(new Dictionary { ["id"] = 1, ["val"] = null });
var thrown = await RunAsync(carrier);
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { null }, "a key that is present binds its value, null included");
}
[Fact]
public async Task Batch_ExplicitTemplateEvaluatingToNull_BindsNull()
{
var carrier = Carrier(new Dictionary { ["id"] = 1 });
var thrown = await RunAsync(carrier, options: new() { ["param.val"] = "${header.missing}" });
thrown.Should().BeNull(Outcome.Describe(thrown));
Values().Should().Equal(new object?[] { null }, "an explicit param.* always has a value, even when it evaluates to null");
}
[Fact]
public async Task Batch_ScalarItems_AllNamesResolvedElsewhere_Bind()
{
var carrier = new Exchange(new Message(new List { 1, 2 }));
carrier.In.Headers["val"] = "h";
var thrown = await RunAsync(carrier, options: new() { ["param.id"] = "${body}" });
thrown.Should().BeNull(Outcome.Describe(thrown));
Ids().Should().Equal(new object?[] { 1L, 2L });
Values().Should().Equal(new object?[] { "h", "h" }, "a scalar item carries no names, the header still provides one");
}
private static Exchange Carrier(params Dictionary[] items) => new(new Message(items.ToList()));
private async Task RunAsync(Exchange carrier, bool? breakOnError = null, Dictionary? options = null)
{
var parameters = new Dictionary { ["outputType"] = "None", ["batchSize"] = "10" };
if (breakOnError is { } mode)
parameters["breakBatchOnError"] = mode ? "true" : "false";
if (options is not null)
foreach (var (key, value) in options)
parameters[key] = value;
await using var context = new RouteContext();
var endpoint = SqlEndpointHarness.CreateEndpoint(context, _db.CreateFactory(), Insert, parameters);
return await Outcome.Of(() => endpoint.CreateProducer().Process(carrier, CancellationToken.None));
}
private List