Path: redb/redb.Tests.Integration/Tests/Base/ElementIndexFilterTestsBase.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/ElementIndexFilterTestsBase.cs
----
using System.Linq.Expressions;
using redb.Core;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A filter of redb Pro reads an element of an array or a list by its index: the element itself (Levels[0] == 5)
/// or one field of an element of a class (Items[0].Name == v). An element past the end is no value, as a missing
/// dictionary key or a missing field is: == and != leave the object out, == null takes it. The
/// expected answers are written out per case (LINQ to objects would throw on an index past the end).
///
/// Four objects, Marker o0..o3; the object's own Name and Rank differ from every element's:
/// o0: Levels [5, 1], Tags [a, b], Items/Parts [(x0, 1, null), (y0, 2, 3)], Statuses [Admin, User];
/// o1: Levels [1, 5], Tags [b, a], Items/Parts [(y1, 2, 4), (x1, 1, null)], Statuses [User];
/// o2: Levels [5], Tags [a], Items/Parts [(x2, 3, 7)], Statuses [];
/// o3: no arrays.
///
public abstract class ElementIndexFilterTestsBase
{
protected readonly IRedbService Redb;
protected ElementIndexFilterTestsBase(IRedbService redb) => Redb = redb;
private async Task<(string Tag, RedbList List)> SeedAsync()
{
await Redb.SyncSchemeAsync();
var tag = "ei-" + Guid.NewGuid().ToString("N")[..8];
var list = await Redb.ListProvider.SaveListAsync(RedbList.Create(tag, tag));
var items = await Redb.ListProvider.AddItemsAsync(list, ["Admin", "User"]);
var admin = items.First(i => i.Value == "Admin");
var user = items.First(i => i.Value == "User");
static ElementIndexItem[] Elements(params (string Name, int Rank, int? Score)[] e)
=> e.Select(x => new ElementIndexItem { Name = x.Name, Rank = x.Rank, Score = x.Score }).ToArray();
var props = new[]
{
new ElementIndexProps { Levels = [5, 1], Tags = ["a", "b"], Items = Elements(("x0", 1, null), ("y0", 2, 3)), Statuses = [admin, user] },
new ElementIndexProps { Levels = [1, 5], Tags = ["b", "a"], Items = Elements(("y1", 2, 4), ("x1", 1, null)), Statuses = [user] },
new ElementIndexProps { Levels = [5], Tags = ["a"], Items = Elements(("x2", 3, 7)), Statuses = [] },
new ElementIndexProps()
};
for (var i = 0; i < props.Length; i++)
{
props[i].Marker = tag;
props[i].Name = "own";
props[i].Rank = 9;
props[i].Parts = props[i].Items?.Select(e => new ElementIndexItem { Name = e.Name, Rank = e.Rank, Score = e.Score }).ToList();
await Redb.SaveAsync(new RedbObject { name = $"o{i}", Props = props[i] });
}
return (tag, list);
}
private async Task NamesAsync(string tag, Expression> filter)
=> (await Redb.Query().Where(p => p.Marker == tag).Where(filter).ToListAsync())
.Select(o => o.name!).OrderBy(n => n).ToArray();
private async Task CleanupAsync(RedbList list)
{
var items = await Redb.ListProvider.GetListItemsAsync(list.Id);
await Redb.Context.Bulk.BulkDeleteValuesByListItemIdsAsync(items.Select(i => i.Id).ToList());
await Redb.ListProvider.DeleteListAsync(list.Id);
}
public static IEnumerable