Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProContainsWalkTests.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/MsSqlPro/MsSqlProContainsWalkTests.cs ---- using System.Linq.Expressions; using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.MsSqlPro; /// /// A page in object order under a Contains on an array of text: SQL Server Pro walks the first rows of the array in /// object order (at least 1 000, fifty per row of the page) and answers from the walk when it filled the page, from the /// streams otherwise. The answers must not change: every query runs with the planner off and on and is compared with the /// answer computed from the loaded objects, with the page filled by the walk (two values on many objects, Skip, a value /// twice in one array, a scalar condition beside it, the id ascending), and left to the streams (a rare value; values /// only on the last objects, past the walk's rows). The first object that matches is in the trash, its rows still there. /// [Collection("MsSqlPro")] public class MsSqlProContainsWalkTests { private const int Objects = 300; private static readonly SemaphoreSlim SeedLock = new(1, 1); private readonly IRedbService _redb; public MsSqlProContainsWalkTests(MsSqlProFixture fixture) => _redb = fixture.Redb; // About five rows of the array per object (its header and the elements), 1 500 for 300 objects: the walk of a page // of 5 reads the first 1 000, about 200 objects, so the last objects are past it. private static string[] SkillsOf(int i) { var skills = new List { "x", "y", "z" }; if (i % 2 == 0) skills.Add("a"); if (i % 10 == 0) skills.Add("a"); if (i % 3 == 0) skills.Add("b"); if (i == 250) skills.Add("rare"); if (i >= 280) skills.Add("late"); return skills.ToArray(); } private async Task>> SeedAsync() { await SeedLock.WaitAsync(); try { var all = await _redb.Query().ToListAsync(); if (all.Count == Objects - 1) return all; if (all.Count > 0) await _redb.DeleteAsync(all.Select(o => o.Id).ToList()); var objects = Enumerable.Range(0, Objects).Select(i => new RedbObject { name = $"contains-walk-{i:000}", Props = new ContainsWalkProps { Age = i % 50, Department = i % 4 == 0 ? "eng" : "ops", Skills = SkillsOf(i) } }).ToList(); await _redb.SaveAsync(objects); // The first object holds a and b: in the trash with its rows, it must not come back. await _redb.SoftDeleteAsync([objects[0].Id]); all = await _redb.Query().ToListAsync(); all.Should().HaveCount(Objects - 1); return all; } finally { SeedLock.Release(); } } private async Task EachFormAsync(Func check) { var restore = _redb.Configuration.EnablePvtPrefilter; try { foreach (var on in new[] { false, true }) { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = on); await check(on ? "planner on" : "planner off"); } } finally { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = restore); } } private static List Expected(IEnumerable> all, Func keep, int skip, int take) => all.Where(o => keep(o.Props)).Select(o => o.Id).OrderBy(id => id).Skip(skip).Take(take).ToList(); public static IEnumerable Pages() { static object[] Case(string name, Expression> filter, int skip, int take) => [name, filter, skip, take]; yield return Case("two wide values", p => p.Skills!.Contains("a") && p.Skills!.Contains("b"), 0, 5); yield return Case("two wide values, Skip", p => p.Skills!.Contains("a") && p.Skills!.Contains("b"), 7, 5); yield return Case("a value twice in one array", p => p.Skills!.Contains("a"), 0, 5); yield return Case("a scalar condition beside it", p => p.Skills!.Contains("b") && p.Age > 30, 0, 5); yield return Case("a text scalar beside it", p => p.Skills!.Contains("a") && p.Department == "eng", 0, 5); yield return Case("a rare value", p => p.Skills!.Contains("rare") && p.Skills!.Contains("a"), 0, 5); yield return Case("values past the walk", p => p.Skills!.Contains("late") && p.Skills!.Contains("a"), 0, 5); yield return Case("no match", p => p.Skills!.Contains("rare") && p.Skills!.Contains("late"), 0, 5); } [Theory] [MemberData(nameof(Pages))] public async Task PageInObjectOrder_SameAnswer(string name, Expression> filter, int skip, int take) { var all = await SeedAsync(); var keep = filter.Compile(); var expected = Expected(all, keep, skip, take); await EachFormAsync(async form => { var plain = await _redb.Query().Where(filter).Skip(skip).Take(take).ToListAsync(); plain.Select(o => o.Id).Should().Equal(expected, $"{name}, no ordering, {form}"); var byId = await _redb.Query().Where(filter).OrderByRedb(o => o.Id).Skip(skip).Take(take).ToListAsync(); byId.Select(o => o.Id).Should().Equal(expected, $"{name}, by id, {form}"); var byIdDesc = await _redb.Query().Where(filter).OrderByDescendingRedb(o => o.Id).Skip(skip).Take(take).ToListAsync(); byIdDesc.Select(o => o.Id).Should().Equal( all.Where(o => keep(o.Props)).Select(o => o.Id).OrderByDescending(id => id).Skip(skip).Take(take).ToList(), $"{name}, by id descending, {form}"); }); } /// /// The walk is written for a page in object order only: not for a page sorted by a Props field or by the id descending, /// nor for a count. Read off the statement with the planner on (the streams form needs it). /// [Fact] public async Task Walk_OnlyForAPageInObjectOrder() { await SeedAsync(); var restore = _redb.Configuration.EnablePvtPrefilter; try { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = true); IRedbQueryable Query() => _redb.Query().Where(p => p.Skills!.Contains("a") && p.Skills!.Contains("b")); (await Query().Take(5).ToSqlStringAsync()).Should().Contain("@pvt_walk", "a page in object order"); (await Query().OrderByRedb(o => o.Id).Take(5).ToSqlStringAsync()).Should().Contain("@pvt_walk", "a page by id"); (await Query().OrderByDescendingRedb(o => o.Id).Take(5).ToSqlStringAsync()).Should().NotContain("@pvt_walk", "by id descending"); (await Query().OrderBy(p => p.Age).Take(5).ToSqlStringAsync()).Should().NotContain("@pvt_walk", "by a Props field"); (await Query().ToSqlStringAsync()).Should().NotContain("@pvt_walk", "no page"); } finally { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = restore); } } }