Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Base/BatchBindingE2ETestsBase.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/E2E/Base/BatchBindingE2ETestsBase.cs
----
using System.Diagnostics;
using redb.Route.Abstractions;
using redb.Route.Aggregation;
using redb.Route.Core;
using redb.Route.DataFormats.Csv;
using redb.Route.Sql;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql.E2E.Base;
///
/// Batch items in the shapes real routes produce — CSV rows, JSON objects, POCOs, aggregated exchanges — written to a real
/// database: every source record must arrive with its own values.
///
public abstract class BatchBindingE2ETestsBase : IAsyncLifetime
{
private const string EntryUri = "direct://batch-binding";
private SqlE2EDatabase? _db;
/// The provider under test.
protected abstract SqlE2EProvider Provider { get; }
private SqlE2EDatabase Db => _db ?? throw new InvalidOperationException("The table is created in InitializeAsync.");
///
public async Task InitializeAsync() => _db = await SqlE2EDatabase.CreateAsync(Provider, Provider.CreateNullableTableSql);
///
public async Task DisposeAsync()
{
try
{
if (_db is not null)
await _db.DisposeAsync();
}
finally
{
(Provider as IDisposable)?.Dispose();
}
}
[Fact]
public async Task CsvWithHeader_ToBatch_AllRowsPersisted()
{
const string csv = "id,val,n\n1,a,10\n2,b,\n3,,30\n";
// CSV values are text, and PostgreSQL does not convert a text parameter to an integer column: the statement casts.
var sql = $"INSERT INTO {Db.Table} (id, val, n) VALUES (CAST(:#id AS int), :#val, CAST(:#n AS bigint))";
await RunRouteAsync(route => route.UnmarshalCsv>>().To(BatchUri(sql)), csv);
(await ReadRowsAsync()).Should().Equal("1|a|10", "2|b|", "3||30");
}
[Fact]
public async Task JsonArray_ToBatch_AllRowsPersisted()
{
const string json = """[{"id":1,"val":"a","n":10},{"id":2,"val":"b","n":null},{"id":3,"val":null,"n":9007199254740993}]""";
var sql = $"INSERT INTO {Db.Table} (id, val, n) VALUES (:#id, :#val, :#n)";
await RunRouteAsync(route => route.Unmarshal>>("application/json").To(BatchUri(sql)), json);
(await ReadRowsAsync()).Should().Equal("1|a|10", "2|b|", "3||9007199254740993");
}
[Fact]
public async Task JsonArrayUnmarshalledToObject_ToBatch_AllRowsPersisted()
{
const string json = """[{"id":1,"val":"a","n":10},{"id":2,"val":"b","n":null},{"id":3,"val":null,"n":30}]""";
var sql = $"INSERT INTO {Db.Table} (id, val, n) VALUES (:#id, :#val, :#n)";
// Unmarshal to object gives one JsonElement array: a batch source since 17.4 (decision 18).
await RunRouteAsync(route => route.Unmarshal