Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests #489

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/FasTnT.Application/Database/DataSources/EventQueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace FasTnT.Application.Database.DataSources;

internal class EventQueryContext
internal sealed class EventQueryContext
{
private bool _ascending;
private int? _skip, _take;
Expand Down Expand Up @@ -78,9 +78,9 @@ private void ParseParameter(QueryParameter param)
case "EQ_correctiveEventID":
Filter(x => x.CorrectiveEventIds.Any(ce => param.Values.Contains(ce.CorrectiveId))); break;
case "WD_readPoint":
Filter(x => _context.Set<MasterdataHierarchy>().Any(h => h.Type == MasterData.ReadPoint && h.Root == x.ReadPoint && param.Values.Contains(h.Id))); break;
Filter(x => _context.Hierarchy(MasterData.ReadPoint, x.ReadPoint).Any(h => param.Values.Contains(h.Id))); break;
case "WD_bizLocation":
Filter(x => _context.Set<MasterdataHierarchy>().Any(h => h.Type == MasterData.Location && h.Root == x.BusinessLocation && param.Values.Contains(h.Id))); break;
Filter(x => _context.Hierarchy(MasterData.Location, x.ReadPoint).Any(h => 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 Expand Up @@ -203,12 +203,19 @@ public IQueryable<Event> ApplyTo(IQueryable<Event> query)

private void ApplyMasterdataAttributeParameter(string field, string attributeName, string[] values)
{
var filter = (Expression<Func<MasterDataAttribute, bool>>)(a => a.Id == attributeName);

if(values.Length > 0)
{
filter = filter.AndAlso(a => values.Contains(a.Value));
}

switch (field)
{
case "bizLocation":
Filter(e => _context.BizLocations.Any(p => p.Id == e.BusinessLocation && p.Attributes.Any(a => a.Id == attributeName && (values.Length == 0 || values.Contains(a.Value))))); break;
Filter(e => _context.Set<MasterData>().Any(p => p.Type == MasterData.Location && p.Id == e.BusinessLocation && p.Attributes.AsQueryable().Any(filter))); break;
case "readPoint":
Filter(e => _context.ReadPoints.Any(p => p.Id == e.ReadPoint && p.Attributes.Any(a => a.Id == attributeName && (values.Length == 0 || values.Contains(a.Value))))); break;
Filter(e => _context.Set<MasterData>().Any(p => p.Type == MasterData.ReadPoint && p.Id == e.ReadPoint && p.Attributes.AsQueryable().Any(filter))); break;
default:
throw new EpcisException(ExceptionType.QueryParameterException, $"Invalid masterdata field: {field}");
}
Expand All @@ -229,7 +236,14 @@ private void ParseOrderField(QueryParameter param)

private void ApplyFieldParameter(FieldType type, bool inner, string name, string ns, string[] values)
{
Filter(x => x.Fields.Any(f => f.Type == type && f.ParentIndex == null == !inner && f.Name == name && f.Namespace == ns && (values.Length == 0 || values.Contains(f.TextValue))));
var filter = (Expression<Func<Field, bool>>)(f => f.Type == type && f.ParentIndex.HasValue == inner && f.Name == name && f.Namespace == ns);

if (values.Length > 0)
{
filter = filter.AndAlso(f => values.Contains(f.TextValue));
}

Filter(x => x.Fields.AsQueryable().Any(filter));
}

private void ApplyReportUomParameter(float[] values, string uom)
Expand All @@ -239,9 +253,10 @@ private void ApplyReportUomParameter(float[] values, string uom)

private void ApplyComparison(QueryParameter param, FieldType type, string ns, string name, bool inner)
{
var customFieldPredicate = (Expression<Func<Field, bool>>)(f => f.Type == type && f.Name == name && f.Namespace == ns && f.ParentIndex != null == inner);
var customFieldPredicate = (Expression<Func<Field, bool>>)(f => f.Type == type && f.Name == name && f.Namespace == ns && f.ParentIndex.HasValue == inner);
var fieldValuePredicate = param.Compare<Field>(x => param.IsDateTime() ? x.DateValue : x.NumericValue);

Filter(x => x.Fields.AsQueryable().Any(customFieldPredicate.AndAlso(param.Compare<Field>(x => param.IsDateTime() ? x.DateValue : x.NumericValue))));
Filter(x => x.Fields.AsQueryable().Any(customFieldPredicate.AndAlso(fieldValuePredicate)));
}

private void ApplyMatchParameter(QueryParameter param)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace FasTnT.Application.Database.DataSources;

internal class MasterDataQueryContext
internal sealed class MasterDataQueryContext
{
private int _take = int.MaxValue;
private readonly List<Func<IQueryable<MasterData>, IQueryable<MasterData>>> _filters = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public static EpcType[] GetMatchEpcTypes(this QueryParameter parameter)
public static Expression<Func<T, bool>> Compare<T>(this QueryParameter parameter, Expression<Func<T, object>> accessor)
{
var param = Expression.Parameter(typeof(T));

Expression right = parameter.IsDateTime() ? Expression.Constant(parameter.AsDate()) : Expression.Constant(parameter.AsFloat());
Expression left = Expression.Convert(Expression.Invoke(accessor, param), right.Type);

Expand Down
8 changes: 5 additions & 3 deletions src/FasTnT.Application/Database/EpcisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace FasTnT.Application.Database;

public class EpcisContext : DbContext
public sealed class EpcisContext : DbContext
{
public EpcisContext(DbContextOptions<EpcisContext> options) : base(options)
{
Expand All @@ -28,8 +28,10 @@ public IQueryable<MasterData> QueryMasterData(IEnumerable<QueryParameter> parame
return masterdataContext.ApplyTo(Set<MasterData>());
}

internal IQueryable<MasterData> BizLocations => Set<MasterData>().Where(x => x.Type == "urn:epcglobal:epcis:vtype:BusinessLocation");
internal IQueryable<MasterData> ReadPoints => Set<MasterData>().Where(x => x.Type == "urn:epcglobal:epcis:vtype:ReadPoint");
public IQueryable<MasterdataHierarchy> Hierarchy(string type, string root)
{
return Set<MasterdataHierarchy>().Where(x => x.Type == type && x.Root == root);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
2 changes: 1 addition & 1 deletion src/FasTnT.Application/Handlers/DataRetrieverHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<List<Event>> QueryEventsAsync(IEnumerable<QueryParameter> para
.WhereIn(x => x.Id, eventIds)
.ToListAsync(cancellationToken);

return events.OrderBy(e => eventIds.IndexOf(e.Id)).ToList();
return new (events.OrderBy(e => eventIds.IndexOf(e.Id)));
}

public async Task<List<MasterData>> QueryMasterDataAsync(IEnumerable<QueryParameter> parameters, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async ValueTask<CaptureDocumentRequest> BindAsync(HttpContext cont
}
else
{
var headerContext = context.Request.Headers.TryGetValue("GS1-Extensions", out var extensions) ? extensions.ToArray() : Array.Empty<string>();
var headerContext = context.Request.Headers.TryGetValue("GS1-Extensions", out var extensions) ? extensions.ToArray() : [];
var epcisContext = Namespaces.ParseHeader(headerContext);

request = await JsonCaptureRequestParser.ParseDocumentAsync(context.Request.Body, epcisContext, context.RequestAborted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class EventQueryContextTests
[ClassCleanup]
public static void Cleanup()
{
if (Context != null)
{
Context.Database.EnsureDeleted();
}
Context?.Database.EnsureDeleted();
}

[ClassInitialize]
Expand All @@ -35,7 +32,7 @@ public static void Initialize(TestContext _)
RecordTime = new DateTime(2020, 03, 15, 21, 14, 10),
Id = 1,
SchemaVersion = "2.0",
Events = new List<Event> {
Events = [
new Event
{
Type = EventType.ObjectEvent,
Expand All @@ -48,7 +45,7 @@ public static void Initialize(TestContext _)
EventId = "ni://test",
EventTime = new DateTime(2020, 02, 15, 21, 14, 10),
EventTimeZoneOffset = "+02:00",
Epcs = new List<Epc>{ new Epc { Id = "epc1", Type = EpcType.List } }
Epcs = [new Epc { Id = "epc1", Type = EpcType.List }]
},
new Event
{
Expand All @@ -60,9 +57,9 @@ public static void Initialize(TestContext _)
Disposition = "disposition",
EventTime = new DateTime(2021, 02, 15, 21, 14, 10),
EventTimeZoneOffset = "+01:00",
Epcs = new List<Epc>{ new Epc { Id = "epc2", Type = EpcType.List }, new Epc { Id = "epc.value.1", Type = EpcType.List } },
Fields = new List<Field>
{
Epcs = [ new Epc { Id = "epc2", Type = EpcType.List }, new Epc { Id = "epc.value.1", Type = EpcType.List }],
Fields =
[
new Field
{
Index = 1,
Expand All @@ -89,9 +86,9 @@ public static void Initialize(TestContext _)
NumericValue = 2.5,
TextValue = "2.5"
}
}
]
}
}
]
}
});

Expand All @@ -110,7 +107,7 @@ public void ItShouldReturnTheStoredEvents()
[TestMethod]
public void ItShouldRestrictTheDataIfTheEventCountLimitIsExceeded()
{
var result = Context.QueryEvents(new[] { new QueryParameter { Name = "eventCountLimit", Values = new[] { "1" } } }).ToList();
var result = Context.QueryEvents(new[] { new QueryParameter { Name = "eventCountLimit", Values = [ "1" ] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand All @@ -119,7 +116,7 @@ public void ItShouldRestrictTheDataIfTheEventCountLimitIsExceeded()
[TestMethod]
public void ItShouldRestrictTheDataIfTheMaxEventCountIsExceeded()
{
var result = Context.QueryEvents(new[] { new QueryParameter { Name = "maxEventCount", Values = new[] { "1" } } }).ToList();
var result = Context.QueryEvents(new[] { new QueryParameter { Name = "maxEventCount", Values = ["1"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand Down Expand Up @@ -374,11 +371,11 @@ public void ItShouldThrowAnExceptionIfTheParameterIsUnknown(string paramName, bo
{
if (throws)
{
Assert.ThrowsException<EpcisException>(() => Context.QueryEvents(new[] { new QueryParameter { Name = paramName, Values = new[] { "value" } } }).ToList());
Assert.ThrowsException<EpcisException>(() => Context.QueryEvents(new[] { new QueryParameter { Name = paramName, Values = ["value"] } }).ToList());
}
else
{
var result = Context.QueryEvents(new[] { new QueryParameter { Name = paramName, Values = new[] { "value" } } }).ToList();
var result = Context.QueryEvents(new[] { new QueryParameter { Name = paramName, Values = ["value"] } }).ToList();

Assert.IsNotNull(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void ItShouldReturnTheStoredMasterdata()
[TestMethod]
public void ItShouldRestrictTheDataIfTheMaxElementCountLimitIsExceeded()
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "maxElementCount", Values = new[] { "1" } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "maxElementCount", Values = ["1"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand All @@ -123,7 +123,7 @@ public void ItShouldRestrictTheDataIfTheMaxElementCountLimitIsExceeded()
[TestMethod]
public void ItShouldApplyTheVocabularyNameParameter()
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "vocabularyName", Values = new[] { "urn:epcglobal:epcis:vtype:ReadPoint" } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "vocabularyName", Values = ["urn:epcglobal:epcis:vtype:ReadPoint"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand All @@ -133,7 +133,7 @@ public void ItShouldApplyTheVocabularyNameParameter()
[TestMethod]
public void ItShouldApplyTheEQNameParameter()
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "EQ_name", Values = new[] { "MD02" } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "EQ_name", Values = ["MD02"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand All @@ -143,7 +143,7 @@ public void ItShouldApplyTheEQNameParameter()
[TestMethod]
public void ItShouldApplyTheWDNameParameter()
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "WD_name", Values = new[] { "MD03" } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "WD_name", Values = ["MD03"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count);
Expand All @@ -154,7 +154,7 @@ public void ItShouldApplyTheWDNameParameter()
[TestMethod]
public void ItShouldApplyTheHasAttrParameter()
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "HASATTR", Values = new[] { "MD2AT1" } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = "HASATTR", Values = ["MD2AT1"] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(1, result.Count);
Expand All @@ -165,7 +165,7 @@ public void ItShouldApplyTheHasAttrParameter()
[DataRow("MD2AT1", "NOVALUE", 0)]
public void ItShouldApplyTheEqAttrParameter(string paramName, string value, int expectedResult)
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = $"EQATTR_{paramName}", Values = new[] { value } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = $"EQATTR_{paramName}", Values = [value] } }).ToList();

Assert.IsNotNull(result);
Assert.AreEqual(expectedResult, result.Count);
Expand All @@ -186,11 +186,11 @@ public void ItShouldThrowAnExceptionIfTheParameterIsUnknown(string paramName, st
{
if (throws)
{
Assert.ThrowsException<EpcisException>(() => Context.QueryMasterData(new[] { new QueryParameter { Name = paramName, Values = new[] { paramValue } } }).ToList());
Assert.ThrowsException<EpcisException>(() => Context.QueryMasterData(new[] { new QueryParameter { Name = paramName, Values = [paramValue] } }).ToList());
}
else
{
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = paramName, Values = new[] { paramValue } } }).ToList();
var result = Context.QueryMasterData(new[] { new QueryParameter { Name = paramName, Values = [paramValue] } }).ToList();

Assert.IsNotNull(result);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10</LangVersion>
<IsPackable>false</IsPackable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void Initialize(TestContext _)
UserId = UserContext.UserId,
Parameters = new List<QueryParameter>
{
new QueryParameter{ Name = "EQ_type", Values = new []{ "ObjectEvent", "TestEvent" }}
new QueryParameter{ Name = "EQ_type", Values = ["ObjectEvent", "TestEvent"]}
}
},
new StoredQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void Initialize(TestContext _)
Name = "QueryOne",
Parameters = new List<QueryParameter>
{
new QueryParameter{ Name = "EQ_type", Values = new []{ "ObjectEvent", "TestEvent" }}
new QueryParameter{ Name = "EQ_type", Values = ["ObjectEvent", "TestEvent"]}
}
},
new StoredQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static void Initialize(TestContext _)
Name = "QueryOne",
Parameters = new List<QueryParameter>
{
new QueryParameter{ Name = "EQ_type", Values = new []{ "ObjectEvent", "TestEvent" }}
new QueryParameter{ Name = "EQ_type", Values = ["ObjectEvent", "TestEvent"]}
}
}
});
Expand Down
Loading