Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
For #15
Browse files Browse the repository at this point in the history
Implemented mindatetime(), maxdatetime() and now()
  • Loading branch information
TrevorPilley committed Nov 13, 2020
1 parent acad42a commit 4ac460e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,35 @@ public void Apply_Filter_Single_Property_Hour_TimeOfDay()
[Fact]
public void Apply_Filter_Single_Property_MaxDateTime()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$filter=Date le maxdatetime()",
EntityDataModel.Current.EntitySets["Orders"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = _orders.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_orders.Where(x => x.Date < DateTimeOffset.MaxValue).Count(), results.Count);

Assert.All(results, x => Assert.True(((dynamic)x).Date < DateTimeOffset.MaxValue));
}

[Fact]
public void Apply_Filter_Single_Property_MinDateTime()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$filter=Date ge mindatetime()",
EntityDataModel.Current.EntitySets["Orders"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = _orders.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_orders.Where(x => x.Date > DateTimeOffset.MinValue).Count(), results.Count);

Assert.All(results, x => Assert.True(((dynamic)x).Date > DateTimeOffset.MinValue));
}

[Fact]
Expand Down Expand Up @@ -164,6 +188,18 @@ public void Apply_Filter_Single_Property_Month_DateTimeOffset()
[Fact]
public void Apply_Filter_Single_Property_Now()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$filter=Date le now()",
EntityDataModel.Current.EntitySets["Orders"],
Mock.Of<IODataQueryOptionsValidator>());

IList<ExpandoObject> results = _orders.AsQueryable().Apply(queryOptions).ToList();

Assert.Equal(_orders.Where(x => x.Date < DateTimeOffset.Now).Count(), results.Count);

Assert.All(results, x => Assert.True(((dynamic)x).Date < DateTimeOffset.Now));
}

[Fact]
Expand Down
16 changes: 14 additions & 2 deletions Net.Http.OData/Query/Linq/FilterBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ internal static class FilterBinder
private static readonly PropertyInfo s_dateTimeOffsetDate = typeof(DateTimeOffset).GetProperty("Date");
private static readonly PropertyInfo s_dateTimeOffsetDay = typeof(DateTimeOffset).GetProperty("Day");
private static readonly PropertyInfo s_dateTimeOffsetHour = typeof(DateTimeOffset).GetProperty("Hour");
private static readonly FieldInfo s_dateTimeOffsetMaxValue = typeof(DateTimeOffset).GetField("MaxValue");
private static readonly PropertyInfo s_dateTimeOffsetMinute = typeof(DateTimeOffset).GetProperty("Minute");
private static readonly FieldInfo s_dateTimeOffsetMinValue = typeof(DateTimeOffset).GetField("MinValue");
private static readonly PropertyInfo s_dateTimeOffsetMonth = typeof(DateTimeOffset).GetProperty("Month");
private static readonly PropertyInfo s_dateTimeOffsetNow = typeof(DateTimeOffset).GetProperty("Now");
private static readonly PropertyInfo s_dateTimeOffsetSecond = typeof(DateTimeOffset).GetProperty("Second");
private static readonly PropertyInfo s_dateTimeOffsetYear = typeof(DateTimeOffset).GetProperty("Year");
private static readonly PropertyInfo s_dateTimeYear = typeof(DateTime).GetProperty("Year");
private static readonly MethodInfo s_enumHasFlag = typeof(Enum).GetMethod("HasFlag");
private static readonly MethodInfo s_mathCeilingDecimal = typeof(Math).GetMethod("Ceiling", new[] { typeof(decimal) });
private static readonly MethodInfo s_mathFloorDecimal = typeof(Math).GetMethod("Floor", new[] { typeof(decimal) });
private static readonly MethodInfo s_mathRoundDecimal = typeof(Math).GetMethod("Round", new[] { typeof(decimal) });
private static readonly MethodInfo s_mathCeilingDouble = typeof(Math).GetMethod("Ceiling", new[] { typeof(double) });
private static readonly MethodInfo s_mathFloorDecimal = typeof(Math).GetMethod("Floor", new[] { typeof(decimal) });
private static readonly MethodInfo s_mathFloorDouble = typeof(Math).GetMethod("Floor", new[] { typeof(double) });
private static readonly MethodInfo s_mathRoundDecimal = typeof(Math).GetMethod("Round", new[] { typeof(decimal) });
private static readonly MethodInfo s_mathRoundDouble = typeof(Math).GetMethod("Round", new[] { typeof(double) });
private static readonly MethodInfo s_stringConcat = typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) });
private static readonly MethodInfo s_stringContains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
Expand Down Expand Up @@ -224,6 +227,12 @@ private static Expression Bind(FunctionCallNode functionCallNode)
case "length":
return Expression.Property(Bind(functionCallNode.Parameters[0]), s_stringLength);

case "maxdatetime":
return Expression.Field(null, s_dateTimeOffsetMaxValue);

case "mindatetime":
return Expression.Field(null, s_dateTimeOffsetMinValue);

case "minute":
switch (((PropertyAccessNode)functionCallNode.Parameters[0]).PropertyPath.InnerMostProperty.PropertyType.FullName)
{
Expand All @@ -247,6 +256,9 @@ private static Expression Bind(FunctionCallNode functionCallNode)
throw new NotSupportedException();
}

case "now":
return Expression.Property(null, s_dateTimeOffsetNow);

case "round":
switch (((PropertyAccessNode)functionCallNode.Parameters[0]).PropertyPath.InnerMostProperty.PropertyType.FullName)
{
Expand Down

0 comments on commit 4ac460e

Please sign in to comment.