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 date and time functions
  • Loading branch information
TrevorPilley committed May 22, 2020
1 parent e738d08 commit c6fde98
Show file tree
Hide file tree
Showing 3 changed files with 336 additions and 11 deletions.
14 changes: 14 additions & 0 deletions Net.Http.OData.Tests/Query/QueryableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class QueryableExtensionsTests
private readonly IList<Customer> _customers;
private readonly IList<Employee> _employees;
private readonly IList<Manager> _managers;
private readonly IList<Order> _orders;
private readonly IList<Product> _products;

public QueryableExtensionsTests()
Expand Down Expand Up @@ -59,6 +60,19 @@ public QueryableExtensionsTests()
{
new Customer { AccountManager = _employees[_managers.Count + 1], Address = "Some Street", City = "Star City", CompanyName = "Target", ContactName = "Geoff Jr", Country = "USA", LegacyId = 8763, Phone = "555-4202", PostalCode = "76542" },
};

_orders = new[]
{
new Order { Customer = _customers[0], Date = new DateTimeOffset(2020, 4, 25, 9, 30, 53, new TimeSpan(2, 0, 0)), Freight = 12.00M, OrderDetails = new []{ new OrderDetail { Discount = 0.05M, Product = _products[3], Quantity = 15, UnitPrice = _products[3].Price } }, OrderId = 2632, ShipCountry = _customers[0].Country, TransactionId = new Guid("c46e35b8-0160-4b37-b5af-766d9699d0a8") }
};

foreach (Order order in _orders)
{
foreach (OrderDetail orderDetail in order.OrderDetails)
{
orderDetail.Order = order;
}
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Moq;
using Net.Http.OData.Linq;
using Net.Http.OData.Model;
using Net.Http.OData.Query;
using Xunit;

namespace Net.Http.OData.Tests.Query
{
public partial class QueryableExtensionsTests
{
[Fact]
public void Apply_Filter_Single_Property_Date()
{
TestHelper.EnsureEDM();

var queryOptions = new ODataQueryOptions(
"?$filter=date(Date) eq 2020-04-25",
EntityDataModel.Current.EntitySets["Orders"],
Mock.Of<IODataQueryOptionsValidator>());

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

Assert.Equal(_orders.Where(x => x.Date.Date == new DateTime(2020, 4, 25)).Count(), results.Count);

Assert.All(results, x => Assert.Equal(new DateTime(2020, 4, 25), ((dynamic)x).Date.Date));
}

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

var queryOptions = new ODataQueryOptions(
"?$filter=day(ReleaseDate) eq 24",
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

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

Assert.Equal(_products.Where(x => x.ReleaseDate.Day == 24).Count(), results.Count);

Assert.All(results, x => Assert.Equal(24, ((dynamic)x).ReleaseDate.Day));
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Day == 25).Count(), results.Count);

Assert.All(results, x => Assert.Equal(25, ((dynamic)x).Date.Day));
}

[Fact]
public void Apply_Filter_Single_Property_FractionalSeconds_DateTimeOffset()
{
}

[Fact]
public void Apply_Filter_Single_Property_FractionalSeconds_TimeOfDay()
{
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Hour == 9).Count(), results.Count);

Assert.All(results, x => Assert.Equal(9, ((dynamic)x).Date.Hour));
}

[Fact]
public void Apply_Filter_Single_Property_Hour_TimeOfDay()
{
}

[Fact]
public void Apply_Filter_Single_Property_MaxDateTime()
{
}

[Fact]
public void Apply_Filter_Single_Property_MinDateTime()
{
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Minute == 30).Count(), results.Count);

Assert.All(results, x => Assert.Equal(30, ((dynamic)x).Date.Minute));
}

[Fact]
public void Apply_Filter_Single_Property_Minute_TimeOfDay()
{
}

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

var queryOptions = new ODataQueryOptions(
"?$filter=month(ReleaseDate) eq 4",
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

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

Assert.Equal(_products.Where(x => x.ReleaseDate.Month == 4).Count(), results.Count);

Assert.All(results, x => Assert.Equal(4, ((dynamic)x).ReleaseDate.Month));
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Month == 4).Count(), results.Count);

Assert.All(results, x => Assert.Equal(4, ((dynamic)x).Date.Month));
}

[Fact]
public void Apply_Filter_Single_Property_Now()
{
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Second == 53).Count(), results.Count);

Assert.All(results, x => Assert.Equal(53, ((dynamic)x).Date.Second));
}

[Fact]
public void Apply_Filter_Single_Property_Second_TimeOfDay()
{
}

[Fact]
public void Apply_Filter_Single_Property_Time()
{
}

[Fact]
public void Apply_Filter_Single_Property_TotalOffsetMinutes()
{
}

[Fact]
public void Apply_Filter_Single_Property_TotalSeconds()
{
}

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

var queryOptions = new ODataQueryOptions(
"?$filter=year(ReleaseDate) eq 2020",
EntityDataModel.Current.EntitySets["Products"],
Mock.Of<IODataQueryOptionsValidator>());

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

Assert.Equal(_products.Where(x => x.ReleaseDate.Year == 2020).Count(), results.Count);

Assert.All(results, x => Assert.Equal(2020, ((dynamic)x).ReleaseDate.Year));
}

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

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

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

Assert.Equal(_orders.Where(x => x.Date.Year == 2020).Count(), results.Count);

Assert.All(results, x => Assert.Equal(2020, ((dynamic)x).Date.Year));
}
}
}
Loading

0 comments on commit c6fde98

Please sign in to comment.