Files
lmxopcua/tests/Core/ZB.MOM.WW.OtOpcUa.Configuration.Tests/ReservationListQueryTests.cs
T
Joseph Doherty ea976ffa81 fix(perf): #996 Wave 4 — blob-free health probes, narrow data-connections load, off-thread coordinator acks + AdminOps audit, events page cap, capped reservations page
C-6: DeploymentQueries projects Deployment without ArtifactBlob (DbHealthProbe, readiness, ClusterOverview, DriverHost boot).
C-11/A-5: DraftSnapshotFactory.FromConfigDbForDataConnectionsAsync + one-parse-per-artifact LoadAppliedAsync + capped pickers.
H-6: ConfigPublishCoordinator persists acks off the actor thread (AckPersisted); deadline backlog kicked in parallel.
H-3: Restart/Reconnect reply = dispatch; ConfigEdit audit written off-mailbox, failure logged not surfaced.
U-6: ServerHistorian:MaxEventsPerRead (default 10000) pages HistoryReadEvents; advertised as MaxReturnEventValues.
A-8: /reservations active-only by default, projected, capped at 200.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-12-03 10:58:50 -05:00

64 lines
3.1 KiB
C#

using Microsoft.EntityFrameworkCore;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
using ZB.MOM.WW.OtOpcUa.Configuration.Queries;
namespace ZB.MOM.WW.OtOpcUa.Configuration.Tests;
/// <summary>lmxopcua#996 / review A-8: the /reservations page read is active-only by default, projected and capped.</summary>
[Trait("Category", "Unit")]
public sealed class ReservationListQueryTests
{
private static OtOpcUaConfigDbContext InMemory() =>
new(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>().UseInMemoryDatabase($"resv-{Guid.NewGuid():N}").Options);
private static void Seed(OtOpcUaConfigDbContext db, int active, int released)
{
for (var i = 0; i < active + released; i++)
db.ExternalIdReservations.Add(new ExternalIdReservation
{
ReservationId = Guid.NewGuid(), Kind = i % 2 == 0 ? ReservationKind.ZTag : ReservationKind.SAPID,
Value = $"v{i:0000}", EquipmentUuid = Guid.NewGuid(), ClusterId = "MAIN", FirstPublishedBy = "t",
ReleasedAt = i < active ? null : DateTime.UtcNow, ReleasedBy = i < active ? null : "t",
});
db.SaveChanges();
}
/// <summary>1,000 rows render at most the page size and the total is reported; released rows are hidden by default.</summary>
[Fact]
public async Task Page_is_capped_and_defaults_to_active_rows()
{
using var db = InMemory();
Seed(db, active: 700, released: 300);
var page = await ReservationListQuery.LoadAsync(db, includeReleased: false, ct: TestContext.Current.CancellationToken);
page.Rows.Count.ShouldBe(ReservationListQuery.DefaultPageSize);
page.Total.ShouldBe(700);
page.Rows.ShouldAllBe(r => r.ReleasedAt == null);
// Positive control: asking for released rows too changes the total (so the filter was what hid them).
var all = await ReservationListQuery.LoadAsync(db, includeReleased: true, pageSize: 50, ct: TestContext.Current.CancellationToken);
all.Total.ShouldBe(1000);
all.Rows.Count.ShouldBe(50);
}
/// <summary>The SQL selects only the displayed columns (no audit columns) and filters released rows server-side.</summary>
[Fact]
public void Query_projects_displayed_columns_and_filters_released_rows_in_sql()
{
using var db = new OtOpcUaConfigDbContext(new DbContextOptionsBuilder<OtOpcUaConfigDbContext>()
.UseSqlServer("Server=127.0.0.1,1;Database=never;Connect Timeout=1").Options);
var sql = ReservationListQuery.Query(db, includeReleased: false).ToQueryString();
sql.ShouldContain("[ReleasedAt] IS NULL");
sql.ShouldNotContain("FirstPublishedBy");
sql.ShouldNotContain("LastPublishedAt");
ReservationListQuery.Query(db, includeReleased: true).ToQueryString().ShouldNotContain("IS NULL");
// Positive control: the whole-entity read does select the audit columns.
db.ExternalIdReservations.AsNoTracking().ToQueryString().ShouldContain("FirstPublishedBy");
}
}