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; /// lmxopcua#996 / review A-8: the /reservations page read is active-only by default, projected and capped. [Trait("Category", "Unit")] public sealed class ReservationListQueryTests { private static OtOpcUaConfigDbContext InMemory() => new(new DbContextOptionsBuilder().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(); } /// 1,000 rows render at most the page size and the total is reported; released rows are hidden by default. [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); } /// The SQL selects only the displayed columns (no audit columns) and filters released rows server-side. [Fact] public void Query_projects_displayed_columns_and_filters_released_rows_in_sql() { using var db = new OtOpcUaConfigDbContext(new DbContextOptionsBuilder() .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"); } }