Path: redb/redb.Tests.Integration/Tests/Base/BulkCreateAtomicityTestsBase.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/BulkCreateAtomicityTestsBase.cs
----
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// AddNewObjectsAsync outside a caller transaction writes the object rows and then the value rows. When the value insert
/// fails, the create as a whole has failed: none of its objects may stay behind without their values. The failure here is
/// a duplicate key of a [RedbUnique] field, which the value rows carry; it does not depend on the scheme.
///
public abstract class BulkCreateAtomicityTestsBase : IAsyncLifetime
{
private const string Marker = "bulk-create-atomicity";
protected readonly IRedbService Redb;
protected BulkCreateAtomicityTestsBase(IRedbService redb) => Redb = redb;
public async Task InitializeAsync()
{
await Redb.SyncSchemeAsync();
await CleanAsync();
}
public Task DisposeAsync() => CleanAsync();
private Task CleanAsync() =>
Redb.Context.ExecuteAsync($"DELETE FROM _objects WHERE _name LIKE '{Marker}%'");
private Task CountAsync(string name) =>
Redb.Context.ExecuteScalarAsync($"SELECT COUNT(*) FROM _objects WHERE _name = '{name}'");
[Fact]
public async Task AddNewObjects_WhenTheValueInsertFails_LeavesNoObjectBehind()
{
var code = $"{Marker}-{Guid.NewGuid():N}";
await Redb.SaveAsync(new RedbObject
{ name = $"{Marker}-seed", Props = new UniqueOrderProps { Code = code } });
var duplicate = new RedbObject
{ name = $"{Marker}-duplicate", Props = new UniqueOrderProps { Code = code, Note = "the key is taken" } };
var create = () => Redb.AddNewObjectsAsync(new[] { duplicate });
await create.Should().ThrowAsync("the value rows break the unique key of Code");
Redb.Context.IsInTransaction.Should().BeFalse("the create ran on its own, outside a caller transaction");
(await CountAsync($"{Marker}-duplicate")).Should().Be(0, "a failed create leaves no object row without its values");
(await CountAsync($"{Marker}-seed")).Should().Be(1);
}
}