Skip to content

Commit

Permalink
Merge pull request #506 from louisaxel-ambroise/fix-wd-parameters
Browse files Browse the repository at this point in the history
Fix WD_ query parameters
  • Loading branch information
louisaxel-ambroise authored Mar 29, 2024
2 parents 34442c8 + 4dc682f commit 805f498
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MasterdataHierarchy>().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<MasterdataHierarchy>().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":
Expand Down
11 changes: 5 additions & 6 deletions src/FasTnT.Application/Database/EpcisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public EpcisContext(DbContextOptions<EpcisContext> options) : base(options)
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

public IQueryable<MasterdataHierarchy> QueryHierarchy(string type, string root)
{
return Set<MasterdataHierarchy>().Where(x => x.Type == type && x.Root == root);
}

public IQueryable<Event> QueryEvents(IEnumerable<QueryParameter> parameters)
{
var eventContext = new EventQueryContext(this, parameters);
Expand All @@ -27,15 +32,9 @@ public IQueryable<MasterData> QueryMasterData(IEnumerable<QueryParameter> parame

return masterdataContext.ApplyTo(Set<MasterData>());
}

public IEnumerable<MasterdataHierarchy> LocationHierarchy(string id) => QueryHierarchy(this, MasterData.Location, id);
public IEnumerable<MasterdataHierarchy> ReadPointHierarchy(string id) => QueryHierarchy(this, MasterData.ReadPoint, id);


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
EpcisModelConfiguration.Apply(modelBuilder);
}

static Func<EpcisContext, string, string, IEnumerable<MasterdataHierarchy>> QueryHierarchy => EF.CompileQuery((EpcisContext ctx, string type, string root) => ctx.Set<MasterdataHierarchy>().Where(x => x.Type == type && x.Root == root));
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CorrectiveEventId>{{ new CorrectiveEventId { CorrectiveId = "ni://test2" } }},
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 805f498

Please sign in to comment.