Path: redb/redb.Tests.Integration/Tests/Base/EnumComparisonTestsBase.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/EnumComparisonTestsBase.cs
----
using System.Linq.Expressions;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// A filter on an enum field, nullable or not, with a constant or a captured value, by == and !=, against null, and in a
/// Contains: each returns the ids the same filter returns on the text twin that holds the stored name.
///
public abstract class EnumComparisonTestsBase
{
protected readonly IRedbService Redb;
protected EnumComparisonTestsBase(IRedbService redb) => Redb = redb;
[Fact]
public async Task EnumFilters_ReturnTheIdsOfTheSameFilterOnTheStoredName()
{
await Redb.SyncSchemeAsync();
var tag = $"enum-cmp-{Guid.NewGuid():N}";
var shades = new ComparisonShade?[] { ComparisonShade.Red, ComparisonShade.Green, ComparisonShade.Blue, null };
var objects = Enumerable.Range(0, 8).Select(i =>
{
var maybe = shades[i % 4];
var always = (ComparisonShade)(i % 3);
return new RedbObject
{
name = $"{tag}-{i}",
Props = new EnumComparisonProps
{
Tag = tag, Maybe = maybe, MaybeName = maybe?.ToString(), Always = always, AlwaysName = always.ToString(),
}
};
}).ToList();
await Redb.SaveAsync(objects);
ComparisonShade? maybeGreen = ComparisonShade.Green;
var green = ComparisonShade.Green;
ComparisonShade? noShade = null;
var nullable = new List { ComparisonShade.Red, ComparisonShade.Blue };
var plain = new List { ComparisonShade.Red, ComparisonShade.Blue };
var names = new List { "Red", "Blue" };
var cases = new (string Name, Expression> Enum, Expression> Text)[]
{
("nullable == constant", p => p.Maybe == ComparisonShade.Green, p => p.MaybeName == "Green"),
("nullable != constant", p => p.Maybe != ComparisonShade.Green, p => p.MaybeName != "Green"),
("constant == nullable", p => ComparisonShade.Green == p.Maybe, p => p.MaybeName == "Green"),
("nullable == captured nullable", p => p.Maybe == maybeGreen, p => p.MaybeName == "Green"),
("nullable != captured nullable", p => p.Maybe != maybeGreen, p => p.MaybeName != "Green"),
("nullable == captured plain", p => p.Maybe == green, p => p.MaybeName == "Green"),
("nullable == null", p => p.Maybe == null, p => p.MaybeName == null),
("nullable != null", p => p.Maybe != null, p => p.MaybeName != null),
("nullable == captured null", p => p.Maybe == noShade, p => p.MaybeName == null),
("nullable in Contains", p => nullable.Contains(p.Maybe), p => names.Contains(p.MaybeName)),
("plain == constant", p => p.Always == ComparisonShade.Green, p => p.AlwaysName == "Green"),
("plain != constant", p => p.Always != ComparisonShade.Green, p => p.AlwaysName != "Green"),
("plain == captured", p => p.Always == green, p => p.AlwaysName == "Green"),
("plain != captured", p => p.Always != green, p => p.AlwaysName != "Green"),
("plain in Contains", p => plain.Contains(p.Always), p => names.Contains(p.AlwaysName)),
};
var failures = new List();
foreach (var (name, byEnum, byText) in cases)
{
var expected = await IdsAsync(tag, byText);
try
{
var got = await IdsAsync(tag, byEnum);
if (!got.SequenceEqual(expected))
failures.Add($"{name}: [{string.Join(",", got)}], the name gives [{string.Join(",", expected)}]");
}
catch (Exception ex)
{
failures.Add($"{name}: {ex.GetType().Name}: {ex.Message.Split('\n')[0]}");
}
}
failures.Should().BeEmpty(string.Join("; ", failures));
}
private async Task> IdsAsync(string tag, Expression> filter)
=> (await Redb.Query().Where(p => p.Tag == tag).Where(filter).ToListAsync())
.Select(o => o.Id).OrderBy(id => id).ToList();
}