Path: redb-identity/tests/redb.Identity.Tests/Infrastructure/MockRedbQuery.cs Snapshot: redb-identity, 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-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/Infrastructure/MockRedbQuery.cs ---- using System.Linq.Expressions; using NSubstitute; using redb.Core; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Core.Query.Aggregation; using redb.Core.Query.Grouping; using redb.Core.Query.Window; using redb.Identity.Core.Models; namespace redb.Identity.Tests.Infrastructure; /// /// Configures mock to return a queryable over an in-memory list. /// Usage: MockRedbQuery.Setup(redb, items) /// internal static class MockRedbQuery { /// /// Makes redb.Query<TProps>() return a mock queryable over . /// The mock supports: Where, WhereRedb, OrderByRedb, Skip, Take, ToListAsync, CountAsync, FirstOrDefaultAsync, AnyAsync. /// public static void Setup(IRedbService redb, List> items) where TProps : class, new() { var queryable = new InMemoryRedbQueryable(items); redb.Query().Returns(_ => new InMemoryRedbQueryable(items)); } /// /// Creates a RedbObject with the given id, name, and props for testing. /// public static RedbObject CreateObject(long id, string name, TProps props) where TProps : class, new() { var obj = new RedbObject(props) { Id = id, Name = name, DateCreate = DateTimeOffset.UtcNow, DateModify = DateTimeOffset.UtcNow }; // Sync value_string for [RedbIgnore] identity fields obj.value_string = props switch { ApplicationProps a => a.ClientId, ScopeProps s => s.ScopeName, _ => null }; return obj; } } /// /// Minimal in-memory IRedbQueryable for unit tests. /// Supports basic fluent operations used by management processors. /// internal class InMemoryRedbQueryable : IRedbQueryable, IOrderedRedbQueryable where TProps : class, new() { private IEnumerable> _source; public InMemoryRedbQueryable(IEnumerable> source) => _source = source; public IRedbQueryable Where(Expression> predicate) { var compiled = predicate.Compile(); return new InMemoryRedbQueryable(_source.Where(o => compiled(o.Props))); } public IRedbQueryable WhereRedb(Expression> predicate) { var compiled = predicate.Compile(); return new InMemoryRedbQueryable(_source.Where(o => compiled(o))); } public IOrderedRedbQueryable OrderBy(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbQueryable(_source.OrderBy(o => compiled(o.Props))); } public IOrderedRedbQueryable OrderByDescending(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbQueryable(_source.OrderByDescending(o => compiled(o.Props))); } public IOrderedRedbQueryable OrderByRedb(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbQueryable(_source.OrderBy(o => compiled(o))); } public IOrderedRedbQueryable OrderByDescendingRedb(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbQueryable(_source.OrderByDescending(o => compiled(o))); } public IRedbQueryable Take(int count) => new InMemoryRedbQueryable(_source.Take(count)); public IRedbQueryable Skip(int count) => new InMemoryRedbQueryable(_source.Skip(count)); public Task>> ToListAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.ToList()); public Task CountAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.Count()); public Task?> FirstOrDefaultAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.FirstOrDefault()); public Task?> FirstOrDefaultAsync(Expression> predicate, CancellationToken cancellationToken = default) { var compiled = predicate.Compile(); return Task.FromResult(_source.FirstOrDefault(o => compiled(o.Props))); } public Task AnyAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.Any()); public Task AnyAsync(Expression> predicate, CancellationToken cancellationToken = default) { var compiled = predicate.Compile(); return Task.FromResult(_source.Any(o => compiled(o.Props))); } // ── Not used by management processors but required by interface ── public IRedbQueryable WhereIn(Expression> selector, IEnumerable values) => throw new NotSupportedException(); public IRedbQueryable WhereInRedb(Expression> selector, IEnumerable values) => throw new NotSupportedException(); public Task AllAsync(Expression> predicate, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public IRedbProjectedQueryable Select(Expression, TResult>> selector) { var compiled = selector.Compile(); return new InMemoryRedbProjectedQueryable(_source.Select(compiled)); } public IRedbQueryable Distinct() => new InMemoryRedbQueryable(_source.Distinct()); public IRedbQueryable DistinctRedb() => throw new NotSupportedException(); public IRedbQueryable DistinctBy(Expression> keySelector) => throw new NotSupportedException(); public IRedbQueryable DistinctByRedb(Expression> keySelector) => throw new NotSupportedException(); public IRedbQueryable WithMaxRecursionDepth(int depth) => throw new NotSupportedException(); public IRedbQueryable WithLazyReferences(bool enabled = true) => throw new NotSupportedException(); public IRedbQueryable WhereHasAncestor(Expression> ancestorCondition, int? maxDepth = null) where TTarget : class => throw new NotSupportedException(); public IRedbQueryable WhereHasDescendant(Expression> descendantCondition, int? maxDepth = null) where TTarget : class => throw new NotSupportedException(); public IRedbQueryable WhereLevel(int level) => throw new NotSupportedException(); public IRedbQueryable WhereLevel(Expression> levelCondition) => throw new NotSupportedException(); public IRedbQueryable WhereRoots() => throw new NotSupportedException(); public IRedbQueryable WhereLeaves() => throw new NotSupportedException(); public IRedbQueryable WhereChildrenOf(long parentId) => throw new NotSupportedException(); public IRedbQueryable WhereChildrenOf(IRedbObject parentObject) => throw new NotSupportedException(); public IRedbQueryable WhereDescendantsOf(long ancestorId, int? maxDepth = null) => throw new NotSupportedException(); public IRedbQueryable WhereDescendantsOf(IRedbObject ancestorObject, int? maxDepth = null) => throw new NotSupportedException(); public IRedbQueryable WithMaxDepth(int depth) => throw new NotSupportedException(); public IRedbQueryable WithPropsDepth(int depth) => throw new NotSupportedException(); public Task>> ToTreeListAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task> ToRootListAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task>> ToFlatListAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task DeleteAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task SumAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task AverageAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task MinAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task MaxAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task> GetStatisticsAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task AggregateAsync(Expression, TResult>> selector, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task SumRedbAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task AverageRedbAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task MinRedbAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task MaxRedbAsync(Expression> selector, CancellationToken cancellationToken = default) where TField : struct => throw new NotSupportedException(); public Task AggregateRedbAsync(Expression> selector, CancellationToken cancellationToken = default) => throw new NotSupportedException(); public IRedbGroupedQueryable GroupBy(Expression> keySelector) => throw new NotSupportedException(); public IRedbGroupedQueryable GroupByRedb(Expression> keySelector) => throw new NotSupportedException(); public IRedbGroupedQueryable GroupByArray(Expression>> arraySelector, Expression> keySelector) => throw new NotSupportedException(); public IRedbWindowedQueryable WithWindow(Action> windowConfig) => throw new NotSupportedException(); public string ToSqlString() => throw new NotSupportedException(); public Task ToSqlStringAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); public Task ToFilterJsonAsync(CancellationToken cancellationToken = default) => throw new NotSupportedException(); // IOrderedRedbQueryable public IOrderedRedbQueryable ThenBy(Expression> keySelector) => throw new NotSupportedException(); public IOrderedRedbQueryable ThenByDescending(Expression> keySelector) => throw new NotSupportedException(); public IOrderedRedbQueryable ThenByRedb(Expression> keySelector) => throw new NotSupportedException(); public IOrderedRedbQueryable ThenByDescendingRedb(Expression> keySelector) => throw new NotSupportedException(); } /// /// In-memory projected queryable for unit tests. Supports Distinct, Take, ToListAsync. /// internal class InMemoryRedbProjectedQueryable : IRedbProjectedQueryable { private IEnumerable _source; public InMemoryRedbProjectedQueryable(IEnumerable source) => _source = source; public IRedbProjectedQueryable Where(Expression> predicate) { var compiled = predicate.Compile(); return new InMemoryRedbProjectedQueryable(_source.Where(compiled)); } public IRedbProjectedQueryable OrderBy(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbProjectedQueryable(_source.OrderBy(compiled)); } public IRedbProjectedQueryable OrderByDescending(Expression> keySelector) { var compiled = keySelector.Compile(); return new InMemoryRedbProjectedQueryable(_source.OrderByDescending(compiled)); } public IRedbProjectedQueryable Take(int count) => new InMemoryRedbProjectedQueryable(_source.Take(count)); public IRedbProjectedQueryable Skip(int count) => new InMemoryRedbProjectedQueryable(_source.Skip(count)); public IRedbProjectedQueryable Distinct() => new InMemoryRedbProjectedQueryable(_source.Distinct()); public Task> ToListAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.ToList()); public Task CountAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.Count()); public Task FirstOrDefaultAsync(CancellationToken cancellationToken = default) => Task.FromResult(_source.FirstOrDefault()); public Task GetProjectionInfoAsync(CancellationToken cancellationToken = default) => Task.FromResult("in-memory-projection"); }