Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Base/AmbientTransactionBatchTestsBase.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/AmbientTransactionBatchTestsBase.cs
----
using System.Transactions;
using redb.Route.Tests.Sql.E2E.Infrastructure;
namespace redb.Route.Tests.Sql.E2E.Base;
///
/// The batch inside a route-level transaction (.Transacted() — an ambient ). There is no
/// local transaction to take savepoints on, so continuing past an error cannot be honest and is refused before any write.
/// Runs on providers that enlist in System.Transactions.
///
public abstract class AmbientTransactionBatchTestsBase : IAsyncLifetime
{
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);
///
public async Task DisposeAsync()
{
try
{
if (_db is not null)
await _db.DisposeAsync();
}
finally
{
(Provider as IDisposable)?.Dispose();
}
}
[Fact]
public async Task BatchContinue_UnderTransactionScope_FailsBeforeFirstWrite()
{
Exception? thrown;
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
(_, thrown) = await SqlBatchRun.RunAsync(Db, Provider.InsertSql(Db.Table), Provider.DuplicateKeyItems(), breakOnError: false);
// A route completes its transaction when the pipeline did not fail.
if (thrown is null)
scope.Complete();
}
thrown.Should().BeOfType(
$"continue mode needs a local transaction with savepoints, but the batch threw {Outcome.Describe(thrown)}");
(await Db.ReadIdsAsync()).Should().BeEmpty("the refusal comes before anything is written");
}
[Fact]
public async Task BatchBreak_UnderTransactionScopeNotCompleted_LeavesNoRows()
{
var items = new List>
{
new() { ["id"] = 1, ["val"] = "a" },
new() { ["id"] = 2, ["val"] = "b" },
new() { ["id"] = 3, ["val"] = "c" },
};
using (new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
var (_, thrown) = await SqlBatchRun.RunAsync(Db, Provider.InsertSql(Db.Table), items, breakOnError: true);
thrown.Should().BeNull($"the batch itself succeeds ({Outcome.Describe(thrown)})");
// A step after the SQL endpoint fails: the route does not complete its transaction.
}
(await Db.ReadIdsAsync()).Should().BeEmpty("the batch wrote inside the route's transaction, which rolled back");
}
}