Lists need one component for the whole app (SPEC §10.6-§10.7): a list query model that turns search, filters, sort and paging into SQL or runs over rows in memory, a KhGrid that pages 100,000 rows while scrolling (or in pages) with sort, selection, keyboard, inline edit, totals, row menu, saved column layout and CSV/XLSX export, the KhRadar around it with search, quick filters, a filter builder and saved filters, a radar window for F4 lookups, and KhLinesGrid for document lines typed from the keyboard. Checked in the browser on the component catalog and the sample document. Fixed on the way: the grid height lost to the flex basis, a new search kept the old scroll position, Save of a filter needed a second click, page shortcuts (Ctrl+Canc) reached the record behind a modal window, Escape did nothing once focus left a dialog, totals did not follow an inline edit, and amounts exported as 0 instead of 0,00. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
7.0 KiB
C#
158 lines
7.0 KiB
C#
using Khaemhat.Application.Querying;
|
|
using Khaemhat.Data.Mapping;
|
|
using Khaemhat.Data.Querying;
|
|
using Khaemhat.Data.Sqlite;
|
|
|
|
namespace Khaemhat.Application.Tests;
|
|
|
|
/// <summary>The request of grids and radars, run in memory and translated to SQL (issue #28).</summary>
|
|
[TestClass]
|
|
public sealed class ListQueryTests
|
|
{
|
|
private static readonly ListFields<ListRow> Fields = new ListFields<ListRow>()
|
|
.Add("code", x => x.Code, searchable: true)
|
|
.Add("name", x => x.Name, searchable: true)
|
|
.Add("amount", x => x.Amount, total: true)
|
|
.Add("due", x => x.Due)
|
|
.Add("active", x => x.Active)
|
|
.Add("state", x => x.State)
|
|
.OrderByDefault(new ListSort("code"));
|
|
|
|
private static readonly ListRow[] Rows =
|
|
[
|
|
new() { Id = 1, Code = "C003", Name = "Rossi Impianti", Amount = 100m, Due = new DateOnly(2026, 11, 30), Active = true, State = RowState.Open },
|
|
new() { Id = 2, Code = "C001", Name = "Bianchi e Figli", Amount = 250.5m, Due = null, Active = true, State = RowState.Closed },
|
|
new() { Id = 3, Code = "C002", Name = "rossi torino", Amount = -20m, Due = new DateOnly(2026, 12, 15), Active = false, State = RowState.Open },
|
|
new() { Id = 4, Code = "C004", Name = null, Amount = 0m, Due = new DateOnly(2027, 1, 10), Active = true, State = RowState.Blocked },
|
|
];
|
|
|
|
[TestMethod]
|
|
public void Memory_SearchMatchesEveryWordIgnoringCase()
|
|
{
|
|
var page = Fields.Apply(Rows, new ListQuery { Search = "ROSSI" });
|
|
CollectionAssert.AreEqual(new[] { "C002", "C003" }, page.Items.Select(r => r.Code).ToArray());
|
|
|
|
var both = Fields.Apply(Rows, new ListQuery { Search = "rossi torino" });
|
|
Assert.AreEqual("C002", both.Items.Single().Code);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Memory_FiltersSortTotalsAndPage()
|
|
{
|
|
var query = new ListQuery
|
|
{
|
|
Filters = [ListFilter.Of("active", ListFilterOperator.Equals, "true"), ListFilter.Of("amount", ListFilterOperator.GreaterOrEqual, "0")],
|
|
Sort = [new ListSort("amount", Descending: true)],
|
|
Offset = 1,
|
|
Limit = 1,
|
|
};
|
|
|
|
var page = Fields.Apply(Rows, query);
|
|
|
|
Assert.AreEqual(3, page.Total);
|
|
Assert.AreEqual("C003", page.Items.Single().Code);
|
|
Assert.AreEqual(350.5m, page.Totals["amount"]);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("due", ListFilterOperator.IsEmpty, new string[0], "C001")]
|
|
[DataRow("name", ListFilterOperator.IsEmpty, new string[0], "C004")]
|
|
[DataRow("due", ListFilterOperator.Between, new[] { "2026-12-01", "2026-12-31" }, "C002")]
|
|
[DataRow("state", ListFilterOperator.In, new[] { "Blocked", "Closed" }, "C001,C004")]
|
|
[DataRow("state", ListFilterOperator.NotEquals, new[] { "Open" }, "C001,C004")]
|
|
[DataRow("name", ListFilterOperator.StartsWith, new[] { "BIAN" }, "C001")]
|
|
[DataRow("name", ListFilterOperator.Equals, new[] { "ROSSI TORINO" }, "C002")]
|
|
[DataRow("amount", ListFilterOperator.LessThan, new[] { "0" }, "C002")]
|
|
[DataRow("due", ListFilterOperator.IsNotEmpty, new string[0], "C002,C003,C004")]
|
|
public void Memory_Operators(string field, ListFilterOperator op, string[] values, string expected)
|
|
{
|
|
var page = Fields.Apply(Rows, new ListQuery { Filters = [new ListFilter(field, op, values)] });
|
|
Assert.AreEqual(expected, string.Join(',', page.Items.Select(r => r.Code)), $"{field} {op}");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void InvalidFilters_AreRecognised()
|
|
{
|
|
Assert.IsTrue(Fields.IsValid(ListFilter.Of("amount", ListFilterOperator.GreaterThan, "12.5")));
|
|
Assert.IsFalse(Fields.IsValid(ListFilter.Of("missing", ListFilterOperator.Equals, "x")));
|
|
Assert.IsFalse(Fields.IsValid(ListFilter.Of("amount", ListFilterOperator.GreaterThan, "dodici")));
|
|
Assert.IsFalse(Fields.IsValid(ListFilter.Of("amount", ListFilterOperator.Contains, "1")));
|
|
Assert.IsFalse(Fields.IsValid(ListFilter.Of("name", ListFilterOperator.GreaterThan, "A")));
|
|
Assert.IsFalse(Fields.IsValid(ListFilter.Of("due", ListFilterOperator.Between, "2026-01-01")));
|
|
Assert.ThrowsExactly<ArgumentException>(() => new ListFields<ListRow>().Add("amount", x => x.Amount, searchable: true));
|
|
Assert.ThrowsExactly<ArgumentException>(() => new ListFields<ListRow>().Add("name", x => x.Name, total: true));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Sql_UsesLikeParametersOrderAndPage()
|
|
{
|
|
var map = new ListRowMap();
|
|
var query = Fields.Apply(Query.For<ListRow>(), new ListQuery
|
|
{
|
|
Search = "rossi",
|
|
Filters = [ListFilter.Of("amount", ListFilterOperator.Between, "10", "20.5"), ListFilter.Of("state", ListFilterOperator.In, "Open", "Closed")],
|
|
Sort = [new ListSort("due", Descending: true)],
|
|
Offset = 100,
|
|
Limit = 50,
|
|
});
|
|
|
|
var sql = new QueryBuilder<ListRow>(map, SqliteDialect.Instance).Select(query).Render(SqliteDialect.Instance);
|
|
|
|
StringAssert.Contains(sql.Text, "(kh_lower(\"code\") LIKE kh_lower(@p0) ESCAPE '\\' OR kh_lower(\"name\") LIKE kh_lower(@p1) ESCAPE '\\')");
|
|
StringAssert.Contains(sql.Text, "(\"amount\" >= @p2 AND \"amount\" <= @p3)");
|
|
StringAssert.Contains(sql.Text, "\"state\" IN (@p4, @p5)");
|
|
StringAssert.Contains(sql.Text, "ORDER BY \"due\" DESC, \"code\" ASC");
|
|
StringAssert.Contains(sql.Text, "100");
|
|
CollectionAssert.AreEqual(new object?[] { "%rossi%", "%rossi%", 10m, 20.5m, "Open", "Closed" }, sql.Parameters.Select(p => p.Value).ToArray());
|
|
}
|
|
|
|
[TestMethod]
|
|
public async Task InMemorySource_PagesOfALargeListAreQuick()
|
|
{
|
|
var rows = Enumerable.Range(1, 100_000)
|
|
.Select(i => new ListRow { Id = i, Code = "C" + i.ToString("D6", System.Globalization.CultureInfo.InvariantCulture), Name = "Cliente " + (i % 977), Amount = i % 1000 })
|
|
.ToList();
|
|
var source = new InMemoryListSource<ListRow>(rows, Fields);
|
|
var query = new ListQuery { Search = "cliente 12", Sort = [new ListSort("amount", true)], Limit = 100 };
|
|
|
|
var first = await source.QueryAsync(query, CancellationToken.None);
|
|
var watch = System.Diagnostics.Stopwatch.StartNew();
|
|
for (var page = 1; page < 50; page++)
|
|
{
|
|
_ = await source.QueryAsync(query.WithPage(page * 100, 100), CancellationToken.None);
|
|
}
|
|
|
|
Assert.IsGreaterThan(0, first.Total);
|
|
Assert.IsLessThan(1000, watch.ElapsedMilliseconds, "49 pages after the first should come from the kept selection");
|
|
}
|
|
|
|
public enum RowState
|
|
{
|
|
Open = 0,
|
|
Closed = 1,
|
|
Blocked = 2,
|
|
}
|
|
|
|
public sealed class ListRow
|
|
{
|
|
public long Id { get; set; }
|
|
|
|
public string Code { get; set; } = string.Empty;
|
|
|
|
public string? Name { get; set; }
|
|
|
|
public decimal Amount { get; set; }
|
|
|
|
public DateOnly? Due { get; set; }
|
|
|
|
public bool Active { get; set; }
|
|
|
|
public RowState State { get; set; }
|
|
}
|
|
|
|
private sealed class ListRowMap : EntityMap<ListRow>
|
|
{
|
|
public ListRowMap() => ToTable("list_rows");
|
|
}
|
|
}
|