Path: redb-route/tests/redb.Route.Tests.Core/RedbQueryDslTests.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Core/RedbQueryDslTests.cs
----
using System.Linq.Expressions;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Query;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Definitions;
using redb.Route.RedbCore.Extensions;
using redb.Route.RedbCore.Query;
namespace redb.Route.Tests.Core;
///
/// The RedbQuery verb (step 2): the where-string rides the engine's ONE
/// expression AST into redb LINQ. The queryable is substituted and the predicate that reaches
/// Where is COMPILED and probed against sample props — semantic checks, no database.
///
public sealed class RedbQueryDslTests
{
public sealed class CustomerProps
{
public string? Name { get; set; }
}
public sealed class OrderProps
{
public string? Status { get; set; }
public int Total { get; set; }
public int Limit { get; set; }
public string? Note { get; set; }
public bool Urgent { get; set; }
public CustomerProps? Customer { get; set; }
}
private readonly IRedbService _redb = Substitute.For();
private readonly IRouteContext _context = Substitute.For();
private readonly IOrderedRedbQueryable _query = Substitute.For>();
private Expression>? _captured;
private Expression>? _capturedRedb;
public RedbQueryDslTests()
{
_context.GetService().Returns(_redb);
_redb.Query().Returns(_query);
_query.Where(Arg.Do>>(e => _captured = e)).Returns(_query);
_query.WhereRedb(Arg.Do>>(e => _capturedRedb = e)).Returns(_query);
_query.OrderBy(Arg.Any>>()).Returns(_query);
_query.OrderByDescending(Arg.Any>>()).Returns(_query);
_query.Skip(Arg.Any()).Returns(_query);
_query.Take(Arg.Any()).Returns(_query);
_query.ToListAsync().Returns([]);
}
private RouteDefinition NewRoute()
{
var route = new RouteDefinition();
route._context = _context;
return route;
}
private async Task> PredicateFor(string where,
params (string Key, object? Value)[] headers)
{
var route = NewRoute();
route.RedbQuery(typeof(OrderProps), where: where);
var exchange = new Exchange();
foreach (var (key, value) in headers)
exchange.In.Headers[key] = value;
await route.Outputs[0].CreateProcessor(_context).Process(exchange, CancellationToken.None);
_captured.Should().NotBeNull();
return _captured!.Compile();
}
// ── translation semantics (predicate probed on samples) ──────────
[Fact]
public async Task Comparison_OnAStringProp()
{
var predicate = await PredicateFor("Status == 'open'");
predicate(new OrderProps { Status = "open" }).Should().BeTrue();
predicate(new OrderProps { Status = "closed" }).Should().BeFalse();
}
[Fact]
public async Task ValueSide_FoldsPerMessage_FromTheHeader()
{
var predicate = await PredicateFor("Total > header.min", ("min", 10));
predicate(new OrderProps { Total = 11 }).Should().BeTrue();
predicate(new OrderProps { Total = 10 }).Should().BeFalse();
}
[Fact]
public async Task AndOrNot_Combine()
{
var predicate = await PredicateFor("(Status == 'open' OR Urgent == true) AND NOT (Total < 5)");
predicate(new OrderProps { Status = "open", Total = 5 }).Should().BeTrue();
predicate(new OrderProps { Status = "closed", Urgent = true, Total = 7 }).Should().BeTrue();
predicate(new OrderProps { Status = "closed", Total = 7 }).Should().BeFalse();
predicate(new OrderProps { Status = "open", Total = 4 }).Should().BeFalse();
}
[Fact]
public async Task Contains_TranslatesOntoTheStringProp()
{
var predicate = await PredicateFor("contains(Note, header.q)", ("q", "url"));
predicate(new OrderProps { Note = "urgently" }).Should().BeFalse();
predicate(new OrderProps { Note = "curl it" }).Should().BeTrue();
}
[Fact]
public async Task NestedPath_WalksTheClrProperties()
{
var predicate = await PredicateFor("Customer.Name == 'acme'");
predicate(new OrderProps { Customer = new CustomerProps { Name = "acme" } }).Should().BeTrue();
predicate(new OrderProps { Customer = new CustomerProps { Name = "other" } }).Should().BeFalse();
}
[Fact]
public async Task MirroredComparison_ValueOnTheLeft()
{
var predicate = await PredicateFor("50 <= Total");
predicate(new OrderProps { Total = 50 }).Should().BeTrue();
predicate(new OrderProps { Total = 49 }).Should().BeFalse();
}
[Fact]
public async Task NullComparison_OnANullableProp()
{
var predicate = await PredicateFor("Note == null");
predicate(new OrderProps()).Should().BeTrue();
predicate(new OrderProps { Note = "x" }).Should().BeFalse();
}
[Fact]
public async Task ValueOnlyGate_FoldsToABooleanConstant()
{
var open = await PredicateFor("header.enabled == true AND Status == 'open'", ("enabled", true));
open(new OrderProps { Status = "open" }).Should().BeTrue();
var gated = await PredicateFor("header.enabled == true AND Status == 'open'", ("enabled", false));
gated(new OrderProps { Status = "open" }).Should().BeFalse("the gate folded to false for THIS message");
}
[Theory]
[InlineData("Total > Limit", 5, 3, true)]
[InlineData("Total > Limit", 3, 5, false)]
[InlineData("Limit < Total", 5, 3, true)]
[InlineData("Limit < Total", 3, 5, false)]
[InlineData("Total == Limit", 4, 4, true)]
[InlineData("Limit == Total", 4, 5, false)]
public async Task AFieldAgainstAnotherField_ComparesTheTwoFields(string where, int total, int limit, bool expected)
{
var predicate = await PredicateFor(where);
predicate(new OrderProps { Total = total, Limit = limit }).Should().Be(expected);
}
// ── loud refusals at route build ─────────────────────────────────
[Fact]
public void AFieldAgainstAComputedField_RefusesWithTheSpecHint()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps), where: "Total > Limit * 2");
act.Should().Throw().WithMessage("*#spec*");
}
[Fact]
public void ArithmeticOnProps_RefusesWithTheSpecHint()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps), where: "Total % 2 == 0");
act.Should().Throw().WithMessage("*#spec*");
}
[Fact]
public void UnknownIdentifier_RefusesNamingTheCandidates()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps), where: "Statuss == 'open'");
act.Should().Throw()
.WithMessage("*Statuss*").WithMessage("*Status*", "the property list helps fix the typo");
}
[Fact]
public void OrderingComparisonOnAString_RefusesWithTheHint()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps), where: "Status > 'a'");
act.Should().Throw()
.WithMessage("*string props property*").WithMessage("*#spec*");
}
[Fact]
public void UnboundedScan_Refuses()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps));
act.Should().Throw().WithMessage("*unbounded*");
}
[Fact]
public void OrderByANonPropsPath_Refuses()
{
var act = () => NewRoute().RedbQuery(typeof(OrderProps), orderBy: "header.x");
act.Should().Throw().WithMessage("*props property path*");
}
// ── query assembly ───────────────────────────────────────────────
[Fact]
public async Task OrderBySkipTake_ReachTheQueryable()
{
var route = NewRoute();
route.RedbQuery(typeof(OrderProps), where: "Status == 'open'",
orderBy: "Total", descending: true, skip: 20, take: 10);
await route.Outputs[0].CreateProcessor(_context).Process(new Exchange(), CancellationToken.None);
_query.Received(1).OrderByDescending(Arg.Any>>());
_query.Received(1).Skip(20);
_query.Received(1).Take(10);
await _query.Received(1).ToListAsync();
}
[Fact]
public async Task FilterSpec_AppliesFromTheRegistry()
{
var spec = Substitute.For>();
spec.Apply(_query).Returns(_query);
_context.GetFromRegistry