Path: redb/redb.Tests.Integration/Tests/Base/ConstantPredicateTestsBase.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/ConstantPredicateTestsBase.cs
----
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Query;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A predicate that is a constant, alone or inside a condition: Where(x => true) keeps every object, Where(x => false)
/// keeps none, and inside AND / OR / NOT the constant folds as in LINQ over the same objects in memory. The same for
/// WhereRedb and AllAsync, on a flat query and on a tree query, for the list, the count and Any.
///
/// Each test reads only the objects it seeded (by id, or by the root of its tree), so the shared database stays out.
///
public abstract class ConstantPredicateTestsBase
{
protected readonly IRedbService Redb;
protected ConstantPredicateTestsBase(IRedbService redb) => Redb = redb;
private const int Total = 4; // ages 30, 31, 32, 33
private sealed record Seeded(long RootId, List Ids);
private async Task SeedAsync()
{
await Redb.SyncSchemeAsync();
var tag = "cp-" + Guid.NewGuid().ToString("N")[..8];
var root = new TreeRedbObject
{
name = $"{tag}-root",
Props = TestDataFactory.CreateEmployee(0, department: $"{tag}-root").Props
};
root.id = await Redb.SaveAsync(root);
var ids = new List();
for (var i = 0; i < Total; i++)
{
var p = TestDataFactory.CreateEmployee(i + 1, firstName: $"{tag}-{i}", department: tag).Props;
p.Age = 30 + i;
ids.Add(await Redb.CreateChildAsync(new TreeRedbObject { name = $"{tag}-{i}", Props = p }, root));
}
return new Seeded(root.id, ids);
}
private IRedbQueryable Employees(Seeded s, bool tree)
{
if (tree)
return Redb.TreeQuery(s.RootId).WhereRedb(o => o.Id != s.RootId);
var idSet = s.Ids.ToHashSet();
return Redb.Query().WhereRedb(o => idSet.Contains(o.Id));
}
/// The query of a case and the number of seeded objects it keeps.
private static (IRedbQueryable Query, int Expected) Case(IRedbQueryable q, string kind)
{
// A bool value of the caller is the constant it holds.
var yes = true;
var no = false;
return kind switch
{
"true" => (q.Where(e => true), Total),
"false" => (q.Where(e => false), 0),
"captured-true" => (q.Where(e => yes), Total),
"captured-false" => (q.Where(e => no), 0),
"field-and-captured-true" => (q.Where(e => e.Age >= 32 && yes), 2),
"field-and-captured-false" => (q.Where(e => e.Age >= 32 && no), 0),
"field-or-captured-true" => (q.Where(e => e.Age >= 32 || yes), Total),
"field-or-captured-false" => (q.Where(e => e.Age >= 32 || no), 2),
"not-captured-true" => (q.Where(e => !yes), 0),
"not-captured-false" => (q.Where(e => !no), Total),
"nested-captured" => (q.Where(e => (e.Age == 30 || no) && (yes || e.Age == 99)), 1),
// NOT over a condition that folds to a constant.
"not-or-literal-true" => (q.Where(e => !(e.Age >= 32 || true)), 0),
"not-and-literal-false" => (q.Where(e => !(e.Age >= 32 && false)), Total),
"not-or-literal-false" => (q.Where(e => !(e.Age >= 32 || false)), 2),
"true-then-field" => (q.Where(e => true).Where(e => e.Age >= 32), 2),
"field-then-true" => (q.Where(e => e.Age >= 32).Where(e => true), 2),
"field-then-false" => (q.Where(e => e.Age >= 32).Where(e => false), 0),
"nested-literal" => (q.Where(e => (e.Age == 30 || false) && (true || e.Age == 99)), 1),
"field-or-literal-true" => (q.Where(e => e.Age >= 32 || true), Total),
"field-or-literal-false" => (q.Where(e => e.Age >= 32 || false), 2),
"field-and-literal-true" => (q.Where(e => e.Age >= 32 && true), 2),
"field-and-literal-false" => (q.Where(e => e.Age >= 32 && false), 0),
"redb-true" => (q.WhereRedb(o => true), Total),
"redb-false" => (q.WhereRedb(o => false), 0),
"redb-true-then-field" => (q.WhereRedb(o => true).Where(e => e.Age >= 32), 2),
_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null)
};
}
public static IEnumerable