Path: redb/redb.Tests.Integration/Tests/Base/SchemeLoadValidationTestsBase.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/SchemeLoadValidationTestsBase.cs ---- using redb.Core; using redb.Core.Exceptions; using redb.Core.Models.Entities; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// S1 — LoadAsync<TProps> verifies that the loaded object's scheme matches TProps, /// so an object loaded under the wrong type never yields garbage or poisons the cache. /// /// The reaction to a mismatch is configurable via ThrowOnSchemeMismatch: by default /// LoadAsync returns null (so a soft-deleted object, scheme -10, reads as null — what /// soft-delete callers expect); with the flag set it throws. /// /// public abstract class SchemeLoadValidationTestsBase { protected readonly IRedbService Redb; protected SchemeLoadValidationTestsBase(IRedbService redb) => Redb = redb; private async Task SaveSimpleAsync() { var obj = TestDataFactory.CreateSimple("S1-probe", 10m); return await Redb.SaveAsync(obj); } /// Runs with ThrowOnSchemeMismatch forced on, then restores it. private async Task WithStrictMismatchAsync(Func body) { var previous = Redb.Configuration.ThrowOnSchemeMismatch; Redb.Configuration.ThrowOnSchemeMismatch = true; try { await body(); } finally { Redb.Configuration.ThrowOnSchemeMismatch = previous; } } [Fact] public async Task LoadAsync_WrongType_ReturnsNull_ByDefault() { var id = await SaveSimpleAsync(); var loaded = await Redb.LoadAsync(id); loaded.Should().BeNull("default behaviour is a soft null, not an exception"); } [Fact] public async Task LoadAsync_WrongType_Throws_WhenStrict() { var id = await SaveSimpleAsync(); await WithStrictMismatchAsync(async () => { var act = async () => await Redb.LoadAsync(id); var ex = await act.Should().ThrowAsync(); ex.Which.ObjectId.Should().Be(id); ex.Which.RequestedType.Should().Be(typeof(EmployeeProps)); }); } [Fact] public async Task LoadAsync_CorrectType_Works() { var id = await SaveSimpleAsync(); var loaded = await Redb.LoadAsync(id); loaded.Should().NotBeNull(); loaded!.Props.Title.Should().Be("S1-probe"); } [Fact] public async Task LoadAsync_WrongType_DoesNotPoisonCache() { var id = await SaveSimpleAsync(); // Wrong-type load must be rejected BEFORE anything is cached... var bad = await Redb.LoadAsync(id); bad.Should().BeNull(); // ...so the correct-type load still returns clean data (no garbage lingering in the cache). var good = await Redb.LoadAsync(id); good.Should().NotBeNull(); good!.Props.Title.Should().Be("S1-probe"); } [Fact] public async Task LoadAsync_SoftDeletedObject_ReadsAsNull() { var id = await SaveSimpleAsync(); // SoftDeleteAsync (not the hard DeleteAsync) moves the object under a trash container and sets // its _id_scheme to -10 (@@__deleted). Loading it typed must not return it as a live SimpleProps — // -10 never matches a real scheme, so by default the guard returns null (what soft-delete expects). var mark = await Redb.SoftDeleteAsync(new[] { id }); mark.MarkedCount.Should().BeGreaterThan(0); var loaded = await Redb.LoadAsync(id); loaded.Should().BeNull(); } [Fact] public async Task LoadAsync_SoftDeletedObject_Throws_WhenStrict() { var id = await SaveSimpleAsync(); var mark = await Redb.SoftDeleteAsync(new[] { id }); mark.MarkedCount.Should().BeGreaterThan(0); await WithStrictMismatchAsync(async () => { var act = async () => await Redb.LoadAsync(id); await act.Should().ThrowAsync(); }); } }