Path: redb/redb.Tests.Integration/Tests/Base/ListTestsBase.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/ListTestsBase.cs
----
using redb.Core;
using redb.Core.Models.Entities;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// RedbList (lookup/dictionary) CRUD and query tests.
///
public abstract class ListTestsBase
{
protected readonly IRedbService Redb;
protected virtual bool IsPro => false;
///
/// True if the underlying provider supports OrderBy/Where on `ListItem.Value` / `ListItem.Alias`
/// (requires a JOIN to `_list_items` or equivalent). All Postgres tiers (Free PVT + Pro) and
/// MsSql Pro support it; MsSql Free currently does not (ORDER BY is ignored, rows come back
/// in insertion order). Override to opt-in.
///
protected virtual bool SupportsListItemValueAliasOrdering => IsPro;
protected ListTestsBase(IRedbService redb) => Redb = redb;
private async Task CreateTestListAsync(string name = "TestStatuses")
{
// Cleanup if exists
var existing = await Redb.ListProvider.GetListByNameAsync(name);
if (existing != null)
{
var items = await Redb.ListProvider.GetListItemsAsync(existing.Id);
var itemIds = items.Select(i => i.Id).ToList();
if (itemIds.Count > 0)
await Redb.Context.Bulk.BulkDeleteValuesByListItemIdsAsync(itemIds);
await Redb.ListProvider.DeleteListAsync(existing.Id);
}
var list = RedbList.Create(name, $"{name} alias");
return await Redb.ListProvider.SaveListAsync(list);
}
private async Task CleanupListAsync(string name)
{
var existing = await Redb.ListProvider.GetListByNameAsync(name);
if (existing != null)
{
var items = await Redb.ListProvider.GetListItemsAsync(existing.Id);
var itemIds = items.Select(i => i.Id).ToList();
if (itemIds.Count > 0)
await Redb.Context.Bulk.BulkDeleteValuesByListItemIdsAsync(itemIds);
await Redb.ListProvider.DeleteListAsync(existing.Id);
}
}
[Fact]
public async Task CreateList_Persists()
{
var saved = await CreateTestListAsync("ListTest_Create");
saved.Id.Should().BeGreaterThan(0);
saved.Name.Should().Be("ListTest_Create");
await CleanupListAsync("ListTest_Create");
}
[Fact]
public async Task GetListByName_ReturnsExisting()
{
var saved = await CreateTestListAsync("ListTest_GetByName");
var found = await Redb.ListProvider.GetListByNameAsync("ListTest_GetByName");
found.Should().NotBeNull();
found!.Id.Should().Be(saved.Id);
await CleanupListAsync("ListTest_GetByName");
}
[Fact]
public async Task AddItems_PersistsItems()
{
var list = await CreateTestListAsync("ListTest_Items");
var values = new[] { "Active", "Inactive", "Pending" };
var aliases = new[] { "Active status", "Inactive status", "Pending review" };
var added = await Redb.ListProvider.AddItemsAsync(list, values, aliases);
added.Should().HaveCount(3);
added.Select(i => i.Value).Should().BeEquivalentTo(values);
await CleanupListAsync("ListTest_Items");
}
[Fact]
public async Task GetListItems_ReturnsAll()
{
var list = await CreateTestListAsync("ListTest_GetItems");
await Redb.ListProvider.AddItemsAsync(list, ["A", "B", "C"]);
var items = await Redb.ListProvider.GetListItemsAsync(list.Id);
items.Should().HaveCount(3);
await CleanupListAsync("ListTest_GetItems");
}
[Fact]
public async Task GetListWithItems_LoadsItemsToo()
{
var list = await CreateTestListAsync("ListTest_WithItems");
await Redb.ListProvider.AddItemsAsync(list, ["X", "Y"]);
var loaded = await Redb.ListProvider.GetListWithItemsAsync(list.Id);
loaded.Should().NotBeNull();
loaded!.Items.Should().HaveCount(2);
await CleanupListAsync("ListTest_WithItems");
}
[Fact]
public async Task GetListItemByValue_FindsMatch()
{
var list = await CreateTestListAsync("ListTest_ByValue");
await Redb.ListProvider.AddItemsAsync(list, ["Alpha", "Beta"]);
var item = await Redb.ListProvider.GetListItemByValueAsync(list.Id, "Alpha");
item.Should().NotBeNull();
item!.Value.Should().Be("Alpha");
await CleanupListAsync("ListTest_ByValue");
}
[Fact]
public async Task DeleteList_Removes()
{
var list = await CreateTestListAsync("ListTest_Delete");
var deleted = await Redb.ListProvider.DeleteListAsync(list.Id);
deleted.Should().BeTrue();
var found = await Redb.ListProvider.GetListByNameAsync("ListTest_Delete");
found.Should().BeNull();
}
[Fact]
public async Task ListItemOnProps_SaveAndQuery()
{
// Setup: create a list with items
var list = await CreateTestListAsync("ListTest_Props");
var statuses = await Redb.ListProvider.AddItemsAsync(list, ["Active", "Blocked"]);
var activeItem = statuses.First(s => s.Value == "Active");
var blockedItem = statuses.First(s => s.Value == "Blocked");
// Save a Person with Status = Active
var person = new RedbObject
{
name = "Test Person",
Props = new PersonProps
{
Name = "John",
Age = 30,
Email = "john@test.com",
Status = activeItem,
Roles = [activeItem, blockedItem]
}
};
person.id = await Redb.SaveAsync(person);
// Load and verify
var loaded = await Redb.LoadAsync(person.id);
loaded.Should().NotBeNull();
loaded!.Props.Status.Should().NotBeNull();
loaded.Props.Status!.Value.Should().Be("Active");
loaded.Props.Roles.Should().HaveCount(2);
await CleanupListAsync("ListTest_Props");
}
[Fact]
public async Task ListItemArray_QueryAny()
{
// Setup
var list = await CreateTestListAsync("ListTest_QueryAny");
var items = await Redb.ListProvider.AddItemsAsync(list, ["Admin", "User", "Viewer"]);
var adminItem = items.First(i => i.Value == "Admin");
var userItem = items.First(i => i.Value == "User");
var person = new RedbObject
{
name = "Admin User",
Props = new PersonProps
{
Name = "AdminPerson",
Age = 35,
Email = "admin@test.com",
Roles = [adminItem, userItem]
}
};
person.id = await Redb.SaveAsync(person);
// Query: find persons with Admin role
var results = await Redb.Query()
.Where(p => p.Roles!.Any(r => r.Value == "Admin"))
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().Contain(r => r.id == person.id);
await CleanupListAsync("ListTest_QueryAny");
}
[Fact]
public async Task SyncListFromEnum_CreatesItems()
{
// Cleanup if exists
await CleanupListAsync("TestPriority");
var list = await Redb.ListProvider.SyncListFromEnumAsync();
list.Should().NotBeNull();
list.Id.Should().BeGreaterThan(0);
var items = await Redb.ListProvider.GetListItemsAsync(list.Id);
items.Select(i => i.Value).Should().BeEquivalentTo(["Low", "Medium", "High", "Critical"]);
await CleanupListAsync("TestPriority");
}
[Fact]
public async Task ListItem_ClosureContains_FiltersCorrectly()
{
// Setup: create list with items and save persons with different statuses
var list = await CreateTestListAsync("ListTest_ClosureContains");
var statuses = await Redb.ListProvider.AddItemsAsync(list, ["Active", "Blocked", "Pending"]);
var activeItem = statuses.First(s => s.Value == "Active");
var blockedItem = statuses.First(s => s.Value == "Blocked");
var pendingItem = statuses.First(s => s.Value == "Pending");
var person1 = new RedbObject
{
name = "Person Active",
Props = new PersonProps { Name = "Alice", Age = 25, Email = "alice@test.com", Status = activeItem }
};
var person2 = new RedbObject
{
name = "Person Blocked",
Props = new PersonProps { Name = "Bob", Age = 30, Email = "bob@test.com", Status = blockedItem }
};
var person3 = new RedbObject
{
name = "Person Pending",
Props = new PersonProps { Name = "Carol", Age = 35, Email = "carol@test.com", Status = pendingItem }
};
var batchIds = await Redb.SaveAsync(new[] { person1, person2, person3 });
person1.id = batchIds[0];
person2.id = batchIds[1];
person3.id = batchIds[2];
// Query: find persons where Status IN (Active, Pending) — closure Contains pattern
var selected = new List { activeItem, pendingItem };
var results = await Redb.Query()
.Where(p => selected.Contains(p.Status!))
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().Contain(r => r.id == person1.id, "Active person should be found");
results.Should().Contain(r => r.id == person3.id, "Pending person should be found");
results.Should().NotContain(r => r.id == person2.id, "Blocked person should NOT be found");
await CleanupListAsync("ListTest_ClosureContains");
}
[Fact]
public async Task ListItem_ArrayContains_FiltersCorrectly()
{
// Setup: create list with items and save person with Roles array
var list = await CreateTestListAsync("ListTest_ArrayContains");
var roles = await Redb.ListProvider.AddItemsAsync(list, ["Admin", "User", "Viewer"]);
var adminItem = roles.First(r => r.Value == "Admin");
var userItem = roles.First(r => r.Value == "User");
var viewerItem = roles.First(r => r.Value == "Viewer");
var person1 = new RedbObject
{
name = "Admin User",
Props = new PersonProps { Name = "Dave", Age = 40, Email = "dave@test.com", Roles = [adminItem, userItem] }
};
var person2 = new RedbObject
{
name = "Viewer Only",
Props = new PersonProps { Name = "Eve", Age = 28, Email = "eve@test.com", Roles = [viewerItem] }
};
var batchIds = await Redb.SaveAsync(new[] { person1, person2 });
person1.id = batchIds[0];
person2.id = batchIds[1];
// Query: x.Roles.Contains(adminItem) — array Contains pattern
var results = await Redb.Query()
.Where(p => p.Roles!.Contains(adminItem))
.ToListAsync();
results.Should().NotBeEmpty();
results.Should().Contain(r => r.id == person1.id, "Person with Admin role should be found");
results.Should().NotContain(r => r.id == person2.id, "Viewer-only person should NOT be found");
await CleanupListAsync("ListTest_ArrayContains");
}
[Fact]
public async Task ListItem_OrderByValue_SortsAlphabetically()
{
// Setup: create list with items that have known alphabetical order
var list = await CreateTestListAsync("ListTest_OrderByValue");
// Values: Alpha < Beta < Gamma (alphabetical order)
var statuses = await Redb.ListProvider.AddItemsAsync(list, ["Gamma", "Alpha", "Beta"]);
var gammaItem = statuses.First(s => s.Value == "Gamma");
var alphaItem = statuses.First(s => s.Value == "Alpha");
var betaItem = statuses.First(s => s.Value == "Beta");
// Use unique per-run prefix to avoid cross-test pollution in PersonProps scheme
var tag = $"OrderByValue_{Guid.NewGuid():N}";
var person1 = new RedbObject
{
name = "Person Gamma",
Props = new PersonProps { Name = $"{tag}_P1", Age = 20, Email = "p1@test.com", Status = gammaItem }
};
var person2 = new RedbObject
{
name = "Person Alpha",
Props = new PersonProps { Name = $"{tag}_P2", Age = 30, Email = "p2@test.com", Status = alphaItem }
};
var person3 = new RedbObject
{
name = "Person Beta",
Props = new PersonProps { Name = $"{tag}_P3", Age = 40, Email = "p3@test.com", Status = betaItem }
};
var batchIds = await Redb.SaveAsync(new[] { person1, person2, person3 });
person1.id = batchIds[0];
person2.id = batchIds[1];
person3.id = batchIds[2];
// Query: OrderBy Status.Value — should sort alphabetically by list item text
var results = await Redb.Query()
.Where(p => p.Name == $"{tag}_P1" || p.Name == $"{tag}_P2" || p.Name == $"{tag}_P3")
.OrderBy(p => p.Status!.Value)
.ToListAsync();
results.Should().HaveCount(3);
if (SupportsListItemValueAliasOrdering)
{
// Provider supports JOIN to _list_items (Postgres Free + Pro, MsSql Pro):
// OrderBy Status.Value must sort alphabetically by the list item text.
var values = results.Select(r => r.Props.Status!.Value).ToList();
values.Should().BeEquivalentTo(["Alpha", "Beta", "Gamma"],
opts => opts.WithStrictOrdering(),
"OrderBy Status.Value should sort alphabetically by list item text");
}
await CleanupListAsync("ListTest_OrderByValue");
}
[Fact]
public async Task ListItem_OrderByAlias_SortsAlphabetically()
{
// Setup: create list with items that have known aliases in reverse alphabetical order
var list = await CreateTestListAsync("ListTest_OrderByAlias");
var values = new[] { "Status1", "Status2", "Status3" };
var aliases = new[] { "Cherry", "Apple", "Banana" };
var statuses = await Redb.ListProvider.AddItemsAsync(list, values, aliases);
var s1 = statuses.First(s => s.Alias == "Cherry");
var s2 = statuses.First(s => s.Alias == "Apple");
var s3 = statuses.First(s => s.Alias == "Banana");
// Use unique per-run prefix to avoid cross-test pollution in PersonProps scheme
var aliasTag = $"OrderByAlias_{Guid.NewGuid():N}";
var person1 = new RedbObject
{
name = "Person Cherry",
Props = new PersonProps { Name = $"{aliasTag}_A1", Age = 20, Email = "a1@test.com", Status = s1 }
};
var person2 = new RedbObject
{
name = "Person Apple",
Props = new PersonProps { Name = $"{aliasTag}_A2", Age = 30, Email = "a2@test.com", Status = s2 }
};
var person3 = new RedbObject
{
name = "Person Banana",
Props = new PersonProps { Name = $"{aliasTag}_A3", Age = 40, Email = "a3@test.com", Status = s3 }
};
var batchIds = await Redb.SaveAsync(new[] { person1, person2, person3 });
person1.id = batchIds[0];
person2.id = batchIds[1];
person3.id = batchIds[2];
// Query: OrderBy Status.Alias — should sort alphabetically by alias
var results = await Redb.Query()
.Where(p => p.Name == $"{aliasTag}_A1" || p.Name == $"{aliasTag}_A2" || p.Name == $"{aliasTag}_A3")
.OrderBy(p => p.Status!.Alias)
.ToListAsync();
results.Should().HaveCount(3);
if (SupportsListItemValueAliasOrdering)
{
// Provider supports JOIN to _list_items (Postgres Free + Pro, MsSql Pro):
// OrderBy Status.Alias must sort alphabetically by alias.
var resultAliases = results.Select(r => r.Props.Status!.Alias).ToList();
resultAliases.Should().BeEquivalentTo(["Apple", "Banana", "Cherry"],
opts => opts.WithStrictOrdering(),
"OrderBy Status.Alias should sort alphabetically by list item alias");
}
await CleanupListAsync("ListTest_OrderByAlias");
}
}
public enum TestPriority
{
Low,
Medium,
High,
Critical
}