Path: redb/redb.Tests.Integration/Tests/Base/TransactionIsolationTestsBase.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/TransactionIsolationTestsBase.cs
----
using System.Data;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// BR-1 (owner decision 2026-09-02): the transaction isolation level is an optional parameter of
/// BeginTransactionAsync / ExecuteAtomicAsync. Without it nothing changes - each
/// provider keeps its own default. PostgreSQL and MSSQL apply the requested level to the
/// transaction they open; SQLite accepts the parameter for portability and stays what it always
/// is, a single serial writer (BEGIN IMMEDIATE). An active or ambient transaction is joined as it
/// is - its level is never changed. Free and Pro share the connection classes, so the three Free
/// suites cover both editions.
///
public abstract class TransactionIsolationTestsBase
{
protected readonly IRedbService Redb;
protected TransactionIsolationTestsBase(IRedbService redb) => Redb = redb;
/// SQL returning the current isolation level as text; null when the provider has no readout (SQLite).
protected abstract string? ReadLevelSql { get; }
protected virtual string SerializableLevelText => "serializable";
protected virtual string DefaultLevelText => "read committed";
[Fact]
public async Task ExplicitLevel_IsApplied_AndTheDefaultIsUntouched()
{
if (ReadLevelSql is { } sql)
{
await using (var tx = await Redb.Context.BeginTransactionAsync(IsolationLevel.Serializable))
{
(await Redb.Context.ExecuteScalarAsync(sql)).Should().Be(SerializableLevelText,
"the requested level must reach the transaction the provider opens");
await tx.RollbackAsync();
}
await using (var tx = await Redb.Context.BeginTransactionAsync())
{
(await Redb.Context.ExecuteScalarAsync(sql)).Should().Be(DefaultLevelText,
"the parameter is optional and the default must stay exactly what it was");
await tx.RollbackAsync();
}
}
else
{
// SQLite: no readout - the parameter must be accepted and the transaction fully usable.
await using var tx = await Redb.Context.BeginTransactionAsync(IsolationLevel.Serializable);
(await Redb.Context.ExecuteScalarAsync("SELECT 1")).Should().Be(1);
await tx.RollbackAsync();
}
}
[Fact]
public async Task ExecuteAtomic_WithLevel_CommitsTheWork()
{
long id = 0;
await Redb.Context.ExecuteAtomicAsync(IsolationLevel.Serializable, async () =>
{
id = await Redb.SaveAsync(new RedbObject
{
name = "iso-atomic",
Props = new SimpleProps { Title = "iso", Count = 7 }
});
});
(await Redb.LoadAsync(id, depth: 1))!.Props.Title.Should().Be("iso",
"work done under the isolation-level overload commits like the plain form");
}
}