From 4dc682f6e19108cc4e41e2aabf82882a0a39cd4f Mon Sep 17 00:00:00 2001 From: Louis-Axel Ambroise Date: Thu, 28 Mar 2024 13:37:39 +0100 Subject: [PATCH] Fix WD_ query parameters Fixes #505 --- .../Database/DataSources/EventQueryContext.cs | 4 +- .../Database/EpcisContext.cs | 11 ++-- .../DataSources/EventQueryContextTests.cs | 56 +++++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/FasTnT.Application/Database/DataSources/EventQueryContext.cs b/src/FasTnT.Application/Database/DataSources/EventQueryContext.cs index 6baeac25..7ef7f568 100644 --- a/src/FasTnT.Application/Database/DataSources/EventQueryContext.cs +++ b/src/FasTnT.Application/Database/DataSources/EventQueryContext.cs @@ -82,9 +82,9 @@ private void ParseParameter(QueryParameter param) case "LT_errorDeclarationTime": Filter(x => x.CorrectiveDeclarationTime < param.AsDate()); break; case "WD_readPoint": - Filter(x => _context.ReadPointHierarchy(x.ReadPoint).Any(h => param.Values.Contains(h.Id))); break; + Filter(x => _context.Set().Any(h => h.Type == MasterData.ReadPoint && h.Root == x.ReadPoint && param.Values.Contains(h.Id))); break; case "WD_bizLocation": - Filter(x => _context.LocationHierarchy(x.BusinessLocation).Any(h => param.Values.Contains(h.Id))); break; + Filter(x => _context.Set().Any(h => h.Type == MasterData.Location && h.Root == x.BusinessLocation && param.Values.Contains(h.Id))); break; case "EQ_requestID": Filter(x => param.Values.Select(int.Parse).Contains(x.Request.Id)); break; case "EQ_captureID": diff --git a/src/FasTnT.Application/Database/EpcisContext.cs b/src/FasTnT.Application/Database/EpcisContext.cs index c004bc39..c1a34a50 100644 --- a/src/FasTnT.Application/Database/EpcisContext.cs +++ b/src/FasTnT.Application/Database/EpcisContext.cs @@ -14,6 +14,11 @@ public EpcisContext(DbContextOptions options) : base(options) ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; } + public IQueryable QueryHierarchy(string type, string root) + { + return Set().Where(x => x.Type == type && x.Root == root); + } + public IQueryable QueryEvents(IEnumerable parameters) { var eventContext = new EventQueryContext(this, parameters); @@ -27,15 +32,9 @@ public IQueryable QueryMasterData(IEnumerable parame return masterdataContext.ApplyTo(Set()); } - - public IEnumerable LocationHierarchy(string id) => QueryHierarchy(this, MasterData.Location, id); - public IEnumerable ReadPointHierarchy(string id) => QueryHierarchy(this, MasterData.ReadPoint, id); - protected override void OnModelCreating(ModelBuilder modelBuilder) { EpcisModelConfiguration.Apply(modelBuilder); } - - static Func> QueryHierarchy => EF.CompileQuery((EpcisContext ctx, string type, string root) => ctx.Set().Where(x => x.Type == type && x.Root == root)); } \ No newline at end of file diff --git a/tests/FasTnT.Application.Tests/DataSources/EventQueryContextTests.cs b/tests/FasTnT.Application.Tests/DataSources/EventQueryContextTests.cs index 65be93b3..27012e93 100644 --- a/tests/FasTnT.Application.Tests/DataSources/EventQueryContextTests.cs +++ b/tests/FasTnT.Application.Tests/DataSources/EventQueryContextTests.cs @@ -5,6 +5,7 @@ using FasTnT.Domain.Exceptions; using FasTnT.Domain.Model; using FasTnT.Domain.Model.Events; +using FasTnT.Domain.Model.Masterdata; using FasTnT.Domain.Model.Queries; namespace FasTnT.Application.Tests.DataSources; @@ -32,12 +33,43 @@ public static void Initialize(TestContext _) RecordTime = new DateTime(2020, 03, 15, 21, 14, 10, DateTimeKind.Utc), Id = 1, SchemaVersion = "2.0", + Masterdata = [ + new MasterData + { + Type = MasterData.Location, + Id = "test_location2" + }, + new MasterData + { + Type = MasterData.Location, + Id = "test_location", + Children = [new MasterDataChildren + { + ChildrenId = "test_location2" + }] + }, + new MasterData + { + Type = MasterData.ReadPoint, + Id = "rp_test2" + }, + new MasterData + { + Type = MasterData.ReadPoint, + Id = "rp_test", + Children = [new MasterDataChildren + { + ChildrenId = "rp_test2" + }] + } + ], Events = [ new Event { Type = EventType.ObjectEvent, Action = EventAction.Add, BusinessLocation = "test_location", + ReadPoint = "rp_test2", CorrectiveDeclarationTime = new DateTime(2024, 02, 15, 21, 14, 10), CorrectiveReason = "invalid_evt", CorrectiveEventIds = new List{{ new CorrectiveEventId { CorrectiveId = "ni://test2" } }}, @@ -358,6 +390,30 @@ public void ItShouldApplyTheMATCH_anyEpcFilter() Assert.IsTrue(result.All(x => x.Epcs.Any(e => e.Id.StartsWith("epc.")))); } + [TestMethod] + [DataRow("test_location2", 2)] + [DataRow("test_location", 1)] + [DataRow("test_unknownlocation", 0)] + public void ItShouldApplyTheWD_bizLocationFilter(string value, int expected) + { + var result = Context.QueryEvents(new[] { QueryParameter.Create("WD_bizLocation", value) }).ToList(); + + Assert.IsNotNull(result); + Assert.AreEqual(expected, result.Count); + } + + [TestMethod] + [DataRow("rp_test2", 2)] + [DataRow("rp_test", 1)] + [DataRow("rp_unknown", 0)] + public void ItShouldApplyTheWD_readPointFilter(string value, int expected) + { + var result = Context.QueryEvents(new[] { QueryParameter.Create("WD_readPoint", value) }).ToList(); + + Assert.IsNotNull(result); + Assert.AreEqual(expected, result.Count); + } + [TestMethod] [DataRow("UNKNOWN", true)] [DataRow("HASATTR_bizLocation_attrv1", false)]